CMake Setting up Release and Debug version and Flags-Collection of common programming errors
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
-
build/linux
- CMakeLists.txt (Main CMakelist file)
-
build/linux/Release (Should contain the release libraries and files)
-
build/linux/Debug (Should contain the debug version of the files)
-
SRC
- subProject_1
.cpp (all source files) and CMakeLists.txt 1 for this folder (creating a static library) - subproject_2
.cpp (all source files) and CMakeLists.txt 2 for this folder (creating a static library) - subproject_3
.cpp (all source files) and CMakeLists.txt 3 for this folder (creating the executable)
- subProject_1
-
Include
- subProject_1
.h (all the header files) - subProject_2
.h (all the header files) - subProject_3
.h (all the header files)
- subProject_1
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 ?
Originally posted 2013-11-09 23:13:59.