为什么cmake还为导入的包添加编译选项?
why does cmake also adds compile options for imported packages?
我有一个项目需要一些依赖项,我使用 vcpkg 来管理它们。现在我想用 add_compile_options( /W4 /WX /std:c++17 )
为我的可执行文件和子目录设置编译选项,但我不希望它们应用于用 find_package
.
加载的包
这是我的 CMakeLists.txt
中来自我的测试存储库的代码:
cmake_minimum_required(VERSION 3.15)
project(test)
set(VCPKG_DEPENDENCIES glm)
# set the build triplet for windows because default is x86-windows
if(WIN32)
set(VCPKG_TARGET_TRIPLET "x64-windows")
add_compile_options( /W4 /WX /std:c++17 )
endif()
get_filename_component(ABS_PATH_VCPKG "./vcpkg" REALPATH)
set(VCPKG_ROOT ${ABS_PATH_VCPKG})
set(CMAKE_TOOLCHAIN_FILE "${ABS_PATH_VCPKG}/scripts/buildsystems/vcpkg.cmake")
# additional folder where find_XXXX functions are searching for packages
set(CMAKE_PREFIX_PATH "${VCPKG_ROOT}/installed/${VCPKG_TARGET_TRIPLET}/share")
foreach(DEPENDENCY ${VCPKG_DEPENDENCIES})
message(STATUS "installing vcpkg dependency: <${DEPENDENCY}>")
execute_process(COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/vcpkg.exe install ${DEPENDENCY}:${VCPKG_TARGET_TRIPLET} OUTPUT_FILE ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg_install_log.txt)
endforeach()
add_executable(test main.cpp)
find_package(glm CONFIG REQUIRED)
target_link_libraries(test PRIVATE glm)
回答这是我的主要问题:
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
int main()
{
glm::vec3 vec(1.0);
std::cout << ("Hello World!");
return 0;
}
然而,glm 包正在使用 IMPORTED
键调用 add_library
,这应该告知该包是一个系统包,不应获得应用的选项。
这是错误代码:
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build <path>/cpp_test_Isystem/build --config Debug --target all -- -j 18
[build] [1/2 50% :: 0.752] Building CXX object CMakeFiles\test.dir\main.cpp.obj
[build] FAILED: CMakeFiles/test.dir/main.cpp.obj
[build] <path>\VC\Tools\MSVC23~1.281\bin\Hostx64\x64\cl.exe /nologo /TP -I..\vcpkg\installed\x64-windows\include /DWIN32 /D_WINDOWS /GR /EHsc /Zi /Ob0 /Od /RTC1 -MDd /W4 /WX /std:c++17 /showIncludes /FoCMakeFiles\test.dir\main.cpp.obj /FdCMakeFiles\test.dir\ /FS -c ..\main.cpp
[build] <path>\cpp_test_Isystem\vcpkg\installed\x64-windows\include\glm\ext\../detail/type_quat.hpp(58): error C2220: the following warning is treated as an error
[build] <path>\cpp_test_Isystem\vcpkg\installed\x64-windows\include\glm\ext\../detail/type_quat.hpp(137): note: see reference to class template instantiation 'glm::qua<T,Q>' being compiled
[build] <path>\cpp_test_Isystem\vcpkg\installed\x64-windows\include\glm\ext\../detail/type_quat.hpp(58): warning C4201: nonstandard extension used: nameless struct/union
[build] ninja: build stopped: subcommand failed.
[build] Build finished with exit code 1
您的 main.cpp 文件包含此 header,因此适用于它的任何选项也适用于 header。
您的选择:
- 在 GLM headers:
的 #include
附近使用 #pragma
禁用 C4201 警告
#pragma warning( push )
#pragma warning( disable : 4201 )
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#pragma warning( pop )
- 禁用来自 CMake 的警告:
target_add_compile_options(test /wd4201)
- 编辑:使用 GLM-specific 定义来消除这些警告 (source):
#define GLM_FORCE_SILENT_WARNINGS
(或等价于 target_add_definitions
)
我有一个项目需要一些依赖项,我使用 vcpkg 来管理它们。现在我想用 add_compile_options( /W4 /WX /std:c++17 )
为我的可执行文件和子目录设置编译选项,但我不希望它们应用于用 find_package
.
这是我的 CMakeLists.txt
中来自我的测试存储库的代码:
cmake_minimum_required(VERSION 3.15)
project(test)
set(VCPKG_DEPENDENCIES glm)
# set the build triplet for windows because default is x86-windows
if(WIN32)
set(VCPKG_TARGET_TRIPLET "x64-windows")
add_compile_options( /W4 /WX /std:c++17 )
endif()
get_filename_component(ABS_PATH_VCPKG "./vcpkg" REALPATH)
set(VCPKG_ROOT ${ABS_PATH_VCPKG})
set(CMAKE_TOOLCHAIN_FILE "${ABS_PATH_VCPKG}/scripts/buildsystems/vcpkg.cmake")
# additional folder where find_XXXX functions are searching for packages
set(CMAKE_PREFIX_PATH "${VCPKG_ROOT}/installed/${VCPKG_TARGET_TRIPLET}/share")
foreach(DEPENDENCY ${VCPKG_DEPENDENCIES})
message(STATUS "installing vcpkg dependency: <${DEPENDENCY}>")
execute_process(COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/vcpkg.exe install ${DEPENDENCY}:${VCPKG_TARGET_TRIPLET} OUTPUT_FILE ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg_install_log.txt)
endforeach()
add_executable(test main.cpp)
find_package(glm CONFIG REQUIRED)
target_link_libraries(test PRIVATE glm)
回答这是我的主要问题:
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
int main()
{
glm::vec3 vec(1.0);
std::cout << ("Hello World!");
return 0;
}
然而,glm 包正在使用 IMPORTED
键调用 add_library
,这应该告知该包是一个系统包,不应获得应用的选项。
这是错误代码:
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build <path>/cpp_test_Isystem/build --config Debug --target all -- -j 18
[build] [1/2 50% :: 0.752] Building CXX object CMakeFiles\test.dir\main.cpp.obj
[build] FAILED: CMakeFiles/test.dir/main.cpp.obj
[build] <path>\VC\Tools\MSVC23~1.281\bin\Hostx64\x64\cl.exe /nologo /TP -I..\vcpkg\installed\x64-windows\include /DWIN32 /D_WINDOWS /GR /EHsc /Zi /Ob0 /Od /RTC1 -MDd /W4 /WX /std:c++17 /showIncludes /FoCMakeFiles\test.dir\main.cpp.obj /FdCMakeFiles\test.dir\ /FS -c ..\main.cpp
[build] <path>\cpp_test_Isystem\vcpkg\installed\x64-windows\include\glm\ext\../detail/type_quat.hpp(58): error C2220: the following warning is treated as an error
[build] <path>\cpp_test_Isystem\vcpkg\installed\x64-windows\include\glm\ext\../detail/type_quat.hpp(137): note: see reference to class template instantiation 'glm::qua<T,Q>' being compiled
[build] <path>\cpp_test_Isystem\vcpkg\installed\x64-windows\include\glm\ext\../detail/type_quat.hpp(58): warning C4201: nonstandard extension used: nameless struct/union
[build] ninja: build stopped: subcommand failed.
[build] Build finished with exit code 1
您的 main.cpp 文件包含此 header,因此适用于它的任何选项也适用于 header。
您的选择:
- 在 GLM headers: 的
#include
附近使用 #pragma
禁用 C4201 警告
#pragma warning( push )
#pragma warning( disable : 4201 )
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#pragma warning( pop )
- 禁用来自 CMake 的警告:
target_add_compile_options(test /wd4201)
- 编辑:使用 GLM-specific 定义来消除这些警告 (source):
#define GLM_FORCE_SILENT_WARNINGS
(或等价于 target_add_definitions
)