130

I am trying to use CMake in order to compile opencv.

I am reading the tutorial but can't understand what is CMakeLists files and how is it connected to the gui of CMake?

Also couldn't understand what are makefiles, are they the same is CMakeLists?

And which file is it which I in the end open with visual-studio?

sashoalm
  • 75,001
  • 122
  • 434
  • 781
lital maatuk
  • 5,921
  • 20
  • 57
  • 79

6 Answers6

204

I don't know about Windows (never used it), but on a Linux system you just have to create a build directory (in the top source directory)

mkdir build-dir

go inside it

cd build-dir

then run cmake and point to the parent directory

cmake ..

and finally run make

make

Notice that make and cmake are different programs. cmake is a Makefile generator, and the make utility is governed by a Makefile textual file. See cmake & make wikipedia pages.

NB: On Windows, cmake might operate so could need to be used differently. You'll need to read the documentation (like I did for Linux)

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 6
    Great answer; I couldn't figure out how to get the build files in a sub-directory. I don't understand why the official documentation doesn't mention this. – TheBigB Jan 19 '15 at 09:45
  • This works under Linux, but on Windows it breaks on detecting the C/C++ compiler. Any ideas how to do that? – bartlomiej.n Nov 21 '18 at 19:07
  • 1
    It depends upon how you *particular* computer is set up. Check that your [`PATH` variable](https://en.wikipedia.org/wiki/PATH_(variable)) is correct. BTW, I *never* used Windows in my life (and wrote my first program in 1974) – Basile Starynkevitch Nov 21 '18 at 19:39
  • 3
    So many upvotes and the answer says nothing about Visual Studio. – Abyx Jan 24 '19 at 12:50
  • 2
    @byx: AFAIK, Visual Studio is a proprietary C++ compiler (not the same as Visual Studio Code, which is a source code editor) that works on Windows only. My answer is for Linux – Basile Starynkevitch Jan 25 '19 at 13:55
40

CMake takes a CMakeList file, and outputs it to a platform-specific build format, e.g. a Makefile, Visual Studio, etc.

You run CMake on the CMakeList first. If you're on Visual Studio, you can then load the output project/solution.

holtavolt
  • 4,378
  • 1
  • 26
  • 40
  • 2
    What is the Makefile? Where do I find the project/solution file for visualstudio? – lital maatuk Oct 22 '11 at 13:00
  • 3
    After you run cmake (or CMakeSetup and hit generate), you should be able to find the Makefile (if unix) or project files somewhere in the project tree. The location can be specified in the CMakeList. Here's a short presentation I found on the CMake wiki you might find helpful: http://www.elpauer.org/stuff/learning_cmake.pdf – holtavolt Oct 22 '11 at 13:06
  • 2
    CMake configures the entire project using a build format of your choice, meaning it sets everything up so that you can use, for example, Visual Studio to compile. By default (at least on linux), it makes a project that uses Make instead. Make is used to actually build the project, not configure or set it up. So first you'd have to use CMake to set up the project, then run Make to compile and build stuff. – leinaD_natipaC Sep 18 '14 at 15:54
  • What do you mean by "takes a CMakeList file"? How do I tell to take it? Where in the CMake UI can I select this file? There is no File/Open menu or something. Instead it asks "where is the source code" - and I can't answer that. I hoped that that information is in the CMakeList file. – Thomas Weller Jul 07 '22 at 10:33
12

Yes, cmake and make are different programs. cmake is (on Linux) a Makefile generator (and Makefile-s are the files driving the make utility). There are other Makefile generators (in particular configure and autoconf etc...). And you can find other build automation programs (e.g. ninja).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • I think this is a bit confusing. cmake is a build system generator, and _one_ of the things it can generate are makefiles (as in https://www.gnu.org/software/make/). There many other systems that it can generate, though, and they're not "Makefile generators". On macos (and probably everywhere else), if you run cmake --help, it'll give you a long list of the generators it has. For example, cmake can generate Xcode projects - nothing to do with make. – James Moore Nov 29 '17 at 23:18
8

CMake (Cross platform make) is a build system generator. It doesn't build your source, instead, generates what a build system needs: the build scripts. Doing so you don't need to write or maintain platform specific build files. CMake uses relatively high level CMake language which usually written in CMakeLists.txt files. Your general workflow when consuming third party libraries usually boils down the following commands:

cmake -S thelibrary -B build
cmake --build build
cmake --install build

The first line known as configuration step, this generates the build files on your system. -S(ource) is the library source, and -B(uild) folder. CMake falls back to generate build according to your system. it will be MSBuild on Windows, GNU Makefiles on Linux. You can specify the build using -G(enerator) paramater, like:

cmake -G Ninja -S libSource -B build

end of the this step, generates build scripts, like Makefile, *.sln files etc. on build directory.

The second line invokes the actual build command, it's like invoking make on the build folder.

The third line install the library. If you're on Windows, you can quickly open generated project by, cmake --open build.

Now you can use the installed library on your project with configured by CMake, writing your own CMakeLists.txt file. To do so, you'll need to create a your target and find the package you installed using find_package command, which will export the library target names, and link them against your own target.

7

Cmake from Windows terminal:

mkdir build
cd build/
cmake ..
cmake --build . --config Release
./Release/main.exe
Burak
  • 2,251
  • 1
  • 16
  • 33
  • If you want to have include files in a separate `installdir` subdirectory inside `build` directory, you can do: `cmake -DCMAKE_INSTALL_PREFIX=./installdir ..` – Megidd Feb 11 '21 at 04:58
0

Regarding CMake 3.13.3, platform Windows, and IDE Visual Studio 2017, I suggest this guide. In brief I suggest:
1. Download cmake > unzip it > execute it.
2. As example download GLFW > unzip it > create inside folder Build.
3. In cmake Browse "Source" > Browse "Build" > Configure and Generate.
4. In Visual Studio 2017 Build your Solution.
5. Get the binaries.
Regards.