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

CMake's equivalent to Visual Studio's Property Sheets (.vsprops)

本文关键字:vsprops 属性 相当于 Visual Studio CMake      更新时间:2023-10-16

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

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

但是,我在 CMake 中找不到等效的 Visual Studio 的一个方面是属性表:我主要使用它们来保存包含目录的路径和库的链接库(即每个库一个.vsprops文件,例如 OpenCV.vspropsBoost.vsprops等)。

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

CMake 是否有与 Visual Studio 的属性表类似的机制?如何将库的包含/库存储在 CMake-parsable 文件中,然后将其"导入"到 CMakeList 中.txt以便链接到库?

基本上,我想做的是:

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

在 CMake 中,库可以导出带有导入目标的包,其他构建系统使用 find_package 导入该包:

http://www.cmake.org/cmake/help/v3.1/manual/cmake-packages.7.html

http://www.cmake.org/cmake/help/v3.0/manual/cmake-buildsystem.7.html

http://www.cmake.org/cmake/help/v3.0/manual/cmake-buildsystem.7.html#imported-targets

不是"链接到属性表",而是链接到导入的目标。

target_link_libraries(myTarget Dep1::Dep1 Dep2::Dep2)
并非所有库都

创建导入的目标,也不是所有库都提供 cmake 配置文件包。在这些情况下(包括 OpenCV 和 Boost),CMake 提供了查找模块:

http://www.cmake.org/cmake/help/v3.0/manual/cmake-developer.7.html#find-modules

与find_package一起使用并链接到变量的内容。

由于我真的想将库的包含/链接设置为单行命令,并且就我对 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. 要使用/链接库,请在 CMakeLists.txt 中调用 include("path/to/.cmake")

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

x11.cmake

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

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)

CMakeList.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")
#############################################

主.cpp

#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;
}

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

这似乎很好用,但肯定有我没有发现的问题。 (我担心添加相同target_link_libraries的多个宏会导致"已经定义"的链接错误,但至少 g++ 5.1.0 处理多次被赋予相同的库名称而不会出错。

在根 CMakeLists.txt 中, add_subdirectory() 调用或 glob 之前,包括:

macro(USES_WX)
    include_directories(SYSTEM /usr/local/include/wx-3.0)
    include_directories(SYSTEM /usr/local/lib/wx/include/gtk3-unicode-3.0)
    link_directories(/usr/local/lib)
    add_definitions(-D_FILE_OFFSET_BITS=64 -DWXUSINGDLL -D__WXGTK__ -pthread)
    target_link_libraries(${TARGET} pthread wx_gtk3u_xrc-3.0 wx_gtk3u_html-3.0 wx_gtk3u_qa-3.0 wx_gtk3u_adv-3.0 wx_gtk3u_core-3.0 wx_baseu_xml-3.0 wx_baseu_net-3.0 wx_baseu-3.0)
endmacro()

(您可以使宏更花哨,例如检查CMAKE_BUILD_TYPE是"调试"还是"发布"以链接到适当的库,改变预处理器定义等。 见 http://www.cmake.org/cmake/help/v3.0/command/if.html)

并让你项目的CMakeList.txt像这样:

set(TARGET myProgramName)
add_executable(${TARGET} myProgramName.cpp)
USES_WX()

^^ 宏调用必须在 add_executable() 之后


如果需要多个目标支持,请将上面显示的根 CMakeLists.txt 部分中的行修改为:

    ...
    target_link_libraries(${ARGV0} pthread wx_gtk3u_xrc-3.0 ...)
    ...

并让你项目的CMakeLists.txt像这样(更少的行,但更多的错误机会):

add_executable(myProgramName myProgramName.cpp)
USES_WX(myProgramName)