CMake设置发布和调试版本和标志 [英] CMake Setting up Release and Debug version and Flags

查看:535
本文介绍了CMake设置发布和调试版本和标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的CMake,我试图让我的项目编译。该项目创建了一些静态库和一些可执行文件。



下面是我有的文件结构的例子。



项目




  • build / linux


    1. CMakeLists.txt(主CMakelist文件)


  • build / linux / Release(应包含版本库和文件)


  • build / linux / Debug >


  • SRC


    1. subProject_1

      .cpp源文件)和CMakeLists.txt 1(创建静态库)

    2. subproject_2

      .cpp(所有源文件)和CMakeLists.txt 2 (创建静态库)

    3. subproject_3

      .cpp(所有源文件)和CMakeLists.txt 3(用于创建可执行文件)


  • 包含


    1. subProject_1

      .h(所有头文件)

    2. subProject_2

      .h(所有头文件)

    3. subProject_3

      .h(所有头文件)




< Main CMakeLists.txt

  cmake_minimum_required(VERSION 2.6)
SET(CMAKE_CXX_COMPILERg ++)
Project ort)

#设置源和目录位置
set(ORT_HEADER_DIRECTORY../../include)#include上面解释的文件夹结构
set(ORT_SOURCE_DIRECTORY。 ./../src)
set(ORT_BINARY_DIRECTORY../../lib)#lib文件夹以包含所有库

set(CMAKE_CURRENT_BINARY_DIR。)

#包括库包
include_directories(/ usr / include / wx-2.8)
include_directories(/ usr / local / cuda / include)等

$ b #set所有项目的名称(用于创建库)
SET(PROJECT_NAMESlogdatacc)

foreach (PROJECT_NAME $ {PROJECT_NAMES})
#在out文件夹中创建cmake相关文件,以便库可以
#复制到lib文件夹
add_subdirectory({ORT_SOURCE_DIRECTORY} / $ { PROJECT_NAME}$ {CMAKE_CURRENT_BINARY_DIR} / out / $ {PROJECT_NAME}

endforeach(PROJECT_NAME $ {PROJECT_NAMES})

设置所有项目的名称创建库)
SET(EXECUATALE_PROJECTSmetadata)

foreach(EXECUATALE_PROJECT $ {EXECUATALE_PROJECTS})
#在out文件夹中创建cmake相关文件, be
#复制到lib文件夹
add_subdirectory({ORT_SOURCE_DIRECTORY} / $ {EXECUATALE_PROJECT}$ {CMAKE_CURRENT_BINARY_DIR} / out / $ {EXECUATALE_PROJECT}

endforeach用于日志目录的CMakeLists.txt文件(与我使用的逻辑相同)。用于日志目录的CMakeLists.txt文件(用于日志文件的逻辑) cc和数据项目)

  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)

元数据CMakeLists.txt文件(创建可执行项目)

  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)

目前,我可以在build / linux / out文件夹中成功生成所需的库。我没有创建发行和调试文件夹,并尝试建立相同。将在相应的RElease或Debug文件夹中创建和构建文件。
cd版本



cmake -DCMAKE_BUILD_TYPE =发布..



make



问题:
1)因为,该项目可以在build / linux级别以及build / linux / Release build / linux / Debug级别上构建,有没有办法通过它,项目只能在build / linux级别基于提供的发布或调试选项进行编译(此外,文件将根据指定的选项放置在调试或发行文件夹中)。



我想做cmake -DCMAKE_BUILD =释放。在build / linux级别,而不是发布调试级别。



我尝试在build / linux级别运行下面的选项



cmake -DCMAKE_BUILD_TYPE = Debug



但我收到一条错误消息Unknown argument specified。你能否让我知道如何设置这个配置,以便以后我可以集成到Eclipse



2)我想设置编译标志为每个项目都基于调试和版本指定,我该如何做?

解决方案

CMakeLists.txt属于源目录,不是构建目录。在build目录中执行cmake之前,它第一次应该为空。
您不应该直接与pthread链接,但使用以下方法:

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

1)据我所知,使用任何目录,但在每个目录/ cmake执行中只能有一种类型的构建。



您应该注意使用CMAKE_BUILD_TYPE = Debug会自动向构建中添加调试标志,所以'-g'参数是多余的。



也许你应该使用set(CMAKE_CXX_FLAGS$ {CMAKE_CXX_FLAGS} -Wall)不设置默认参数cmake用于gcc。



2)Cmake有一个变量,每个构建类型都有默认参数。你可以添加自己的每个。
例如,这样的东西可能会做的诀窍:

  set(CMAKE_CXX_FLAGS_DEBUG$ {CMAKE_CXX_FLAGS_DEBUG}  -  Wall) 

查看此 post 了解详情。


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

    1. 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

    1. subProject_1
      .cpp (all source files) and CMakeLists.txt 1 for this folder (creating a static library)
    2. subproject_2
      .cpp (all source files) and CMakeLists.txt 2 for this folder (creating a static library)
    3. subproject_3
      .cpp (all source files) and CMakeLists.txt 3 for this folder (creating the executable)

  • Include

    1. subProject_1
      .h (all the header files)
    2. subProject_2
      .h (all the header files)
    3. subProject_3
      .h (all the header files)

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 ?

解决方案

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.

这篇关于CMake设置发布和调试版本和标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆