leader

CMake getting started

Image Description
View Markdown

The cmake program uses a CMakeList.txt, similar to how make uses a Makefile. This post shows a few CMakefileList.txt examples.

Table of Contents

embedded

Online Resources

RTFM. Read the reference documentation, it's excellent. Many online blogs, including this one, have obsolete information. CMake is continually evolving. Use the docs that match your version of CMake.

How to Run CMake


cd to folder with CMakefileList.txt mkdir build ## or build or builds
cd build
cmake ..
make

Examples


cat ./src/CMakeLists.txt
cmakeminimum_required(VERSION 2.8)
add_executable(lued lued.c main.c)
target_link_libraries(lued carr lua m dl)

cat ./CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(lued_prj) 

SET(EXTERN_PATH ${CMAKE_BINARY_DIR}/extern)
SET(INSTALL_PATH ${CMAKE_BINARY_DIR}/install)
SET(EXE_PATH ${CMAKE_SOURCE_DIR}/lued_root)

include(ExternalProject)

ExternalProject_Add (prj_lua52
  BUILD_IN_SOURCE 1
  DOWNLOAD_NO_PROGRESS 1
  URL http://www.lua.org/ftp/lua-5.2.4.tar.gz
  PREFIX ${EXTERN_PATH}/prj_lua52
  CONFIGURE_COMMAND ""
  BUILD_COMMAND make linux
  INSTALL_COMMAND make install INSTALL_TOP=${INSTALL_PATH}
)

ExternalProject_Add (prj_carr
  GIT_REPOSITORY https://github.com/jwrr/carr.git
  PREFIX ${EXTERN_PATH}/prj_carr
  CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${INSTALL_PATH}
)

set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${EXE_PATH})
include_directories(${INSTALL_PATH}/include)
link_directories(${INSTALL_PATH}/lib)

add_subdirectory(src)


$ cat ./build/extern/prj_carr/src/prj_carr/src/CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(carr_prj)
add_library(carr STATIC carr_arg.c carr.c carr_readline.c carr_readline_lua.c lua_hacked.c)
INSTALL(TARGETS carr DESTINATION lib)
INSTALL(FILES carr_readline.h carr_esc.h carr_readline_lua.h carr.h carr_arg.h carr_cis.h DESTINATION include)


$ cat ./build/extern/prj_carr/src/prj_carr/CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(carr_prj)


SET

add_subdirectory(src)