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

Setting up an Open Source tool chain

By , 12 Jun 2009
 

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
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralAttention Newbies ... Why you got my 5groupprogrammersmind6 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 anymoremembermetcarob24 Jun '09 - 8:42 
I wanted to add to the articles so far using this seciton.
I wanted to link to http://www.codeproject.com/KB/cpp/starting_sqlite_in_cpp.aspx
This shows writing a program using SQLite
GeneralMake files are horrible and time suckingmembervisualc9 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 suckingmembermetcarob10 Jun '09 - 9:55 
Thanks for the comments about my article and about Make. I am on a learning journey (and always well be) as far as this is concered and this is a good way to learn from others experences.
GeneralRe: Make files are horrible and time suckingmemberJim Crafton16 Jun '09 - 3:37 
SCons and CMake are interesting and worth a look at too.
 

¡El diablo está en mis pantalones! ¡Mire, mire!
SELECT * FROM User WHERE Clue > 0
0 rows returned

Save an Orange - Use the VCF!
Personal 3D projects
Just Say No to Web 2 Point Oh


GeneralFreebeesmembergeoyar8 Jun '09 - 15:49 
I tried many open source development tools. I always got a subptime IDEs, not very good compilers and weak debuggers. I prefer the tools from professionals.
If you want to go free of charge, download MC VC++ express edition. You will get editor, compiler, linker, debugger - all for free, and all top quality. No problems with multiplatform development - just don't use MS C++ extensions. (Note: I am not an admirer of Microsoft, but the Express is good.)
But it might be better to invest couple hunderd dollars to buy tools on Ebay - like CodeWrite(now discontihued), MS VS Standard, etc.
IMHO you recommend the wrong way to aquire development tools.
 
geoyar

GeneralRe: FreebeesmemberPedroMC8 Jun '09 - 23:13 
geoyar wrote:
No problems with multiplatform development

 
For cross platform development, Microsoft development tools are inferior, by far, to the GNU development tools. Also, GCC is a better compiler that any Microsoft provides and GDB and valgrind are excellent debugging tools.
 
The only thing Microsoft development tools are best is for Windows only development, but use the Intel or GCC compiler for best performance.
 

GeneralRe: Freebeesmembergeoyar9 Jun '09 - 5:25 
Please elaborate why MS development tools are inferior for the cross-platform development, why gcc is better compiler and why gdb is better debugger?
 
geoyar

GeneralRe: FreebeesmemberPedroMC9 Jun '09 - 23:51 
The GNU build tools (autoconf, automake, binutils, make, gcc) make it very easy to build a binary for many architectures and operating systems. I routinely auto build in GNU/Linux for GNU/Linux, GNU/FreeBSD and Windows (mingw) with a single click. Part of the auto build includes uploading and testing in vitualbox virtual machines.
 
GDB supports many OSs and architectures, has more features than I can count, requires few resources and allows remote control making it very usable in embedded hardware, and can be automated with scripting.
 
GCC supported target architectures:
http://en.wikipedia.org/wiki/GNU_Compiler_Collection#Architectures[^]
 
Some archs-OSs sets GCC targets:
http://gcc.gnu.org/install/specific.html[^]
 

GeneralRe: Freebeesmembermetcarob10 Jun '09 - 9:56 
Do you think I should look into using automake instead of make?

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 12 Jun 2009
Article Copyright 2009 by metcarob
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid