cmake Clang似乎忽略了Mac OS下的C++版本(std=gnu++14及类似版本)[已关闭]

xzlaal3s  于 2022-11-11  发布在  Mac
关注(0)|答案(2)|浏览(161)

**已关闭。**此问题不符合Stack Overflow guidelines。当前不接受答案。

这个问题似乎与help center中定义的范围内的编程无关。
两个月前关门了。
Improve this question
我正在Mac OS 11.6.8上开发,安装了Xcode 13.2.1(但我正在VS Code中处理基于CMake的项目)。我被大量的构建错误所困扰,例如:

In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/iterator:419:
[build] /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/usr/include/c++/v1/__functional_base:80:16: error: no template named 'unary_function'; did you mean 'binary_function'?
[build]         static unary_function<_Ap, _Rp>

最后,我搜索了整个项目,确定这些问题一定是由于Boost使用了unary_function而引起的,据说C++ 17中已删除了unary_function。但是,我在CMakeLists.txt中指定了C++ 14,而且std=gnu++14确实包含在Clang调用中,这要归功于

set(CMAKE_CXX_STANDARD 14)

在我的CMakeLists.txt中。
在网上搜索这个错误并没有发现任何解决方案,但是确实出现了一些评论,询问苹果是否以某种方式破坏了对C17之前版本的支持。我不知道对不同C标准的支持是如何实现的。SDK/MacOSX12.1.sdk/usr/include/c++/v1(来自错误输出)是否给予了线索?
下面是铿锵的呼唤:

/usr/bin/clang++ -D_HAS_AUTO_PTR_ETC=0 -I/Users/me/data/series2server/include -I/Users/me/data/series2server/restbed/include -I/Users/me/data/series2server -I/Users/me/data/series2server/restbed_server/api -I/Users/me/data/series2server/restbed_server/model -g -arch arm64 -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -pthread -std=gnu++14 -MD -MT CMakeFiles/Series2Server.dir/main.cpp.o -MF CMakeFiles/Series2Server.dir/main.cpp.o.d -o CMakeFiles/Series2Server.dir/main.cpp.o -c /Users/me/data/series2server/main.cpp

使用unary_function的Boost 1.80.0文件包括:

functional.hpp
hash.hpp
logical.hpp
unique_ptr.hpp

以下是英特尔操作系统11.6.8下的错误堆栈:

In file included from /Users/me/data/series2server/main.cpp:3: THIS LINE SAYS #include <string>
In file included from .../MacOSX12.1.sdk/usr/include/c++/v1/string:511:
In file included from .../MacOSX12.1.sdk/usr/include/c++/v1/string_view:179:
In file included from .../MacOSX12.1.sdk/usr/include/c++/v1/__string:57:
In file included from .../MacOSX12.1.sdk/usr/include/c++/v1/algorithm:652:
In file included from .../MacOSX12.1.sdk/usr/include/c++/v1/utility:206:
In file included from .../MacOSX12.1.sdk/usr/include/c++/v1/__debug:14:
In file included from .../MacOSX12.1.sdk/usr/include/c++/v1/iosfwd:95:
In file included from .../MacOSX12.1.sdk/usr/include/c++/v1/wchar.h:119:
In file included from .../MacOSX12.1.sdk/usr/include/wchar.h:91:
In file included from /Users/me/data/series2server/restbed_server/model/time.h:24: THIS LINE SAYS #include <memory>
In file included from .../MacOSX12.1.sdk/usr/include/c++/v1/memory:676:
In file included from .../MacOSX12.1.sdk/usr/include/c++/v1/iterator:419:
.../MacOSX12.1.sdk/usr/include/c++/v1/__functional_base:80:16: error: no template named 'unary_function'; did you mean 'binary_function'?
        static unary_function<_Ap, _Rp>

以下是操作系统12.5.1下M1的错误堆栈:

In file included from /Users/me/data/series2server/main.cpp:3: THIS LINE SAYS #include<string>
In file included from .../MacOSX12.3.sdk/usr/include/c++/v1/string:519:
In file included from .../MacOSX12.3.sdk/usr/include/c++/v1/__debug:14:
In file included from .../MacOSX12.3.sdk/usr/include/c++/v1/iosfwd:98:
In file included from .../MacOSX12.3.sdk/usr/include/c++/v1/__mbstate_t.h:29:
In file included from .../MacOSX12.3.sdk/usr/include/c++/v1/wchar.h:123:
In file included from .../MacOSX12.3.sdk/usr/include/wchar.h:91:
In file included from /Users/me/data/series2server/restbed_server/model/time.h:24: THIS LINE SAYS #include <memory>
In file included from .../MacOSX12.3.sdk/usr/include/c++/v1/memory:674:
In file included from .../MacOSX12.3.sdk/usr/include/c++/v1/__memory/allocator.h:18:
.../MacOSX12.3.sdk/usr/include/c++/v1/stdexcept:83:32: error: unknown type name 'string'
    explicit logic_error(const string&);

最后注意:这是因为代码生成器创建了一个名为Time. h的文件,它显然导致了这些错误;我猜测是与其他地方的同名文件发生了冲突。

hlswsv35

hlswsv351#

Boost我们的Apple SDK似乎配置错误。Boost可以用C17编译。如果配置正确,Boost可以用宏_HAS_AUTO_PTR_ETC=0定义,并成功地用C17编译。例如,请参见functional.hpp


# if defined(_HAS_AUTO_PTR_ETC) && !_HAS_AUTO_PTR_ETC

        // std::unary_function and std::binary_function were both removed
        // in C++17.

        template <typename Arg1, typename Result>
        struct unary_function
        {
            typedef Arg1 argument_type;
            typedef Result result_type;
        };

        template <typename Arg1, typename Arg2, typename Result>
        struct binary_function
        {
            typedef Arg1 first_argument_type;
            typedef Arg2 second_argument_type;
            typedef Result result_type;
        };

# else

        // Use the standard objects when we have them.

        using std::unary_function;
        using std::binary_function;

# endif

您可以添加到CMakeLists.txt

target_compile_definitions(${TARGET} _HAS_AUTO_PTR_ETC=0)
j7dteeu8

j7dteeu82#

这个项目中的很多代码都是从OpenAPI文档中生成的。其中一个生成的类叫做Time。尽管它被表达在几个嵌套的名称空间中,但在某些时候它似乎与其他Time类或文件混淆了,导致了这些模糊的错误。
在OpenAPI文档中重命名Time模式,然后重新生成类似乎已经解决了这些问题。
感谢所有的反馈。

相关问题