C++ 如何将 CMake 包含和库添加到 Visual Studio 解决方案?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8879269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:16:08  来源:igfitidea点击:

How to add CMake includes and libraries to Visual Studio Solution?

c++visual-studio-2010cmake

提问by Hellhound

I use CMake to generate a Visual Studio 2010 project and solution file. Actually I could set different settings, like warning level, incremental building flag ect. from CMake. But I can't set additional includes and libraries, listed in the VC++ Directory configuration tab. Actually I've to set up those directories manually. This is stupid and boring...

我使用 CMake 生成 Visual Studio 2010 项目和解决方案文件。其实我可以设置不同的设置,比如警告级别,增量构建标志等。来自 CMake。但我无法设置 VC++ 目录配置选项卡中列出的其他包含和库。实际上我必须手动设置这些目录。这是愚蠢和无聊...

I tried to set the following CMake variables: CMAKE_INCLUDE_PATH, INCLUDE_DIRECTORY but nothing happend. If i open the project, the additional include directory of the solution is always empty (only standard MSVE settings are given).

我尝试设置以下 CMake 变量:CMAKE_INCLUDE_PATH、INCLUDE_DIRECTORY 但什么也没发生。如果我打开项目,解决方案的附加包含目录始终为空(仅提供标准 MSVE 设置)。

I also tired to set this variables after executable creation, but this has also no effect.

我也厌倦了在创建可执行文件后设置这个变量,但这也没有效果。

This is what i do directly in the header of the cmake file:

这是我直接在 cmake 文件的标题中所做的:

CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(${MODULE_NAME})
IF (MSVC)
   # Activate C++ exception handling
   IF (NOT CMAKE_CXX_FLAGS MATCHES "/EHsc")
   SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc") 
   ENDIF ()

   # Set Warning level always to 4
   IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
     string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
   ELSE ()
     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
   ENDIF ()

   #read path of dependency modules  
   file(READ "msvc.deps" MSVC_PROPERTIES)
   STRING(REGEX REPLACE ";" "\\;" MSVC_PROPERTIES "${MSVC_PROPERTIES}")
   STRING(REGEX REPLACE "\n" ";" MSVC_PROPERTIES "${MSVC_PROPERTIES}")

   FOREACH(e ${MSVC_PROPERTIES})
     SET(INCLUDE ${INCLUDE} ${e})
     MESSAGE(STATUS "[INFO]: Value ${e}")
   ENDFOREACH(e)
   INCLUDE_DIRECTORIES(${INCLUDE})
ENDIF ()

In the .deps file I've added to path of my dependeny modules, line separated:

在 .deps 文件中,我添加到我的依赖模块的路径中,行分隔:

c:\binrev\development\boost.47\includes
c:\binrev\repository\modules\brCore\trunk\includes

Both are read successfully but couldn't be set as additional include directory in my MSVC solution.

两者都已成功读取,但无法在我的 MSVC 解决方案中设置为附加包含目录。

Best regards, Hellhound

最好的问候,地狱犬

回答by Adam Bowen

CMake is pretty well documented, if I have understood your question then the commands I think you're looking for are

CMake 有很好的文档记录,如果我理解了您的问题,那么我认为您正在寻找的命令是

Although some configuration is done by setting variables, most of it is done using commands to add certain information to parts of the build and slightly less frequently by setting properties on targets.

尽管一些配置是通过设置变量来完成的,但大部分是使用命令将某些信息添加到构建的各个部分来完成的,而通过在目标上设置属性的频率则稍低一些。

回答by wilx

I believe that include_directories ("path")somewhere in the CMakeLists.txt does add the pathto C++ includes path.

我相信include_directories ("path")CMakeLists.txt 中的某个地方确实添加了pathC++ 包含路径。

回答by Xu Cao

Yes. Although it does not appear in "Include Directories" under the "VC++ Directories" tab, it does show up in the "Additional Include Directories" under C/C++ -> General.

是的。尽管它没有出现在“VC++ 目录”选项卡下的“包含目录”中,但它确实出现在 C/C++ -> 常规下的“其他包含目录”中。