Click here to Skip to main content
15,867,330 members
Articles / Containers / Virtual Machine

Setting up an Open Source tool chain

Rate me:
Please Sign up or sign in to vote.
4.67/5 (13 votes)
12 Jun 2009CPOL7 min read 53.3K   48   20
This article shares my experiences setting up an Open Source tool chain for developing C++ applications.

Introduction

This article describes the steps I have taken to set up an Open Source tool chain I use to write C++ programs.

In it, I show how I:

  • Set up a text editor with code highlighting
  • Set up a Unix style environment required to build and compile C++ applications
  • Set up a C/C++ compiler
  • Create and compile a Hello World C++ program using a make file

Background

A while back, I decided to share the source code for an application I developed. I had written the program using Microsoft C++, Visual Studio, and made heavy use of MFC. I found that there was a pool of people who would be willing to help, but they were unable to help because they didn't have Microsoft C++. For this reason, I started investigating alternatives to using proprietary tools, and in this article, I share my experiences.

A secondary reason for writing this article is that I want to write further articles about other technologies I have found out about. This article will allow people to know the tool chain I use so they can follow further articles.

Prerequisites for following this tutorial

To write this tutorial, I have started off with a clean install of Windows XP Home edition. I am actually using a VMware virtual machine for this. The tutorial should work on most Windows versions.

Download and install Notepad++

Notepad++ is an Open Source text editor that includes features like tabbed browsing and syntax highlighting. It is also customizable which will allow me to add menu items to compile and run the programs with one click.

Download the latest version at http://sourceforge.net/project/showfiles.php?group_id=95717&package_id=102072. I downloaded npp.5.4.3.Installer.exe.

Setting it up is easy, and I used all the defaults so I won't go through the steps here. You can check Notepad++ works by running the application which should be in your Program Files menu.

Note: If you want to find out more about Notepad++, the website is http://notepad-plus.sourceforge.net/uk/site.htm.

Download and install MinGW

This is the actual compiler tool chain.

Install this using the auto installer http://sourceforge.net/project/showfiles.php?group_id=2435&package_id=240780.

I selected options as follows:

  1. Download and install
  2. Current version
  3. Full initialization (you must install the base tools, G++, and make as a minimum, but I recommend installing everything)

For everything else, I use the default settings.

Download and install MSYS

Although not strictly necessary, I use MSYS. This is a minimal Unix style SYStem for Windows, and it sets up a Linux like environment on a Windows machine. I download and use various libraries with my programs (zipping libraries, GTK+, MySQL libraries etc.) and the make files frequently contain Unix commands. The main example is the make clean section which uses rm rather than del. MinGW will make using these a lot easier.

Go to http://sourceforge.net/project/showfiles.php?group_id=2435&package_id=24963. You can select the current release section, and it will expand to show the current release. I downloaded MSYS-1.0.10.exe.

I installed it with all the default setup and options.

Note: If you want to find out more about MSYS and MinGW, the website is http://www.mingw.org/.

Set up Environment Variables

We need the MinGW bin directory and the MSYS bin directory to be in the path. To do this, go to Control Panel -> System -> Advanced tab -> Environment Variables button. Under the system variables section, find the path variable and press Edit. Add the MinGW directory to the end. (Addresses are separated by ; so I need to add ;C:\MinGW\bin;C:\msys\1.0\bin on my system.)

While you are there, add a user variable called HOME and set it to the value C:\msys\1.0\home\<<WINDOWS USER NAME>>.

Note: Don't surround the path with "'s. This will cause make to not find the directory. (Although the normal shells will.)

Check if this has worked by running the command prompt and typing gcc --help. If you get a command not found error, it means the path is not correctly set up.

Set up a C/C++ compiler

Finally, we are ready to get our first program together and compiled. Load up MSYS by clicking the blue M icon. (I usually put it in my quick launch for convenience.) Follow these steps:

Note: You can copy the command from here and press shift + INS in MSYS to run it

Make a code directory:

  • cd /c/
  • mkdir code

Make a directory for our Hello World application:

  • cd /c/code/
  • mkdir hello_world

Create the C++ program:

  • "/c/program files/notepad++/notepad++.exe" /c/code/hello_world/main.cpp
  • Paste in the following code, then save and close Notepad++:
C++
#include <iostream>

int main( int argc, char *argv[] )
{
    printf("Hello World\n");
    return 0;
}

Careful copying this code. <'s may be turned into &lt; and >'s may be turned into &gt;.

Don't forget the newline at the end of the file.

Create the Makefile:

  • "/c/program files/notepad++/notepad++.exe" /c/code/hello_world/makefile
  • Paste in the following code, then save and close Notepad++:
$(warning Starting Makefile)

CXX=g++

main.exe: main.cpp
    $(CXX) main.cpp -o main.exe

clean: 
    -rm main.exe

Test the setup by making and running the program:

  • cd /c/code/hello_world/
  • make
  • ./main.exe

You should see the text Hello World appear when the program has run.

Finally, check the clean up works OK:

  • cd /c/code/hello_world/
  • make clean

You should see that the exe file has gone.

Setting up the Compile and Run commands in Notepad++

We can set up Notepad++ shortcuts. First, create two batch files using these steps:

  • "/c/program files/notepad++/notepad++.exe" /c/code/run.bat
  • Paste in the following code, then save and close Notepad++:
:##BATCH to run fron notepad++
c:
cd\
cd %1
make
pause
main.exe
pause
  • "/c/program files/notepad++/notepad++.exe" /c/code/run_clean.bat
  • Paste in the following code, then save and close Notepad++:
:##BATCH to run fron notepad++
c:
cd\
cd %1
make clean
pause

Now, we can set up Notepad++ to run these batch files:

  • Load Notepad++
  • Press F5
  • Enter c:\code\run.bat $(CURRENT_DIRECTORY)
  • Save the command as RUN CODE
  • Press F5
  • Enter c:\code\run_clean.bat $(CURRENT_DIRECTORY)
  • Save the command as CLEAN

All done - so what have we got?

We now have a tool chain set up. The directory c:\code contains the programs and we can create a sub directory for new programs. Each sub directory will need a makefile and the program files. The makefile can be changed depending on what program files are needed. You can use Notepad++ for editing, and use the RUN CODE menu option to make and run the program. The run code command will run make in the directory the current file in Notepad++ is running.

This may all seem pretty simple and not worth an article, but I plan to write more articles and need to share how my system is set up for them to make sense.

Articles (so far) using this

Points of interest

Make file notes

  • When changing the makefile, you should use the make clean option. Normally, the make program will automatically check which commands it needs to run based on file modified dates. This won't work when you are changing the makefile, so you can use the make clean option to force it to re-run.
  • Make is a very powerful system for constructing programs. I have included a simple make script that works, but make scripts can also be more advanced. You can configure for DEBUG and RELEASE builds. I used to let my compiler tools create make files for me, but I have found that hand crafting them has forced me to learn a lot about the build process, and I now understand error messages in the build process much better.

Feedback

This is the process I have used to set up this tool chain. I am interested in hearing from anyone who tries to follow these steps and hear about your experiences. If you can help me change the instructions to make it easier for others, that would be great. I am also interested to know what people think about the way I have setup my tool chain. It's always good to learn more and improve the way you do things.

History

  • 07-Jun-2009 - First version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAttention Newbies ... Why you got my 5 Pin
programmersmind6-Jul-09 19:12
programmersmind6-Jul-09 19:12 
GeneralI can't change this anymore Pin
metcarob24-Jun-09 8:42
metcarob24-Jun-09 8:42 
GeneralMake files are horrible and time sucking Pin
visualc9-Jun-09 1:43
visualc9-Jun-09 1:43 
I agree on the importance of compilers for GNU/Linux and other platforms, that is the only way to achieve a multiplatform C++ software. Instead I disagree completely on the use of the make program. The syntax of make files is horrible, and error prone. The Make program is old and it is not designed in an object oriented way. The build system should be simple, because developers already have to work on the C++ code. There are many projects about new build systems for C++ that do not rely on the Make program and its makefiles. One of these is C++ Build Studio[^], a simple and robust build system for C++, written in standard C++, and designed to support every C++ compiler available. The project is still under development, maybe I will release it as open source. However your article is useful and well written.
GeneralRe: Make files are horrible and time sucking Pin
metcarob10-Jun-09 9:55
metcarob10-Jun-09 9:55 
GeneralRe: Make files are horrible and time sucking Pin
Jim Crafton16-Jun-09 3:37
Jim Crafton16-Jun-09 3:37 
GeneralFreebees Pin
geoyar8-Jun-09 15:49
professionalgeoyar8-Jun-09 15:49 
GeneralRe: Freebees Pin
PedroMC8-Jun-09 23:13
PedroMC8-Jun-09 23:13 
GeneralRe: Freebees Pin
geoyar9-Jun-09 5:25
professionalgeoyar9-Jun-09 5:25 
GeneralRe: Freebees Pin
PedroMC9-Jun-09 23:51
PedroMC9-Jun-09 23:51 
GeneralRe: Freebees Pin
metcarob10-Jun-09 9:56
metcarob10-Jun-09 9:56 
GeneralRe: Freebees Pin
PedroMC11-Jun-09 0:05
PedroMC11-Jun-09 0:05 
GeneralA better way. PinPopular
Andrew Maclean8-Jun-09 14:07
Andrew Maclean8-Jun-09 14:07 
GeneralNice article but Pin
Jim Crafton8-Jun-09 4:41
Jim Crafton8-Jun-09 4:41 
GeneralRe: Nice article but Pin
metcarob8-Jun-09 11:10
metcarob8-Jun-09 11:10 
GeneralRe: Nice article but Pin
Jim Crafton8-Jun-09 11:16
Jim Crafton8-Jun-09 11:16 
GeneralAn alternative [modified] Pin
Jim Xochellis7-Jun-09 22:29
Jim Xochellis7-Jun-09 22:29 
GeneralRe: An alternative Pin
metcarob8-Jun-09 11:11
metcarob8-Jun-09 11:11 
GeneralRe: An alternative Pin
Spatlabor9-Jun-09 0:10
Spatlabor9-Jun-09 0:10 
GeneralWhy my 5 Pin
DaTxomin7-Jun-09 13:55
DaTxomin7-Jun-09 13:55 
GeneralRe: Why my 5 Pin
metcarob8-Jun-09 11:13
metcarob8-Jun-09 11:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.