While CMake enjoys increasing interest, there are still plenty of libraries using native build systems like Unix Makefiles. Including external libraries using other build systems CMake will make sure to first build test before linking it to myapp. In target_link_libraries we tell CMake to link it to our executable. Now, we can use the library defined in CMakeLists.txt of libtest_project in myapp’s CMakeLists.txt: cmake_minimum_required(VERSION 2.8) project(myapp) add_subdirectory(libtest_project) add_executable(myapp main.c) target_link_libraries(myapp test) install(TARGETS myapp DESTINATION bin)Īdd_subdirectory makes the library test defined in libtest project available to the build. Let’s start by adding the library’s directory as a subdirectory to our myapp project. We can extend our executable from above by linking it to our libray libtest.a. Instead of a static library we can build a shared lib as well: add_library(test SHARED test.c) Linking libraries to executables with CMake We also include our public header file into the install step and tell cmake to put it into include. To build a library we use a similar script: cmake_minimum_required(VERSION 2.8) project(libtest_project) add_library(test STATIC test.c) install(TARGETS test DESTINATION lib) install(FILES test.h DESTINATION include)ĬMake will build the library as libtest.a and install it into lib folder of the install directory. This will output a readily configured Xcode project to build our app. If we wanted to use a different generator we pass it to cmake using the -G parameter: cmake. We can now run our binary from the install directory. To run the build script you can simply use the Makefile: make make install To define a custom install directory we simply pass it to cmake: cmake. This will generate build scripts using the default generator - on Linux/OSX this should be Makefiles.īy default cmake will install our build into the system directories. To start a build we create a new folder: mkdir _build cd _buildĪnd call cmake with the path to the project’s root (in this case the parent folder): cmake. BuildingĬMake supports out-of-source builds - so all our compiled code goes into a directory separate to the sources. Install tells cmake to install our binary into the bin directory of the install directory. Thats all we need to be able to build our app with any of the available generators.Īdd_executable defines our binary with all linked source files. cmake_minimum_required(VERSION 2.8) project(app_project) add_executable(myapp main.c) install(TARGETS myapp DESTINATION bin) We by creating a CMakeLists.txt file in the root of our project. Lets assume we have a simple app with a single. Its a work in progress and I will try to continuously improve it.ĬMake is a meta build tool that allows you to generate native build scripts for a range of platforms: So I wrote this short CMake introduction as a distilled version of what I found out after working through the docs and following stackoverflow questions. There are some useful tutorials linked on the CMake Wiki but most of them only cover very specific problems or are too basic. The CMake documentation is quite comprehensive but not suitable for a beginner. When trying to learn CMake I could not find any good introduction.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |