5

It's been quite some times since I began learning C and C++ but I've been very limited only to the Windows platform and the Visual Studio environment. Recently, I wanted to look into some open source C/C++ projects, so I downloaded a few but I was frustrated because I didn't know how to compile those software. Then I had to learn about all those g++ and make stuff and still I only have very limited knowledge in those subjects.

Having been used to an environment like Visual Studio, where you can write the code, hit the play button and boom you're all good to go, it was hard for me to compile a program with the command line at first.

So my question is, if you're a C/C++ developer, how do you compile your code ? How do you compile relatively large code bases ? What are the tools that you use ? Do you use IDEs or simply text editors ? What is the compiler that you use ? Do you build your applications from the command line ? What is the OS that you usually compile on ?

SideNote: My linux knowledge is zero.

6
  • 3
    Those are all tools that run on *nix environments (most notably Linux). In other words, your side note answers your question. Cygwin is a port that can allow you to run it on Windows, but realistically, just install Ubuntu and make your machine a dual boot machine. Once you learn to use Linux, your other questions will be much easier to answer.
    – riwalk
    Commented Feb 14, 2015 at 18:47
  • 1
    in almost all cases, 1) there is no need to (re) compile code that has not changed. 2) when several files must be linked together, often there is a required order for the files in the link and specific locations for files/data. 3) compiling files one-by-one, then linking is very tedious. The best way is by using 'make' and its' many time saving features. When the specifics of the link require specific ordering of the files, in specific locations, then a .cmd file needs to be created and passed to the linker step Commented Feb 14, 2015 at 19:10
  • generally, for open source projects, after downloading/installing the source and header and library files, the first step is running './configure' then 'make' (or 'make all') then 'install' while in the 'home' directory of the project. Sometimes, a step before ./configure is to edit a .cfg (or similar) file to setup for your specific architecture. Commented Feb 14, 2015 at 19:15
  • 3
    The first step is to understand the C++ compilation and linking process (FAQ on Stackoverflow). Pay attention to the intermediate files - the object files, and the static libraries.
    – rwong
    Commented Feb 14, 2015 at 19:37
  • 1
    When I want to compile and install a program, I usually just use emerge <name-of-package>, which takes care of all the complicated bits for me. gentoo.org/main/en/about.xml Commented Feb 15, 2015 at 0:49

2 Answers 2

16

At least for free software on Linux, you usually use some builder like make. You could use some other builder program, like scons or omake

For some (mostly historical) reasons, the Makefile may be generated by utilities like autoconf or cmake; these generators also deal with configuration issues (e.g. they disable some features of the software if a dependency is missing). Some frameworks also provide their own Makefile generators, e.g. qmake for Qt

PS. Recent GNU make 4 has a lot of new features (plugins and guile scriptability) which are worthwhile (and probably could IMHO remove the necessity of generating Makefile-s).

When building large C++ code base, I recommend using command line utilities (notably make), because you understand more what is really happening. Also, a lot of large C++ code bases are sometimes using specialized C++ code generators (e.g. the GCC compiler has a dozen of them, internal to it). There are good reasons to sometimes generate some C++ code (perhaps with a simple awk or python script), e.g. some headers or constants, etc.

To build many GNU -or other- large free software bases -at least on Linux-, you generally don't need to understand all the details of their building. Usually, a README or an INSTALL text file explains how to do that, and it is often -for software using autoconf- (but not always) as simple as running the three commands

 ./configure
 make
 sudo make install

You might prefer to run make install DESTDIR=/tmp/installdir/ then copy the /tmp/installdir with e.g. sudo cp -va /tmp/installdir/ /

For software using cmake, building them is often as simple as

mkdir build 
cd build
cmake ..
make
sudo make install

The pkg-config is also a useful utility, since it gives the compilation and linking flags useful with some packages.

Large software may be difficult to build because of the dependency hell (which is an issue for every large software, on any operating system). Linux distributions are done to manage with it.

I guess that these free software are often easier to build on Linux than on Windows (which I don't know and never use). BTW, I strongly recommend you to install Linux and learn it, because it is fun and made of free software (whose source code you can study, improve, and contribute to).

Very large or foundational free software like the GCC compiler, the GNU glibc, the Linux kernel are tricky to build (because nearly everything depend upon them). So start building some easier free software from source code. See sourceforge or github to find some.

Some languages or frameworks have their own installer or package manager or builder. For example, ocaml has opam

BTW, notice that all IDE (including Visual Studio on Windows) are just editors with some graphical interface to run the C++ compiler on the command line. So it is better to understand what is really happening, by knowing how to run the compiler thru commands.

2
  • Can you recommend any good resources to learn this? Have you come across any resource that walks you through the challenges of direct compilation to make you realize the importance of make tools or cmake tool. I am new to this. I have written only programs that can fit in one file or rather I never tried moving them to different file. Commented Mar 9, 2020 at 12:26
  • 1
    Just try to compile any free software from source code. Linux is mostly made of them. A simple one might be GNU make or GNU emacs Commented Mar 9, 2020 at 12:47
0

My experience is that beyond having a Makefile (or sometimes cmake :-(), it varies according to the personal tastes of the individual. Personally, I use a editor that runs from the command line (xterm), and have editor macros (created over a couple of decades) that more or less imitate many of the features of an IDE without the baggage. For instance, one keypress runs make (after saving all open files), dumps the output into a file which I see in the editor, then another keypress on an error line takes me to the originating line in the source. (And this works for other things, like LaTeX documents.) Other people like to use vi(m), emacs, or other tools.

The common core is that almost everything I've seen builds from a Makefile. If the project is intended for wide distribution, that may be generated by autoconf tools. (Or cmake &c.) However, if you like IDEs, Wikipedia has a long list that run in Linux: https://en.wikipedia.org/wiki/Category:Linux_integrated_development_environments I've no experience with any of them, other than a short (and unpleasant) stint with Eclipse - and that some years ago, so it may well have improved.