45

I got the Box2D project source and want to compile the testbed portion of it. The project folder contains folders like: freeglu glui testbed(a demo) helloword(a demo) Box2D Build CMakeFiles

There are many CMakeLists.txt in all the different folders. I was thinking that I should cmake all those files so that make files are created in all places required. I read this (as instructions to do do want I want) :

wget http://box2d.googlecode.com/files/Box2D_v2.2.1.zip
unzip Box2D_v2.2.1.zip
cd Box2D_v2.2.1/Build
cmake ..
make

What does the cmake .. do? There is no CMakeLists.txt in the build folder.

batman
  • 5,022
  • 11
  • 52
  • 82
  • 1
    But there should be a CMakeLists.txt file in "Box2D_v2.2.1" folder. In "cmake ..", double dotted marks say CMakeLists.txt file exists in one directory upward in the directory hierarchy "Box2D_v2.2.1/Build/". – Pulathisi Bandara Jun 06 '16 at 06:07

3 Answers3

81

cmake is a Makefile generator.

When you call cmake [path], you ask it to generate a Makefile in the current directory following instructions given in [path]/CMakeLists.txt

Usually cmake output some messages while it is working, and after it is done without errors, you can type "make" to execute your newly created Makefile.

CMakeLists.txt files can reference other CMakeLists.txt file in sub-directories, so you are usually only interested by the CMakeLists.txt of the top directory, not the other ones.

Using an empty "build" directory is a technique called "out-of-source build", in which all your generated files (.o, executable, Makefile, .anything) are generated in the separate "build" directory and not mixed with source files. If you want to clean all, you can delete all the content of the build directory.

In fact, you can put your "build" directory in any place, as long as you give cmake the correct path of the top CMakeLists.txt. You can even have several build directories. It is very useful if you need several different builds at the same time (with different options, different versions of gcc, etc.)

In old programs, you generate the Makefile too, but using ./configure (this is called auto-tools. You may have encountered that already). cmake is considered a successor of the auto-tools.

Offirmo
  • 18,962
  • 12
  • 76
  • 97
  • 1
    Ah! Thanks a lot! That cleared my confusion. I had tried to run cmake in a file containing CMakeLists.txt by typing "cmake" 'without' file specification and I thought I saw a man page of cmake displayed. Maybe I hadn't tried it right.:) – batman Sep 02 '12 at 16:03
  • If I may extend this a little further, the actual commands were different from what I had thought. There was a `cmake -DBOX2D_INSTALL=ON -DBOX2D_BUILD_SHARED=ON` .. instead of just `cmake ..` Any idea of what that means? – batman Sep 02 '12 at 16:06
  • 1
    @learner yes. By adding options to the cmake invocation, you give it hints about how it should generate the Makefile, maybe activating some features or using a different mode. Most of the time, you can also change those options by typing "ccmake ." (mind the dot) in your build dir. You will then have a small interface listing Makefile generation options. – Offirmo Sep 02 '12 at 16:11
11

cmake .. generates makefiles in the current directory, using ../CMakeLists.txt file as starting point. make command, executed after this, builds the program, using generated makefile(s) as an input. This is convenient to keep a source code and build results in different folders. General syntax is: cmake source-dir (of course, there are a lot of other switches).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Alex F
  • 42,307
  • 41
  • 144
  • 212
6

Well, .. is shorthand for the parent folder, so it will presumably act upon whatever it finds in Box2D_v2.2.1.

Rook
  • 5,734
  • 3
  • 34
  • 43
  • @Alex Farber and Rook, when I tried cmake in the parent folder nothing happened. It gave a guide of some of sort of cmake. And is it necessary to go in 1 step deep and cmake the upper directory? – batman Sep 02 '12 at 14:22
  • The recommended method of using CMake is out of source which you did initially with the cmake .. in the Build folder. I would not try to defeat that. – drescherjm Sep 02 '12 at 14:35
  • 1
    The cmake wiki url has changed: https://gitlab.kitware.com/cmake/community/wikis/FAQ#out-of-source-build-trees – wisbucky May 28 '19 at 22:41