Click here to Skip to main content
Click here to Skip to main content

Setting up an Open Source tool chain

, 12 Jun 2009
Rate this:
Please Sign up or sign in to vote.
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++:
#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)

About the Author

metcarob
Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
GeneralAttention Newbies ... Why you got my 5 Pingroupprogrammersmind6-Jul-09 19:12 
1. Focus in general, I've seen many incident where a new guy want to start programming, read a couple of books but get confused when comes to day-to-day real life programming, aka confused with tool chain etc. A very thinkful mind is obviously needed to catch this issue, I sincerely thanks to you for this effort.
 
2. All the points are damn clear, and represented in a Perfect Manner. I admit that you could certainly go to a bit more extend, but if you wrote this article not for a general mass, only for newbies, you are the winner.
 
Great job once again
 
T.S Chowdhury

GeneralI can't change this anymore Pinmembermetcarob24-Jun-09 8:42 
GeneralMake files are horrible and time sucking Pinmembervisualc9-Jun-09 1:43 
GeneralRe: Make files are horrible and time sucking Pinmembermetcarob10-Jun-09 9:55 
GeneralRe: Make files are horrible and time sucking PinmemberJim Crafton16-Jun-09 3:37 
GeneralFreebees Pinmembergeoyar8-Jun-09 15:49 
GeneralRe: Freebees PinmemberPedroMC8-Jun-09 23:13 
GeneralRe: Freebees Pinmembergeoyar9-Jun-09 5:25 
GeneralRe: Freebees PinmemberPedroMC9-Jun-09 23:51 
GeneralRe: Freebees Pinmembermetcarob10-Jun-09 9:56 
GeneralRe: Freebees PinmemberPedroMC11-Jun-09 0:05 
GeneralA better way. PinmemberAndrew Maclean8-Jun-09 14:07 
GeneralNice article but PinmemberJim Crafton8-Jun-09 4:41 
GeneralRe: Nice article but Pinmembermetcarob8-Jun-09 11:10 
GeneralRe: Nice article but PinmemberJim Crafton8-Jun-09 11:16 
GeneralAn alternative [modified] PinmemberJim Xochellis7-Jun-09 22:29 
GeneralRe: An alternative Pinmembermetcarob8-Jun-09 11:11 
GeneralRe: An alternative PinmemberSpatlabor9-Jun-09 0:10 
GeneralWhy my 5 PinmemberDaTxomin7-Jun-09 13:55 
GeneralRe: Why my 5 Pinmembermetcarob8-Jun-09 11:13 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 12 Jun 2009
Article Copyright 2009 by metcarob
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid