![cmake add executable cmake add executable](https://wolfgang-ziegler.com/posts/2020/sdl-cmake-osx/vscode-cmake.png)
Linking CXX static library libchucknorris.a Building CXX object CMakeFiles/cpp_demo.dir/src/ Building CXX object CMakeFiles/chucknorris.dir/src/ CMake uses the current working directory as the build folder, and you must specify the path to the folder containing the CMakeLists.txt file as the last argument on the command line: $ mkdir -p build/defaultĪnd now we use ninja to build build and run our executable from the build folder: $ cd build/default So let’s create a folder named build/default and call CMake, asking it to use the Ninja generator.
#Cmake add executable how to
Now that we have described what we want to build and how, we still need to perform the build itself.ĬMake does not know how to actually perform the build. This will allow us to use modern C++ features (available in C++ 11 or later) such as the auto keyword or raw string literals later on.
![cmake add executable cmake add executable](https://i.stack.imgur.com/hC4jO.png)
You can read more about this in the CMake documentation.Īlso note how we set the CMAKE_CXX_STANDARD variable. If the headers were used only to compile the library, we would have used the PRIVATE keyword, and if they were used only by consumers of the library, we would have used the INTERFACE keyword instead. (We used the -I include/ flag both for building libchucknorris.o and ) The target_include_directories informs CMake that there are header files in the include directories that should be used both when builing the library itself, but also by consumers of the library. The add_library and add_executable commands describe the two targets (named chucknorris and cpp_demo) and the sources used to build them, and the target_link_libraries command tells CMake that cpp_demo depends on chucknorris. Cmake_minimum_required( VERSION 3.10) set( CMAKE_CXX_STANDARD 11) project( ChuckNorris) add_library( chucknorris include/ChuckNorris.hpp src/ChuckNorris.cpp ) target_include_directories(Ĭhucknorris PUBLIC "include" ) add_executable( cpp_demo src/main.cpp ) target_link_libraries( cpp_demo chucknorris)