gpt4 book ai didi

c++ - 无法使用 Visual Studio 和 CMake 将 __cplusplus 设置为 C++17 标准

转载 作者:太空宇宙 更新时间:2023-11-04 12:34:17 50 4
gpt4 key购买 nike

我有一个使用 Visual Studio 2019 打开的 CMake 项目。我的代码需要 c++17 功能,所以我在 CMakeLists.txt 中设置了相应的标志

cmake_minimum_required (VERSION 3.10.0)

project (datalog)

message (STATUS "Building project ${PROJECT_NAME}")

find_package(stxxl CONFIG REQUIRED)

include_directories (${CMAKE_SOURCE_DIR}/src)

set (PROJECT_SRC
main.cpp
)

add_executable (${PROJECT_NAME} ${PROJECT_SRC})
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)

target_link_libraries(${PROJECT_NAME} stxxl)

当我构建时,我遇到了很多错误,因为我正在链接的库 stxxlvcpkg 一起安装,具有以下代码:

STXXL_BEGIN_NAMESPACE

template <class Type>
struct compat_unique_ptr {
#if __cplusplus >= 201103L && ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40400)
typedef std::unique_ptr<Type> result;
#else
// auto_ptr is inherently broken and is deprecated by unique_ptr in c++0x
typedef std::auto_ptr<Type> result;
#endif
};

STXXL_END_NAMESPACE

问题是 __cplusplus 的值为 199711L 而不是正确的值 201703L ,因此代码尝试使用在 C++17 标准中删除的 auto_ptr

我还尝试在 Visual Studio 中手动设置标志,将此部分添加到 CmakeLists.txt

if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17 /Zc:__cplusplus")
endif()

但是什么都没有改变。我也在使用一些 C++17 特性,所以现在回头不是一个选择。

我可以手动更新 stxxl 头文件来摆脱宏,但这是一种解决方法,每个开发人员都应该创建相同的修复程序。相反,我想知道为什么会出现错误以及如何使用 Visual Studio 和 CMake 将宏设置为正确的值。你有什么建议吗?

最佳答案

据我所知 this ,您需要手动添加 /Zc:__cplusplus(至少现在)。

你的 CMakeList 代码片段对我有用,你确定 __cplusplus 实际上设置不正确而且它不仅仅是 __GNUC__ 宏,它丢失了吗?

也就是说,我建议测试 VisualStudio 的版本是否真的足够新:

# /Zc:__cplusplus is required to make __cplusplus accurate
# /Zc:__cplusplus is available starting with Visual Studio 2017 version 15.7
# (according to https://learn.microsoft.com/en-us/cpp/build/reference/zc-cplusplus)
# That version is equivalent to _MSC_VER==1914
# (according to https://learn.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=vs-2019)
# CMake's ${MSVC_VERSION} is equivalent to _MSC_VER
# (according to https://cmake.org/cmake/help/latest/variable/MSVC_VERSION.html#variable:MSVC_VERSION)
if ((MSVC) AND (MSVC_VERSION GREATER_EQUAL 1914))
target_compile_options(stxxl INTERFACE "/Zc:__cplusplus")
endif()

注意:我使用了target_compile_options将必要的选项添加到库目标的界面。这样,所有依赖于该库(通过 target_link_libraries)的目标都将使用该标志进行编译,但不需要它(并且可能与其不兼容)的目标将不会得到它。

如果要将标志添加到自己的目标之一,可以使用 target_compile_options(stxxl PUBLIC "/Zc:__cplusplus")(PUBLIC 而不是 接口(interface))。 INTERFACE 表示当其他目标依赖于该目标时使用该选项,PRIVATE 表示该选项用于目标本身,PUBLIC 表示两者。 (恕我直言 this artical 解释得很好。)


除此之外,您不应该通过编译标志设置 C++ 标准版本,而是使用 target_compile_features如果需要更新版本并使用
set_target_properties(your_target_name PROPERTIES CXX_STANDARD 17)如果您只想使用可用的更新版本。

在你的情况下,后者可能就足够了,因为如果 c++ 版本较旧,你的代码片段有后备。


如果更改 stxxl 的代码(通过拉取请求等)是一个选项,代码可以测试 _MSVC_LANG_MSC_VER .这样它就可以在不需要 /Zc:__cplusplus 的情况下工作。

关于c++ - 无法使用 Visual Studio 和 CMake 将 __cplusplus 设置为 C++17 标准,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57102212/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com