Issue
When I clone a project with a configure
or even a Makefile
I know that I can do ./configure && make
or just simply make all && sudo make install
. But everytime I have a CMakelists.txt
it makes my journey harder...
Usually I have to start by creating a new directory, then go into this directory, then launch cmake
with an option I forgot perhaps -G
or -g
.. Then Makefiles
or LinuxMakefiles
or maybe Linux Makefile
:
mkdir build
cd build
cmake -g "Unix Makefiles" ..
make
Usually in 99% of the time I get an error such as ... file.h
No such file or directory. Today it is #include <allegro5/color.h>
that can be installed with allegro5
or perhaps allegro-dev
or... let's try liballegro5-dev
: sudo apt-get install liballegro5-dev
... Yeah it works.
In this example I wanted to show how cumbersome it is to build a project with a CmakeLists.txt
. Is there a way to make the experience more enjoyable : automatically list the dependencies, avoid creating a build folder, and remembering Unix Makefiles
?
Solution
Firstly, don't call the build system directly. Instead, use it through cmake so that it will work regardless of which build system was configured:
# not this
#make
# do this
cmake --build .
I forgot perhaps -G or -g.. Then Makefiles or LinuxMakefiles or maybe Linux Makefile
If you compile on Linux, then Unix Makefiles is the default. You can use the default simply by not specifying the argument:
cmake ..
When we use cmake to invoke the build system as in the first part of the answer, we don't even need to care which build system is being used.
If you want a custom default, you can use the environment variable CMAKE_GENERATOR
As for having multiple steps, although they tend to get stuck in one's brain through repeated use, there is no need to remember them off the top of your head. You can write them into a file, and that file can even be used as a shell script to execute them:
#!/bin/sh
mkdir --parents build
cd build
cmake ..
cmake --build .
Technically you can do inline build, but I would not recommend that because separate directory makes it much easier to keep the build files out of version control, and out of the way of separate builds with different configurations.
Answered By - eerorika