How to migrate this Make and Autotools project with multiple source subdirectories to CMake?

If there’s never any source higher than the lx/src directory, then there’s no need for the lx/CMakeLists.txt file. If there is, it should look something like this:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(lx)

add_subdirectory(src)
add_subdirectory(dir1)
add_subdirectory(dir2)

# And possibly other commands dealing with things
# directly in the "lx" directory

…where the subdirectories are added in library dependency order. Libraries that depend on nothing else should be added first, and then libraries that depend on those, and so on.

lx/src/CMakeLists.txt

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(lx_exe)

add_subdirectory(util)
add_subdirectory(js)
add_subdirectory(server)

set(lx_source_files conf.c lx.c)
add_executable(lx ${lx_source_files})

target_link_libraries(lx server)
  # also transitively gets the "js" and "util" dependencies

lx/src/util/CMakeLists.txt

set(util_source_files
  etc.c
  etc.h
  str.c
  str.h
)
add_library(util ${util_source_files})

lx/src/js/CMakeLists.txt

set(js_source_files
  interp.c
  interp.h
  js.c
  js.h
)
add_library(js ${js_source_files})

target_link_libraries(js util)

lx/src/server/CMakeLists.txt

set(server_source_files
  request.c
  request.h
  server.c
  server.h
)
add_library(server ${server_source_files})

target_link_libraries(server js)
  # also transitively gets the "util" dependency

Then, in a command prompt:

mkdir lx/bin
cd lx/bin

cmake ..
  # or "cmake ../src" if the top level
  # CMakeLists.txt is in lx/src

make

By default, the lx executable will end up in the “lx/bin/src” directory using this exact layout. You can control what directory it ends up in by using the RUNTIME_OUTPUT_DIRECTORY target property and the set_property command.

http://www.cmake.org/cmake/help/cmake-2-8-docs.html#prop_tgt:RUNTIME_OUTPUT_DIRECTORY

http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:set_property

Refer to target_link_libraries libs either by CMake target name, if the lib is built as a CMake target via add_library, or by full path to the library file otherwise.

See also, the output of “cmake –help-command target_link_libraries”, or any other cmake command, and the full online documentation for cmake commands found here:

http://www.cmake.org/cmake/help/cmake-2-8-docs.html#section_Commands

http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:target_link_libraries

Leave a Comment