Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Cmake, gnu make and manually compiling

Tags:

gnu-make

cmake

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.

like image 419
kristhedemented Avatar asked Aug 09 '14 18:08

kristhedemented


People also ask

What is difference between CMake and make?

Make (or rather a Makefile) is a buildsystem - it drives the compiler and other build tools to build your code. CMake is a generator of buildsystems. It can produce Makefiles, it can produce Ninja build files, it can produce KDEvelop or Xcode projects, it can produce Visual Studio solutions.

What is make in CMake?

Make is a cross platform build system originally for Unix like systems. CMake is a build system generator that can use any build system including make for the actual build. GUI Support.

What is the difference between gmake and make?

'gmake' refers specifically to GNU make. 'make' refers to the system's default make implementation; on most Linux distros this is GNU make, but on other unixes, it could refer to some other implementation of make, such as BSD make, or the make implementations of various commercial unixes.

Is CMake a GNU?

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.


2 Answers

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.

like image 159
MadScientist Avatar answered Oct 05 '22 01:10

MadScientist


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.

like image 25
Dimitar Slavchev Avatar answered Oct 05 '22 02:10

Dimitar Slavchev