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:
- Download and install
- Current version
- 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:
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 < and >'s may be turned into >.
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.