CMAKE: Build library and link against it

Well, it is better to read this example and do exactly as suggested.

cmake_minimum_required (VERSION 2.6)
project (MyProj CXX)
add_subdirectory(MyLib)
add_subdirectory(MyApp)

Then for each subdirectory specified, CMakeLists.txt files are created

MyLib\CMakeLists.txt

file(GLOB SRC_FILES *.cpp)
add_library(MyLib ${SRC_FILES})

MyApp\CMakeLists.txt

file(GLOB SRC_FILES *.cpp)
add_executable(MyApp ${SRC_FILES})
target_link_libraries(MyApp MyLib) 

  1. Use "out of the source build". Make a directory used only for build and while in it, call

    cmake <path to the sources, it may be relative>
  2. Either use

    link_directories(${MyProj_BINARY_DIR}/MyLib)

    or make CMakeLists.txt in each subdirectory - that would be better for project larger than very small.

  3. This is a bit tricky, check out CMAKE_BUILD_TYPE in the docs (you can set it and/or "if" by it). You can also set it from command line:

    cmake -DCMAKE_BUILD_TYPE=Debug

Tags:

Makefile

Cmake