Pintu
Pintu

Reputation: 369

CMake Setting up Release and Debug version and Flags

I am new to CMake and I am trying to get my project compiling. The project creates a few static libraries and a few executables.

Below is the example of the file structure that I have.

PROJECT

Main CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
SET(CMAKE_CXX_COMPILER "g++")
Project(ort)

#set the source and header directories location
set(ORT_HEADER_DIRECTORY "../../include") #include folder structure explained above
set(ORT_SOURCE_DIRECTORY "../../src")
set(ORT_BINARY_DIRECTORY "../../lib")  # lib folder to contain all the libraries

set (CMAKE_CURRENT_BINARY_DIR ".")

#Include the library packages
include_directories("/usr/include/wx-2.8")
include_directories("/usr/local/cuda/include") and so on


#set the names of all the projects (for creating the libraries)
SET(PROJECT_NAMES "log" "data" "cc")

foreach(PROJECT_NAME ${PROJECT_NAMES})
     # Create the cmake related files in the out folder so that the libraries can be
     # copied to the lib folder
     add_subdirectory( "{ORT_SOURCE_DIRECTORY}/${PROJECT_NAME}" "${CMAKE_CURRENT_BINARY_DIR}/out/${PROJECT_NAME}"

endforeach(PROJECT_NAME ${PROJECT_NAMES})

#set the names of all the projects (for creating the libraries)
SET(EXECUATALE_PROJECTS "metadata" )

foreach(EXECUATALE_PROJECT ${EXECUATALE_PROJECTS})
     # Create the cmake related files in the out folder so that the libraries can be
     # copied to the lib folder
     add_subdirectory( "{ORT_SOURCE_DIRECTORY}/${EXECUATALE_PROJECT}" "${CMAKE_CURRENT_BINARY_DIR}/out/${EXECUATALE_PROJECT}"

endforeach(EXECUATALE_PROJECT ${EXECUATALE_PROJECTS})

CMakeLists.txt file for log directory (the same logic I have used for cc and data projects)

SET (CMAKE_CXX_FLAGS " -g -Wall -pThread")
include_directories(${ORT_HEADER_DIRECTORY})
SET(LOG_SOURCE a.cpp b.cpp c.cpp)
ADD_LIBRARY(log_d ${LOG_SOURCE})
target_link)libraries(log_d cc_d data_d)

metadata CMakeLists.txt file (creating the executable project)

SET (CMAKE_CXX_FLAGS " -g -Wall -pThread")
FIND_PACKAGE(wxWidgets)
IF(wxWidgets_FOUND)
        INCLUDE(${wxWidgets_USE_FILE})
ENDIF(wxWidgets_FOUND)

Include_Directories(${wxWidgets_INCLUDE_DIRS})
include_directories(${ORT_HEADER_DIRECTORY})
include_directories("/usr/ort/lib/unixODBC/include")

SET(META_SOURCE meta.cpp data.cpp)

ADD_EXECUTABLE(meta_d ${META_SOURCE })
TARGET_LINK_LIBRARIES(meta_d log_d data_d)

Currently with this piece I can successfully generate the required libraries in the build/linux/out folder. I did create the release and debug folder and did try to build the same. The files are created and build in the respective RElease or Debug folder. cd Release

cmake -DCMAKE_BUILD_TYPE=Release ..

make

Question: 1) Since, the project can be build both at the build/linux level as well as build/linux/Release build/linux/Debug level, Is there a way by which the project can be compiled only at the build/linux level based on the release or debug option provided(In addition, the files are to placed in the debug or release folder based on a the option specified).

i.e. I would like to do cmake -DCMAKE_BUILD=Release . on the build/linux level and not the release debug level.

I did try to run with the below option at the build/linux level

cmake -DCMAKE_BUILD_TYPE=Debug

but I got an error message Unknown argument specified. Can you please let me know how can I set up this configuration so that later on I can integrate the same to Eclipse

2) I would like to set the compilation flags for each project based on the debug and version specified, how can I do so ?

Upvotes: 2

Views: 6884

Answers (1)

aled
aled

Reputation: 25664

CMakeLists.txt belong to the source directory, not the build directory. Before you execute cmake in the build directory the first time it should be empty. You should not link directly with pthread but use the following method:

find_package( Threads )
target_link_libraries( proyectA lib1 lib2 ... ${CMAKE_THREAD_LIBS_INIT})

1) As far as I know you can use any directory but there can be only one type of build in each directory/cmake execution.

You should be aware that using CMAKE_BUILD_TYPE=Debug automatically adds debug flags to the build, so the '-g' parameter is redundant.

Also you probably should use set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") to not unset the default parameters that cmake uses for gcc.

2) Cmake has a variable with the default parameters for each build type. You can add your own to each. For example something like this may do the trick:

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall") 

See this post for more details.

Upvotes: 2

Related Questions