CMake 相当于 Visual Studio 的属性表 (.vsprops)
CMake's equivalent to Visual Studio's Property Sheets (.vsprops)
我正在尝试从Visual Studio迁移到Jetbrains的(很棒的)CLion IDE,它使用CMake来组织项目。
到目前为止,过渡一直很顺利:创建 CMake 项目并将其导入 CLion 很容易,我可以在一个平台上开始编码,然后继续另一个平台,没有问题。
但是,我在 CMake 中找不到等效的 Visual Studio 的一个方面是属性表:我主要使用它们来保存包含目录的路径和库的链接库(即每个库一个.vsprops
文件,例如 OpenCV.vsprops
、Boost.vsprops
等)。
这样,在VS中,我可以在不同项目之间共享库的.vsprops
文件,而无需每次都配置路径/库。
CMake 是否有与 Visual Studio 的属性表类似的机制?如何将库的包含/库存储在 CMake-parsable 文件中,然后将其"导入"到 CMakeList 中.txt以便链接到库?
基本上,我想做的是:
- 为给定库创建"cmake 属性表"(因为缺少更好的名称)。
- 然后,在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
和"属性表"之间共享目标名称的变量。所以这是我的解决方案...直到有人提出一个更简单/更干净的:
-
CMake 属性表是一个
.cmake
文本文件, - 一个众所周知的变量名 -
TARGET
- 指定目标(即add_executable()
的第一个参数), - 除了特定于库的命令外,
.cmake
文件还包含对target_include_directories(${TARGET} PRIVATE ${PATH_TO_INCLUDE_DIR})
和target_link_libraries(${TARGET} ${LIST_OF_LIBS})
的调用, - 要使用/链接库,请在
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)
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- C++概念:如何使用'concept'检查模板化结构的属性?
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 通过指向指针数组的指针访问子类的属性
- MSVC是否支持C++11样式的属性而不是__declspec
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 类的C++属性似乎已重新初始化
- Qt Quick-如何仅从c++代码与qml属性交互
- 用概念检查属性的类型
- C++如何将同一类的实例作为属性
- 按多个属性排序
- 主函数参数的属性
- "perf_event_attr"结构的"read_format"属性的选项到底是什么?
- C++删除未使用的类属性会导致 std::logic_error
- 无法使用 SWIG 在 Python 中实例化C++类(获取属性错误)
- 使用内存地址访问结构的属性值
- C++调用具有 *this 属性的单个帮助程序函数
- C++ 在堆栈中包含多态属性的类对象存储
- C++ 命名参数习惯用语 - 未设置字符串属性
- CMake 相当于 Visual Studio 的属性表 (.vsprops)