I’ve try to build a small Visual Studio project with CMake, before i will explain the basic principles of CMake itself.
Below you can see one of the simplest cmake files you can write.
cmake_minimum_required (VERSION 2.8.11) project (MySolutionName) # MySolutionName could be anything, it's not really important add_library (HelloWorldLibrary hello.cpp) # HelloWorldLibrary is the name of our dll-project add_executable (HelloWorldApp main.cpp) # HelloWorldApp is the name of our exe project target_link_libraries (helloDemo PUBLIC Hello) # now we have to link our executable (HelloWorldApp) with our libraray (HelloWorldLibrary)
|cmake_minimum_required||yes||This command define the minimum version of CMake which you want to use. If’ve recommend CMake 3.x|
|project||no||this is (in visual studio context) the name of the solution|
|add_library||no||This command create a target call HelloWorldLibrary, which is a static lib/dll project with the same name in our solution|
|add_executable||no||This command create a target called HelloWorldApp which is a exe project in our solution (you get it, with the same name 🙂|
|target_link_libraries||no||Here we link our HelloWorldApp with our HelloWorldLibrary, because we want to use some function from the library in our exe|
So you can use this snippet, copy it and paste it into a file which you should name CMakeLists.txt.
- Create a directory
- Create a file named CMakeLists.txt
- Copy/Paste the code from above
- Add the missing code files (main.cpp, etc), all in the same directory
- Now you have 2 options to create your visual studio project in Windows (i prefer the console approach)
- Use the CMake Gui application Open the app, choose the source-directory and binary directory. Click on generate an choose the compiler (Visual Studio) version which is installed on your system. Be aware that that you have to choose between win64 and win32. In my example i choose the so call generator Visual Studio 10 2010 Win64. Now you will see an console output in the bottom section of the window. If something went wrong, please read the error messages in the console output section. If everything is fine, you can navigate (f.e. with the Windows explorer) to your binary directory and open your new Solution file, named after your project name, in my example MySolutionName.sln. Then you can compile it normally with Visual Studio
- Use the command prompt/console
Here we need the same paramters as in the Gui approach, but in a console fashion.
You should cd into your source directory and use the following command line parameters
cmake -B<path to binary directory> -G “Visual Studio 10 2010 Win64”If you dont want to cd into your source-directory you could also use the paramter: -H
|Source directory||the directory where your CMakeLists.txt lives|
|Binary directory||the directory where your binaries, visual studio project files and some additional files will go.These files are temporary and should not be checked in to your repository!|
What is a target?
If you are starting with CMake you relatively often use a target, but what the heck is a target?
If you are coming from Visual Studio, a target is a Visual Studio project (*.vcxproject), so if your target is named MyAwesomeApp, your Visual Studio project will also be named MyAwesomeApp. The target concept is based on the principle of makefile targets (as many other things in CMake). A target is generally an entry point, something like a void main() function for your application, but you can have as many as you want.
The target is also the only type in cmake which not act like a string, but i will explain the in a later post.