If we want to program in c++, the very first thing we need to do is set up a c++ compiler and linker. It will also make it easier on us if we use an Integrated Development Environment, also known as an IDE. Fortunately for us, we can get these things in one, simple package: the Dev-c++ IDE.
Note: While I, personally, like MinGW and Dev-c++, there are many other good IDEs and programming packages (henceforth referred to as “compilers”) available. But since these are the tools that I use, I will limit my discussion to these.
Since Dev-c++ is open source, it comes in a couple of different versions. I am currently using the “Orwell” Dev-c++ version available on SourceForge. I chose it because the development and updates are readily available.
The Dev-c++ IDE comes in an executable ready to be installed on your system. It brings with it the MinGW compiler and toolset. Since I have never had any problems with its installation, I only have one piece of advice that will be useful in the future: install it to your root directory instead of “program files” or elsewhere (see following edit). If you ever want to compile extra libraries (which you probably will) or work in the command line (which is very useful sometimes) it will be much easier if Dev-c++ is installed in the root directory instead of buried deep in the drive.
I want to clarify my recommend installation location. Dev-c++, by default, wants to install to a directory called “Dev-Cpp” in a regular Windows location like “C:\program files\Dev-Cpp”. If we change the installation location to “C:\Dev-Cpp” instead, it will be easier to add the MinGW compiler to our path parameter at a later time if needed (and we probably will want to add it to our path parameter at some point).
Now that we have the compiler and IDE installed, it’s time to see some of the different settings.
Common Dev-c++ settings
Most, if not all, the settings we need to create a program are already configured when the Dev-c++ IDE is installed. But depending on the program we are going to make, we might need to either add some settings or change some of the defaults.
MinGW is a collection of command line tools. One of the beautiful things about Dev-c++ is that it will provide the c++ compiler inside MinGW with all the information it needs to create our program. But sometimes programs need more than just the original code. Simetimes our program will need to be linked to other libraries.
If we need to link our program to a different library, here is how we do it:
We have already created our part of the program. We have the various files in a project that we want to compile and link.
But during the linking process we want to link it, for instance, to the wsock32.dll.
MinGW uses a library format that is common on Unix systems (including Linux, and other *nix systems). This library system is less common on Windows, but works just fine regardless. In the Windows world we usually identify libraries by their extension. The extension is commonly either dll for a dynamically linked library or lib for a static library. In the *nix world, it is common to preface a library with the “lib” prefix and an “a” extension. So in *nix the “wsock32.dll” library would look something like “libwsock32.a”. You will note that right after the “lib” prefix the file name is the same until the extension.
Since virtually all *nix libraries use lib(name).a format, this is also the format that MinGW knows. And since it knows this format, it will assume that there is a “lib” prefix and an “a” suffix. In fact, it will only look for the (name) portion of the library using the default command line variables.
The command line option to link is the “-l” switch. You don’t even need to put quotes around it. MinGW will look at the -l and assume that the rest of the letters form the name of the library it is looking for.
Going back to our example of “wsock32.dll” this would make the MinGW command line switch look like this: -lwsock32
Now that we know the format, it is easy to send the command line argument to the linker.
First we click on the “Project” menu and select “Project Options …”:
That will open a Project Options dialog box:
From the list of tabs across the top of the dialog window, select the “Perimeters” tab. The Perimeters tab is where you can add the compiler and linker options that aren’t available elsewhere. For instance, this is where you can type in the -lwsock32 line to cause MinGW to give you access to Windows sockets.
Before we leave this area completely, lets look at another very useful tab. Right next to the “Perimeters” tab is one called “Compiler”. This tab has six (6) sub-tabs that store many different options for the command line. If it is one of these switches (or options) that you need to turn off or on, it can be done there instead of adding it manually in the boxes under the “Perimeters” tab.
Since this essay was mostly demonstrating how to link to a “Windows” library like wsock32.dll, it seems to be a good place to explain that you MinGW isn’t actually linking to wsock32.dll with the -lwsock32 command line switch. If you try to link directly to the wsock32.dll file, you will get an error. Instead, what MinGW is doing is actually linking to a file called “libwsock32.a” like we discussed above.
But that begs the question, what the heck is libwsock32.a?
If you look around in the directories (folders) that are a part of MinGW, you will find several collections of libraries. One of those collections carries libraries that stand in for their Windows counterparts.
The folder they are stored in on my computer is:
There are many different libraries stored in the directory other than just the Windows tie-ins. But now that we know how *nix and MinGW name their libraries, we can begin to make sense of all the different libraries stored there as well as which ones are sometimes needed on a Windows machine.
The (long) listing of the directory on my computer is below the fold:
Continue reading C++ programming tools: Orwell Dev-c++