CMake相当于Visual Studio的属性表(.vsprops)

mad*_*uri 5 c++ cmake vsprops visual-studio clion

我正在尝试从Visual Studio迁移到Jetbrains(非常棒)的CLion IDE,它使用CMake来组织项目.

到目前为止,过渡是顺利的:创建CMake项目并将它们导入CLion很容易,我可以开始在一个平台上编码然后继续在另一个平台上没有问题.

但是,Visual Studio中的一个方面,我找不到一个相当于CMake的是属性表:我使用它们主要用于保持包括目录路径和图书馆链接库(即一个.vsprops文件,每个库,例如OpenCV.vsprops,Boost.vsprops,等等.).

这样,在VS中,我可以.vsprops在不同项目之间共享库文件,而无需每次都配置路径/库.

CMake是否有与Visual Studio属性表类似的机制?如何将库的includes/lib存储在CMake可解析文件中,然后在CMakeLists.txt中"导入"它以便链接到库?

基本上,我想要做的是:

  1. 为给定的库创建"cmake属性表"(缺少更好的名称).
  2. 然后,在CMakeLists.txt中写下类似的内容link_target_to_libs(myTarget "path/to/propertySheet1" "path/to/propertySheet2" ...).

mad*_*uri 1

由于我真的想让库的包含/链接成为一行命令,并且就我对 CMake 的(基本)知识而言,我认为应该做出一些妥协——主要是在之间共享目标名称的变量CMakeLists.txt的“财产表”。所以这是我的解决方案......直到有人提出一个更简单/更干净的解决方案:

  1. CMake 属性是一个.cmake文件,
  2. 一个众所周知的变量名——TARGET -- 指定目标(即 的第一个参数add_executable()),
  3. 除了特定于库的命令之外,文件还包含对和 的.cmake调用target_include_directories(${TARGET} PRIVATE ${PATH_TO_INCLUDE_DIR})target_link_libraries(${TARGET} ${LIST_OF_LIBS})
  4. 为了使用/链接库,请include("path/to/.cmake")调用CMakeLists.txt.

我已经成功构建并执行了一个使用 X11 和 OpenCV 的简单程序,并包含以下文件:

x11.cmake

target_include_directories(${TARGET} PRIVATE "/usr/include/X11")
target_link_libraries(${TARGET} "/usr/lib/x86_64-linux-gnu/libX11.so")
Run Code Online (Sandbox Code Playgroud)

opencv.cmake

# OpenCV-specific stuff
set(OpenCV_DIR "/PATH/TO/OPENCV/INSTALL/DIR/share/OpenCV") # path to OpenCVConfig.cmake
find_package(OpenCV REQUIRED)
# include path
target_include_directories(${TARGET} PRIVATE ${OpenCV_INCLUDE_DIRS})
# linking libs
target_link_libraries(${TARGET} opencv_world opencv_ts)
Run Code Online (Sandbox Code Playgroud)

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.4)
project(hello_clion)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

## hello-clion ##############################
# make a new target name
set(TARGET hello-clion)

# find sources
file(GLOB_RECURSE SOURCE_FILES "src/*.cpp" "src/*.hpp")

# declare a target
add_executable(${TARGET} ${SOURCE_FILES})

# link the libraries (to the last-declared ${TARGET}, which should be the last-added executable)
include("x11.cmake")
include("opencv.cmake")
#############################################
Run Code Online (Sandbox Code Playgroud)

主程序

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <thread>

#include <opencv2/opencv.hpp>

#include <Xlib.h>

int main_x11()
{
    // adapted from: http://rosettacode.org/wiki/Window_creation/X11#Xlib
}

int main_ocv()
{
    // adapted from: http://docs.opencv.org/doc/tutorials/introduction/display_image/display_image.html#source-code
}

int main()
{
    using namespace std;

    thread tocv(main_ocv);
    thread tx11(main_x11);

    tocv.join();
    tx11.join();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在,每次我想在项目/程序中使用 OpenCV 时,我只需将include("opencv.cmake")相应的CMakeLists.txt.