简体   繁体   English

我想用 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?

My project contain unit testing file I want to excludes from the compilation process.我的项目包含我想从编译过程中排除的单元测试文件。 I want to modify the .vcxproj Visual Studio file with CMake.我想用 CMake 修改 .vcxproj Visual Studio 文件。 What could be the possible solutions of my issue ?我的问题有哪些可能的解决方案?

set_source_files_properties(BankTestCase_1.h PROPERTIES VS_DEPLOYEMENT_CONTENT 0)

set_source_files_properties(BankTestCase_1.h PROPERTIES EXCLUDE_FROM_BUILD 1)

First off, you don't modify a .vcxproj file.首先,您不要修改.vcxproj文件。 CMake generates one. CMake 生成一个。 Each time you invoke cmake, a new project file is generated.每次调用 cmake 时,都会生成一个新的项目文件。 You have to generate it correctly.您必须正确生成它。


CMake has already a function defined just to add tests. CMake 已经定义了一个函数来添加测试。 It's calledadd_test它叫做add_test

It work by specifying a name then a command or executable target:它通过指定名称然后指定命令或可执行目标来工作:

add_executable(my_test_exe testsrc1.cpp testsrc2.cpp)
add_test(NAME MyTest COMMAND my_test_exe)

If you also want to not compile the test files in your main project, simply don't add them to your main target:如果您也不想编译主项目中的测试文件,只需不要将它们添加到您的主目标中:

add_executable(main_project
    src1.cpp
    src2.cpp
    src3.cpp
    src4.cpp
    # srctest1.cpp # not added
    # srctest2.cpp # not added too.
)

Also, I notice that you want to remove header files from compilation.另外,我注意到您想从编译中删除头文件。 Header files are not supposed to be compiled at all.根本不应该编译头文件。 You should not add them to your target.您不应该将它们添加到您的目标中。


If you used globs to add all source files, then you can always remove it from the glob list:如果您使用 globs 添加所有源文件,那么您始终可以将其从 glob 列表中删除:

list(REMOVE_ITEM globbed_files file/to/be_excluded.cpp)

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

相关问题 CMake 文件,用于集成 Visual Studio 单元测试 - CMake file for integrated Visual Studio unit testing 无法使用 Visual Studio 生成 CMake 文件 - Can't generate CMake files with Visual Studio 让 CMake 为 Visual Studio 中的单个源文件设置“从构建中排除”选项 - Let CMake set the "Exclude From Build" option for a single source file in Visual Studio 使用Visual Studio Native cpp Unite Testing包含文件I / O的问题单元测试函数 - Problem unit testing functions that contain file I/O using Visual Studio Native cpp Unite Testing Visual Studio外接程序“从生成中排除”属性 - Visual Studio Addin “Exclude From Build” Property 使用CMake生成Visual Studio C ++项目文件 - Using CMake to generate Visual Studio C++ project files 需要Visual Studio 2005兼容的C ++单元测试框架 - Need Visual Studio 2005 Compatible C++ Unit Testing Framework 如何使用CMake生成针对Windows XP的Visual Studio 2012项目? - How can I generate a Visual Studio 2012 project targeting Windows XP with CMake? 如何获得 CMake 工具以使用 C++11(或更高版本)功能在 Visual Studio Code 中编译程序? - How do I get CMake Tools to compile programs in Visual Studio Code with C++11 (or higher) features? 在Visual Studio中进行正确的单元测试 - Proper unit testing in Visual Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM