40
votes

I'm new to programming so this is a more of a abstract question than a technical one. I've been using IDE's to learn but I heard they tend to oversimplify the act of compiling,assembling and linking when creating an executable file.

I'm trying to download and compile a library without relying on an IDE (in this case librocket). I first had to use Cmake to create the binaries. After configuring and generating, I didn't see any object files or .cpp files in the output directory. I then had to use the gnu make command which then created both object files and .cpp files in the output directory.

What steps from downloading the source code to actually creating the object files does Cmake and gnu make do? At what point is the compiler and linker actually called?

I've successfully compiled a test library I wrote and then compiled another program that linked to it using g++ but I'm kind of lost because I have to use Cmake and gnu make instead of calling a compiler directly.

2
You should be able to write a library and write a program using your library without using make or Cmake. They are very high-level helper tools.user755921

2 Answers

44
votes

Cmake is a "project generator". It doesn't actually build any object files etc.; what it does is generate the control files for other tools (such as GNU make) which will build object files, etc.

The advantage of using cmake instead of writing the makefile directly is that, using the same cmake input file, cmake can generate project files for all sorts of different build control tools: in addition to makefiles it can generate Xcode (Mac OS X) project files, Microsoft Visual Studio project files, and control files for other tools like Ninja. If you're writing software that needs to be built on lots of different platforms then cmake is often a good choice.

For your situation, it goes like this: cmake generates a set of makefiles (only). You typically only do this once when you have a clean workspace. Then you run make which uses those makefiles to invoke the compiler as needed, when various files change. The makefiles also have rules to re-run cmake itself if any of the cmake input files change.

10
votes

When compiling a large project with lots of files you can get lost if you call the compiler directly.

Of course you can put the related commands in a script and run it, but then you need to update the script if you add/remove a file. Also in that case you might want not to recompile everything every time you make a small change, so you want to only compile the parts that were changed. If you add this (and other quality of life) functionality in your hyphotetical script you are going to reinvent make.

make is designed to run only the compilation of the parts of your code that were changed till your last call to make. It also allows you to automate adding sourcefiles and etc.

However if you want your code to work with several different compilers, and on several different platforms, and to link to different libraries, and so on, things get a little complicated for make. You can still do it, but it is a lot of work. This is where cmake comes.

cmake builds makefiles (or similar build systems, example: Visual Studio) for the system you want. cmake has prebuilt profiles for different compilers and systems that will automatically find the right set of compiler flags, find an link to the correct libraries and so on.

Of course if you are writing a "Hello World!" or similar program you don't need neither make nor cmake. If you don't need to support multiple compilers/platforms you don't need cmake. If you are working on some huge library/application though make and cmake will help you do things easier.