[CMake] Suggestions about my usage of CMake

Brad King brad.king at kitware.com
Tue May 30 10:34:14 EDT 2006


groton at gmx.net wrote:
> I would like to have suggestions/feedback about my usage of CMake for 
> MSVC2005. I have little project with an executable and a dynamic library.
> I would like to generate either the debug and the release version of the 
> dlls and exes files. So my CMakeLists.txt files looks like:

You should have a top-level CMakeLists.txt file that adds both directories:

PROJECT(MYPROJ)
ADD_SUBDIRECTORY(mylib)
ADD_SUBDIRECTORY(myexe)

Then the executable can directly reference the library target for 
linking.  See below.

> CMakeLists.txt  for the library:
> #########################################################
> PROJECT (LIBRARY)
> SET (PRJ_SRCS lib.cpp lib.h)
> ADD_DEFINITIONS (-DLIBRARY_EXPORTS)
> INCLUDE_DIRECTORIES (..)
> ADD_LIBRARY(LIBRARY ${PRJ_SRCS})
> SET_TARGET_PROPERTIES(LIBRARY PROPERTIES DEBUG_POSTFIX d)
> #ADD -DDEBUG TO THE DEBUG TARGET. THIS DOES NOT WORK! HOW TO ADD -DDEBUG 
> ONLY TO THE DEBUG
> #TARGET AND NOT TO ANY TARGET? I TRYED TO USE "COMPILE_FLAGS_DEBUG" WITH 
> NO LUCK.
> SET_TARGET_PROPERTIES (LIBRARY PROPERTIES COMPILE_FLAGS "-DDEBUG")
> #######################################

SET(CMAKE_CXX_FLAGS_DEBUG "-DDEBUG ${CMAKE_CXX_FLAGS_DEBUG}")

A per-configuration version of COMPILE_FLAGS is a good idea though. 
Please add a feature request here:

http://www.cmake.org/Bug

> CMakeLists.txt for the executable:
> ##############################################
> PROJECT(exe)
> SET (PRJ_SRCS exe.cpp )
> INCLUDE_DIRECTORIES (${CMAKE_CURRENT_SOURCE_DIR}/../LIBRARY/ )
> LINK_DIRECTORIES (${CMAKE_CURRENT_SOURCE_DIR}/../LIBRARY/)
> ADD_EXECUTABLE (exe WIN32 ${PRJ_SRCS})
> TARGET_LINK_LIBRARIES(exe debug kgld optimized kgl)
> ADD_DEFINITIONS (-D_CRT_SECURE_NO_DEPRECATE)
> ######################################

You can skip the LINK_DIRECTORIES command and change the 
TARGET_LINK_LIBRARIES line to just

TARGET_LINK_LIBRARIES(exe kgl)

if you use the top-level CMakeLists.txt file.

-Brad


More information about the CMake mailing list