简体   繁体   English

让 CMake 为 Visual Studio 中的单个源文件设置“从构建中排除”选项

[英]Let CMake set the "Exclude From Build" option for a single source file in Visual Studio

In Visual Studio there is an "Exclude From Build" option in the properties page of each source file, that can be set to exclude the file from build, but keep it visible in the source tree:在 Visual Studio 中,每个源文件的属性页面中都有一个“从构建中排除”选项,可以将其设置为从构建中排除文件,但使其在源树中保持可见:

截图属性页

Is there a way to set that specific property with CMake?有没有办法用 CMake 设置该特定属性? I found a VS_DEPLOYMENT_CONTENT property and tried that but without success (it doesn't seem to do anything).我找到了一个VS_DEPLOYMENT_CONTENT属性并尝试过但没有成功(它似乎没有做任何事情)。

The reason for using that property is mainly to keep the file in the project to be able to open and edit it from within Visual Studio.使用该属性的原因主要是为了将文件保留在项目中,以便能够从 Visual Studio 中打开和编辑它。

Thanks in advance!提前致谢!

Unfortunately, I also can't find the answer how to set "Exclude From Build" for some files with cmake in the same project.不幸的是,我也找不到如何在同一项目中使用 cmake 为某些文件设置“从构建中排除”的答案。 For now, I have added all such files to the target created by add_custom_target .现在,我已将所有此类文件添加到由add_custom_target创建的目标中。

if(MSVC)
  add_custom_target(myproj.additional SOURCES ${otherHeaders} ${otherSources})
endif()

This will create an extra project in the solution.这将在解决方案中创建一个额外的项目。 The files in this project will not compile and I can still edit and search in them.该项目中的文件将无法编译,我仍然可以在其中进行编辑和搜索。

Another option is to set the HEADER_FILE_ONLY property for sources that should not be compile:另一种选择是为不应编译的源设置HEADER_FILE_ONLY属性:

if(MSVC)
  set_source_files_properties(${otherSources} PROPERTIES
    HEADER_FILE_ONLY TRUE
  )
endif()

In this case, the files stay in the project but without any marks - such files will not differ in appearance from those that are compiled在这种情况下,文件保留在项目中但没有任何标记 - 这些文件的外观与编译的文件没有区别

Since CMake 3.18 you can use VS_SETTINGS .从 CMake 3.18 开始,您可以使用VS_SETTINGS That allows you to set any VS Project setting you like.这允许您设置您喜欢的任何 VS 项目设置。

set_property(SOURCE ${SourceFiles} PROPERTY VS_SETTINGS "ExcludedFromBuild=true")

Note that you have to set the settings values with the name as it is written in the vcxproj file.请注意,您必须使用 vcxproj 文件中写入的名称来设置设置值。 To know what the setting is called you can set the setting to the correct value via the VS IDE, save the project and then open the vcxproj file in a text editor and search for the correct setting-value pair.要了解设置的名称,您可以通过 VS IDE 将设置设置为正确的值,保存项目,然后在文本编辑器中打开 vcxproj 文件并搜索正确的设置值对。 For example, in case of Excluded from Build:例如,在从构建中排除的情况下:

<FXCompile Include="C:\path\to\source\file\file.hlsl">
  <ExcludedFromBuild>true</ExcludedFromBuild>
</FXCompile>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从依赖项中排除头文件以在Visual Studio 2017中进行构建 - Exclude a header file from the dependencies for a build in Visual Studio 2017 C ++ Visual Studio 2010从项目/构建中排除文件 - C++ Visual Studio 2010 Exclude file from project/build 使用Visual Studio或cmake从源代码构建lib - Building lib from source with visual studio or cmake CMake根据配置从构建中排除文件 - CMake exclude file from build according to configuration Visual Studio外接程序“从生成中排除”属性 - Visual Studio Addin “Exclude From Build” Property CMake在Visual Studio 2017中将链接器选项设置为/ DEBUG:FULL - CMake set linker option to /DEBUG:FULL in Visual Studio 2017 我想用 CMake 生成和编译 Visual Studio projet 我需要从构建中排除单元测试文件。 有没有办法做到这一点? - I want to generate & compile Visual Studio projet with CMake I need to exclude unit testing files from the build. Is there a way to dothis? Visual Studio从源代码构建应用程序 - visual studio build application from the source code CMake和Visual Studio构建错误 - CMake And Visual Studio build errors CMake-从另一个cmake文件中设置的GUI中删除选项 - CMake - Removing an option from the GUI set in another cmake file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM