Prepare a Unix Based C Development Environment (Ubuntu 18.04)
C Development Environment refers to which platform or operating system you will be developing and running your C applications on. I recommend compiling and running your C application on a Unix-based operating system. It could be
Debian…etc. This is because C applications are normally run on unix-based operating system to provide some sort of service without a graphical display, applications such as a database and authentication services. Of course, you could also run C on Windows, which is not as common.
In this example, we will set up Ubuntu Linux 18.04 operating system as a virtual machine on a Windows laptop. If you are a serious developer and you don’t have a MAC, I would suggest install Ubuntu Linux or other Unix-like operating system on your laptop as host operating system, rather than virtual machine. If you had to use a virtual machine, here’s how.
Download Ubuntu 18.04 ISO image
Official image can be downloaded from Ubuntu releases page here.
Download a virtualization software of your choice.
We recommend either VMWare Workstattion Player:
or VirtualBox to virtualize Ubuntu.
Install Ubuntu 18.04 on a Virtual Machine
Using VMWare Workstation Player as example, Ubuntu can be installed by following the procedure below:
1. Open VMWare Workstation Player and select
Create a New Virtual Machine:
2. Navigate to the Ubuntu 18.04 ISO image you have just downloaded:
3. Enter username and password information for the virtual machine and click ‘Next’:
4. Give your virtual machine a name and specify a location for storage. You can make a backup copy of the storage files to restore your Virtual Machine in case you need it.
5. Configure storage size. Default is 20 GB. This is up to you.
6. Review the settings. I would change the network adapter setting from
bridged so that this virtual machine sits on the same network level as my host machine and have similar IP addresses. This is my personal preference. Click
Customize Hardware to make the change.
7. Change networking to
bridged and close, then click
Finish to complete the setup.
Play Virtual machine to turn it on, and follow the Ubuntu installation wizard to finish the installation.
9. If all goes well, you will have a virtualized Ubuntu for building and running C programs.
Choose an IDE for C Development Environment
Now, you have Ubuntu environment to compile and build C program, you will also need a IDE (Integrated Development Environment). My favorite IDE is
Eclipse IDE for C. You can download Eclipse here. Choose Eclipse IDE for C/C++ Developers.
Extract the packaged downloaded and double click on Eclipse icon to launch it.
Now, you are ready to develop C applications!
Import a Makefile Project to Eclipse
Once you have Eclipse installed, you can create or import Makefile project for the very first time. If you have not installed Eclipse, you can refer to the procedures above to install Please note that I mentioned a Makefile project rather than a regular C/C++ project because Makefile is a popular method of building modern C applications on Unix-based platforms and we will be using this method to build the C programs in the subsequent examples.
You can download a sample program here, including a Makefile, 2 source files and 1 header file.
1) Select File -> New -> Makefile Project with Existing Code
2) Give your project a name and specify the path to the folder that contains Makefile, source file and header files. Here you will choose a toolchain for building the application, a toolchain is just a fancier word for a compiler.
- If you would like to edit the code on Eclipse IDE on Windows, copy everything to the virtual machine and have it compiled there via command line, you can leave the toolchain as
none. If your Eclipse IDE is installed on Ubuntu, I would recommend the same, and just use the command line
makecommand to build your C application.
- If you would like to build the program right on Eclpse IDE on Windows, you can select Cygwin GCC instead
3) Now, you should have a new Makefile project visible on the project explore window to the left. Congrats, you should have everything now to build your first C application.
Build the Application
You may have heard the term “build” or “compile” to generate a C application from source code. They actually refer to different stages during the application generation.
- Build – normally refers to every stage in compilation and to generate a executable program.
- compile – normally refers to the “compile” stage during the entire build process. Compile means to turn a piece of source code into an object file that machine can understand. This object file itself is not the final executable. Rather, a small component of the final program.
Refer to this post here for more details about how exactly a compiler works.
Build and Run C Application on Eclipse
4) If you chose
cygwin gcc during project creation, you should be able to build the program right on Eclipse IDE, which will produce a
.exe program. Navigate to Project -> Build All to carry out the build. A mini-console will be opened and it will show you the build commands used and the results. Eclipse issues the build command according to the Makefile that we have provided. A new application called
myprogram.exe will appear on the project navigation on the left.
5) To run the application right on Eclipse IDE, we can navigate to
myprogram.ext on the left, right click on it, and select run as -> local C/C++ Application:
6) A mini-terminal will again appear to interact with your application:
Build and Run C Application on Ubuntu via Make Command
In addition to building C application on Eclipse IDE on Windows, you also have a choice to build it on the Ubuntu Linux environment that we have just installed. You will have to copy the example source codes to a location on the linux machine, for example, “~/c-prog-class/myprogram-part1”. You can use
WinSCP to copy files between Windows and Linux machine. Once the files are there, you can build and run the program by:
# to build cd ~/c-prog-class/myprogram-part1 make # to run ./myprogram
Hi, this is Cary, your friendly tech enthusiast, educator and author. Currently working as a software architect at Highgo Software Canada. I enjoy simplifying complex concepts, diving into coding challenges, unraveling the mysteries of software. Most importantly, I like sharing and teaching others about all things tech. Find more blogs from me at highgo.ca