cmake
来源:互联网 发布:出售淘宝买家信息 编辑:程序博客网 时间:2024/06/06 19:46
转载请注明出处!
转载于 g-var.com | G-VAR’s Blog
译文链接:CMake与Visual Studio
英文原文:CMake and Visual Studio
CMake
可以无缝地生成Visual Studio
的解决方案。这篇文章会将CMake
命令映射到Visual Studio IDE
上,为了更容易学习会附上一个例子。此篇文章主要倾向于使用Visual Studio
的C++
开发者。这里的大多数引用都是基于Visual Studio 2010
的,但应该也会同样适用于其他版本。大家可以从前言CMake for Visual Studio Developers开始阅读。注:这篇前言就不翻译了。
介绍
先简单的介绍一段关于CMake
理念的背景。通常源文件应该被视为可以由开发者显式修改的任何文件。另外,可以从源文件持续生成的任何东西都应被视为构建过程的输出。每次构建总是会生成大量的临时或中间文件,为了避免生成的文件会弄乱实际的源文件,建议在源文件之外进行构建。
除了像.obj
这样典型的中间文件,Visual Studio
还会生成更多的临时文件,包括.sln
、.sbr
、.suo
、.vcxproj.user
、.vcxproj.filters
。这些文件不需要(也不应该)被上传到源码库中,因为它们有的时候会非常大,并且这些文件还是用户特定的文件。
Visual Studio
的开发者可能会将工程文件.vcproj
视为”源”文件,因为它涉及到添加、删除文件以及修改文件的依赖等。然而,通过CMake
你可以将工程文件也视为中间文件,因为CMake
会自动生成它,并建议将其放置在源文件目录之外。
在CMake
的构建系统中,构建的”规则”或者是”项目的设置”等都会被定义在称之为CMakeLists.txt
的文本文件中。
Visual Studio到CMake的映射
Visual Studio
项目的一些常见的操作和设置,以及它们与CMake
的对应关系简要地罗列在下面的列表里。(注:表的内容就不翻译了)
例子
CMake Tutorial就是一个非常简单的例子。如果你对基本概念还不太清楚,请去该页面进行参考。这里的例子是个改进版本,用以展示常在Windows
下使用的某些具体内容。
示例代码可以通过GitHub获得。而对于那些对Github
不太熟悉的人,我将同样的代码打包成了7z
文件。鼠标右键点击tutorial-7z.jpg,另存到硬盘上,然后将其重命名为Tutorial.7z
,并将文件解压就可以获得源码了。
源码结构
CMake-VisualStudio-Example|---app| |---CMakeLists.txt| |---main.cxx|---math| |---advanced| | |---AdvancedFunctions.cxx| | |---AdvancedFunctions.h| |---simple| | |---SimpleFunctions.cxx| | |---SimpleFunctions.h| |----CMakeLists.txt| |---MathExports.h|---CMakeLists.txt|---TutorialConfig.h.in
执行CMake
如前所述,对于源外构建的建议,需要创建一个目录来存放由CMake
生成的必须的Visual Studio
项目文件和大量的CMake
的特定文件。
D:\tmp\CMake-VisualStudio-Example> mkdir _build
将这个构建目录作当前的工作目录。
D:\tmp\CMake-VisualStudio-Example> cd _build
执行CMake
命令,并指定第一次找到的(也就是根目录下)CMakeLists.txt
的路径。在这种情况下,使用”..“来指定当前工作目录的上一级目录。-G
选项可以指定CMake
生成Visual Studio 2010 Win64
的项目文件。请注意,也可以指定其他的生成器。
D:\tmp\CMake-VisualStudio-Example\_build> cmake .. -G "Visual Studio 10 Win64"
然后,Visual Studio
的工程文件就会被创建在_build
目录下。
Visual Studio的解决方案视图
在Visual Studio
中打开解决方案文件。
编译和运行
在Visual Studio
中编译解决方案。编译会生成二进制文件(app.exe、math.dll),这些文件分别位于_build
目录下的相应子目录下。所以现在这种情况下还不能运行可执行文件,因为会找不到math.dll
。
这也就是安装工程的作用了。单独编译INSTALL.vcproj
,因为它并不会作为解决方案的一部分而被编译。这个工程会将二进制文件拷贝到_build\bin
目录下。
现在可以在命令行窗口中运行可执行文件。
D:\tmp\CMake-VisualStudio-Example\_build\bin> app.exe 200The sum of 200 and 200 is 400The square root of 200 is 14.1421
测试
从构建目录调用CTest
就可以运行测试。另外,测试也可以在Visual Studio
下通过右键Build RUN_TESTS
工程来进行,并且测试的运行报告会被显示在输出窗口中。
D:\tmp\CMake-VisualStudio-Example\_build> ctestTest project D:/tmp/CMake-VisualStudio-Example/_build Start 1: AppTest11/3 Test #1: AppTest1 Passed 0.00 sec Start 2: AppTest22/3 Test #2: AppTest2 Passed 0.00 sec Start 3: AppTest33/3 Test #3: AppTest3 Passed 0.00 sec100% tests passed, 0 tests failed out of 3Total Test time (real) = 0.03 sec
CMakeLists.txt详解
文件中的每一行注释是关于CMake
命令详细解释。
File: CMake-VisualStudio-Example/CMakelist.txt
cmake_minimum_required (VERSION 2.6)# Maps to Visual Studio solution file (Tutorial.sln)# The solution will have all targets (exe, lib, dll) # as Visual Studio projects (.vcproj)project (Tutorial)# Turn on the ability to create folders to organize projects (.vcproj)# It creates "CMakePredefinedTargets" folder by default and adds CMake# defined projects like INSTALL.vcproj and ZERO_CHECK.vcprojset_property(GLOBAL PROPERTY USE_FOLDERS ON)# Set compiler flags and options. # Here it is setting the Visual Studio warning level to 4set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")# Command to output information to the console# Useful for displaying errors, warnings, and debuggingmessage ("cxx Flags: " ${CMAKE_CXX_FLAGS})# Sub-directories where more CMakeLists.txt existadd_subdirectory(app)add_subdirectory (math)# Turn on CMake testing capabilitiesenable_testing()# Add test casesadd_test(AppTest1 ${PROJECT_BINARY_DIR}/bin/app.exe 100)add_test(AppTest2 ${PROJECT_BINARY_DIR}/bin/app.exe 200)add_test(AppTest3 ${PROJECT_BINARY_DIR}/bin/app.exe 300)
File: CMake-VisualStudio-Example/math/CMakeLists.txt
# Collect sources into the variable MATH_SOURCES without# having to explicitly list each header and source file.## CMake documentation states "We do not recommend using GLOB to collect a# list of source files from your source tree. If no CMakeLists.txt file# changes when a source is added or removed then the generated build system# cannot know when to ask CMake to regenerate".file (GLOB MATH_SOURCES "*.h")# Collect sources into the variable SIMPLE_FUNCTION_SOURCESfile (GLOB SIMPLE_FUNCTION_SOURCES "simple/*.h", "simple/*.cxx")# The recommended way to collect sources in variable # ADVANCED_FUNCTION_SOURCES by explicitly specifying the source filesset (ADVANCED_FUNCTION_SOURCES "advanced/AdvancedFunctions.h" "advanced/AdvancedFunctions.cxx")# Create named folders for the sources within the .vcproj# Empty name lists them directly under the .vcprojsource_group("" FILES ${MATH_SOURCES})source_group("simple" FILES ${SIMPLE_FUNCTION_SOURCES})source_group("advanced" FILES ${ADVANCED_FUNCTION_SOURCES})# Properties->C/C++->General->Additional Include Directoriesinclude_directories (.)# Set Properties->General->Configuration Type to Dynamic Library(.dll)# Creates math.dll with the listed sources collected in the variables# Also adds sources to the Solution Exploreradd_library(math SHARED ${MATH_SOURCES} ${SIMPLE_FUNCTION_SOURCES} ${ADVANCED_FUNCTION_SOURCES})# Creates folder "libraries" and adds target project (math.vcproj)set_property(TARGET math PROPERTY FOLDER "libraries")# Adds logic to INSTALL.vcproj to copy math.dll to destination directoryinstall (TARGETS math RUNTIME DESTINATION ${PROJECT_BINARY_DIR}/bin)
File: CMake-VisualStudio-Example/app/CMakeLists.txt
# Properties->C/C++->General->Additional Include Directoriesinclude_directories ("${PROJECT_SOURCE_DIR}/Math")# Set Properties->General->Configuration Type to Application(.exe)# Creates app.exe with the listed sources (main.cxx)# Adds sources to the Solution Exploreradd_executable (app main.cxx)# Properties->Linker->Input->Additional Dependenciestarget_link_libraries (app math)# Creates a folder "executables" and adds target # project (app.vcproj) under itset_property(TARGET app PROPERTY FOLDER "executables")# Adds logic to INSTALL.vcproj to copy app.exe to destination directoryinstall (TARGETS app RUNTIME DESTINATION ${PROJECT_BINARY_DIR}/bin)
CMake的预定义工程
为了给Visual Studio
环境中添加一些CMake
的功能,CMake
创建了一些很大程度上被忽略了的工程文件。不管怎么样,下面给出了这些工程的简单描述。
Debug and Release Configurations
Visual Studio
支持在单个工程文件中具有多个配置,并且会给每个配置创建一个目录。CMake
默认会生成四种配置:Debug
、Release
、MinSizeRel
和RelWithDebInfo
。
可以访问下面的链接来查看如何修改这些默认值。
CMake Wiki: How can I specify my own configurations (for generators that allow it) ?
总结
CMake
是一个比Visual Studio
的工程和解决方案更好的构建系统。它结构紧凑,并且即使是针对仅适用于Windows
的项目,它也是更易于维护的。
希望这篇文章会对大家有用。如果您有任何疑惑或者其他信息可以随意评论。
[ 转载必须在正文中标注并保留原文链接等信息。]
- cmake
- cMAKE
- cmake
- CMake
- Cmake
- cmake
- cmake
- CMake
- cmake
- CMake
- CMake
- CMake
- cmake
- CMAKE
- cmake
- CMake
- CMake
- CMake
- ios 数据持久化之 归档
- angular2在双向数据绑定时[(ngModel)]无法使用的问题
- js发送SOAP请求
- js简单实现遮罩层
- 在 Ubuntu 系统安装搜狗输入法
- cmake
- 除了出行,优步还要试水“旅行”
- 3W纷享派 | 以书会友:与你共同深刻解读《增长黑客》
- 一个完美的逗号——赛创周年庆
- 优步与滴滴血战进入白热化 谁能决胜紫禁之巅?
- 中科院丁汉:制造技术的限制是先进技术无法实用化的重要原因
- 分享一波Unity3D CSharp 对车的第三人称视角摄像机脚本
- 每天一道LeetCode----位运算实现加减乘除四则运算
- RESTful Web Service 架构剖析