Cross platform compilation problem on linux: error: new_allocator.h:93:7: error: multiple overloads of 'address' instantiate to the same signature

Cross platform compilation problem on linux: error: new_allocator.h:93:7: error: multiple overloads of 'address' instantiate to the same signature

我的问题是我正在 mac 上开发一个 cli 软件,最终的二进制文件将在 linux 系统上执行。

所以我使用 docker 进行编译,但我有一个错误似乎不是来自我的代码(因为它在 mac 上就像一个魅力)但你知道.. . 我没有足够的 cpp / 工具链背景来理解 linux 上导致此错误的原因以及如何解决此问题。

TLDR :

cpp_1  | [  8%] Building CXX object core-lib/CMakeFiles/core-lib.dir/controllers/MainController.cpp.o
cpp_1  | In file included from /usr/src/OF/apps/project/core-lib/controllers/MainController.cpp:8:
cpp_1  | In file included from /usr/src/OF/apps/project/core-lib/controllers/MainController.hpp:11:
cpp_1  | In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/vector:61:
cpp_1  | In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/allocator.h:46:
cpp_1  | In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/x86_64-linux-gnu/c++/7.5.0/bits/c++allocator.h:33:
cpp_1  | /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/ext/new_allocator.h:93:7: error: multiple overloads of 'address' instantiate to the same signature '__gnu_cxx::new_allocator<const ColumnState>::const_pointer (__gnu_cxx::new_allocator<const ColumnState>::const_reference) const noexcept' (aka 'const ColumnState *(const ColumnState &) const noexcept')
cpp_1  |       address(const_reference __x) const _GLIBCXX_NOEXCEPT
cpp_1  |       ^
cpp_1  | /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/allocator.h:108:29: note: in instantiation of template class '__gnu_cxx::new_allocator<const ColumnState>' requested here
cpp_1  |     class allocator: public __allocator_base<_Tp>
cpp_1  |                             ^
cpp_1  | /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/stl_deque.h:550:16: note: in instantiation of template class 'std::allocator<const ColumnState>' requested here
cpp_1  |       : public _Tp_alloc_type
cpp_1  |                ^
cpp_1  | /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/stl_deque.h:633:19: note: in instantiation of member class 'std::_Deque_base<const ColumnState, std::allocator<const ColumnState> >::_Deque_impl' requested here
cpp_1  |       _Deque_impl _M_impl;
cpp_1  |                   ^
cpp_1  | /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/stl_deque.h:831:29: note: in instantiation of template class 'std::_Deque_base<const ColumnState, std::allocator<const ColumnState> >' requested here
cpp_1  |     class deque : protected _Deque_base<_Tp, _Alloc>
cpp_1  |                             ^
cpp_1  | /usr/src/OF/apps/project/core-lib/io/AbstractInstallCommunicator.hpp:17:35: note: in instantiation of template class 'std::deque<const ColumnState, std::allocator<const ColumnState> >' requested here
cpp_1  |     std::deque<const ColumnState> states;
cpp_1  |                                   ^
cpp_1  | /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/ext/new_allocator.h:89:7: note: previous declaration is here
cpp_1  |       address(reference __x) const _GLIBCXX_NOEXCEPT

CMAKE cpp 配置:

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

linux 配置:

    ubuntu bionic
    cmake --version : cmake version 3.16.5
    make --version : GNU Make 4.1
    clang --version : clang version 10.0.0-4ubuntu1~18.04.1 

mac 配置

    Mojave 10.14.6
    cmake --version : cmake version 3.16.3
    make --version : GNU Make 3.81
    clang --version : Apple clang version 11.0.0 (clang-1100.0.33.17)

当我在 mac 上编译时它工作正常:

building
-- The C compiler identification is AppleClang 11.0.0.11000033
-- The CXX compiler identification is AppleClang 11.0.0.11000033
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found GLM: /usr/local/include  
-- GLM_INCLUDE_DIR = /usr/local/include
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/fyoudine/Documents/workspaces/OF/of_v20190923_osx_release/apps/project-Optics/build
Scanning dependencies of target core-lib
[  2%] Building CXX object core-lib/CMakeFiles/core-lib.dir/config/GlobalConf.cpp.o
[  5%] Building CXX object core-lib/CMakeFiles/core-lib.dir/controllers/LightManager.cpp.o
[  8%] Building CXX object core-lib/CMakeFiles/core-lib.dir/controllers/MainController.cpp.o
[ 11%] Building CXX object core-lib/CMakeFiles/core-lib.dir/d3/Object3D.cpp.o
[ 14%] Building CXX object core-lib/CMakeFiles/core-lib.dir/d3/Object3DChild.cpp.o
[ 17%] Building CXX object core-lib/CMakeFiles/core-lib.dir/install/AbstractInstall.cpp.o
[ 20%] Building CXX object core-lib/CMakeFiles/core-lib.dir/install/FileConfigurationInstall.cpp.o
[ 22%] Building CXX object core-lib/CMakeFiles/core-lib.dir/install/KineticInstall.cpp.o
[ 25%] Building CXX object core-lib/CMakeFiles/core-lib.dir/io/DMXCommunicator.cpp.o
[ 28%] Building CXX object core-lib/CMakeFiles/core-lib.dir/io/SimulatorInstallCommunicator.cpp.o
[ 31%] Building CXX object core-lib/CMakeFiles/core-lib.dir/positions/PositionSummaryMap.cpp.o
[ 34%] Building CXX object core-lib/CMakeFiles/core-lib.dir/positions/SimpleOptimalPositionFinder.cpp.o
[ 37%] Building CXX object core-lib/CMakeFiles/core-lib.dir/provider/SimpleAnimationProvider.cpp.o
[ 40%] Building CXX object core-lib/CMakeFiles/core-lib.dir/raytracing/Prism.cpp.o
[ 42%] Building CXX object core-lib/CMakeFiles/core-lib.dir/raytracing/Ray/AABB.cpp.o
[ 45%] Building CXX object core-lib/CMakeFiles/core-lib.dir/raytracing/Ray/Base.cpp.o
[ 48%] Building CXX object core-lib/CMakeFiles/core-lib.dir/raytracing/Ray/Plane.cpp.o
[ 51%] Building CXX object core-lib/CMakeFiles/core-lib.dir/raytracing/Ray/Ray.cpp.o
[ 54%] Building CXX object core-lib/CMakeFiles/core-lib.dir/raytracing/RayPath.cpp.o
[ 57%] Building CXX object core-lib/CMakeFiles/core-lib.dir/simulation/Environment.cpp.o
[ 60%] Building CXX object core-lib/CMakeFiles/core-lib.dir/simulation/Simulator.cpp.o
[ 62%] Building CXX object core-lib/CMakeFiles/core-lib.dir/simulation/anim/InstallAnimator.cpp.o
[ 65%] Building CXX object core-lib/CMakeFiles/core-lib.dir/simulation/anim/TweenColumnAnimator.cpp.o
[ 68%] Building CXX object core-lib/CMakeFiles/core-lib.dir/simulation/data/RayCastResult.cpp.o
[ 71%] Building CXX object core-lib/CMakeFiles/core-lib.dir/simulation/elements/ColumnData.cpp.o
[ 74%] Building CXX object core-lib/CMakeFiles/core-lib.dir/simulation/elements/Light.cpp.o
[ 77%] Building CXX object core-lib/CMakeFiles/core-lib.dir/simulation/elements/PrismData.cpp.o
[ 80%] Building CXX object core-lib/CMakeFiles/core-lib.dir/states/ColumnState.cpp.o
[ 82%] Building CXX object core-lib/CMakeFiles/core-lib.dir/states/InstallState.cpp.o
[ 85%] Building CXX object core-lib/CMakeFiles/core-lib.dir/utils/calculateSunPosition.cpp.o
[ 88%] Building CXX object core-lib/CMakeFiles/core-lib.dir/utils/spa.cpp.o
[ 91%] Building CXX object core-lib/CMakeFiles/core-lib.dir/utils/vec_color.cpp.o
[ 94%] Linking CXX static library ../libs/libcore-lib.a
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: ../libs/libcore-lib.a(InstallAnimator.cpp.o) has no symbols
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: ../libs/libcore-lib.a(InstallAnimator.cpp.o) has no symbols
[ 94%] Built target core-lib
Scanning dependencies of target project-cli
[ 97%] Building CXX object project-cli/CMakeFiles/project-cli.dir/main.cpp.o
[100%] Linking CXX executable ../../bin/project-cli
[100%] Built target project-cli

但是当我通过 docker 为 linux 编译时:

starting docker_cpp_1 ... done
Attaching to docker_cpp_1
cpp_1  | building
cpp_1  | -- The C compiler identification is Clang 10.0.0
cpp_1  | -- The CXX compiler identification is Clang 10.0.0
cpp_1  | -- Check for working C compiler: /usr/bin/clang-10
cpp_1  | -- Check for working C compiler: /usr/bin/clang-10 -- works
cpp_1  | -- Detecting C compiler ABI info
cpp_1  | -- Detecting C compiler ABI info - done
cpp_1  | -- Detecting C compile features
cpp_1  | -- Detecting C compile features - done
cpp_1  | -- Check for working CXX compiler: /usr/bin/clang++-10
cpp_1  | -- Check for working CXX compiler: /usr/bin/clang++-10 -- works
cpp_1  | -- Detecting CXX compiler ABI info
cpp_1  | -- Detecting CXX compiler ABI info - done
cpp_1  | -- Detecting CXX compile features
cpp_1  | -- Detecting CXX compile features - done
cpp_1  | -- Found GLM: /usr/include  
cpp_1  | -- GLM_INCLUDE_DIR = /usr/include
cpp_1  | -- Configuring done
cpp_1  | -- Generating done
cpp_1  | -- Build files have been written to: /usr/src/OF/apps/project/build
cpp_1  | Scanning dependencies of target core-lib
cpp_1  | [  2%] Building CXX object core-lib/CMakeFiles/core-lib.dir/config/GlobalConf.cpp.o
cpp_1  | [  5%] Building CXX object core-lib/CMakeFiles/core-lib.dir/controllers/LightManager.cpp.o
cpp_1  | [  8%] Building CXX object core-lib/CMakeFiles/core-lib.dir/controllers/MainController.cpp.o
cpp_1  | In file included from /usr/src/OF/apps/project/core-lib/controllers/MainController.cpp:8:
cpp_1  | In file included from /usr/src/OF/apps/project/core-lib/controllers/MainController.hpp:11:
cpp_1  | In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/vector:61:
cpp_1  | In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/allocator.h:46:
cpp_1  | In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/x86_64-linux-gnu/c++/7.5.0/bits/c++allocator.h:33:
cpp_1  | /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/ext/new_allocator.h:93:7: error: multiple overloads of 'address' instantiate to the same signature '__gnu_cxx::new_allocator<const ColumnState>::const_pointer (__gnu_cxx::new_allocator<const ColumnState>::const_reference) const noexcept' (aka 'const ColumnState *(const ColumnState &) const noexcept')
cpp_1  |       address(const_reference __x) const _GLIBCXX_NOEXCEPT
cpp_1  |       ^
cpp_1  | /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/allocator.h:108:29: note: in instantiation of template class '__gnu_cxx::new_allocator<const ColumnState>' requested here
cpp_1  |     class allocator: public __allocator_base<_Tp>
cpp_1  |                             ^
cpp_1  | /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/stl_deque.h:550:16: note: in instantiation of template class 'std::allocator<const ColumnState>' requested here
cpp_1  |       : public _Tp_alloc_type
cpp_1  |                ^
cpp_1  | /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/stl_deque.h:633:19: note: in instantiation of member class 'std::_Deque_base<const ColumnState, std::allocator<const ColumnState> >::_Deque_impl' requested here
cpp_1  |       _Deque_impl _M_impl;
cpp_1  |                   ^
cpp_1  | /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/stl_deque.h:831:29: note: in instantiation of template class 'std::_Deque_base<const ColumnState, std::allocator<const ColumnState> >' requested here
cpp_1  |     class deque : protected _Deque_base<_Tp, _Alloc>
cpp_1  |                             ^
cpp_1  | /usr/src/OF/apps/project/core-lib/io/AbstractInstallCommunicator.hpp:17:35: note: in instantiation of template class 'std::deque<const ColumnState, std::allocator<const ColumnState> >' requested here
cpp_1  |     std::deque<const ColumnState> states;
cpp_1  |                                   ^
cpp_1  | /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/ext/new_allocator.h:89:7: note: previous declaration is here
cpp_1  |       address(reference __x) const _GLIBCXX_NOEXCEPT
cpp_1  |       ^
cpp_1  | In file included from /usr/src/OF/apps/project/core-lib/controllers/MainController.cpp:8:
cpp_1  | In file included from /usr/src/OF/apps/project/core-lib/controllers/MainController.hpp:16:
cpp_1  | In file included from /usr/src/OF/apps/project/core-lib/install/AbstractInstall.hpp:11:
cpp_1  | In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/deque:64:
cpp_1  | /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/stl_deque.h:843:31: error: '_Tp_alloc_type' is a protected member of 'std::_Deque_base<const ColumnState, std::allocator<const ColumnState> >'
cpp_1  |       typedef typename _Base::_Tp_alloc_type            _Tp_alloc_type;
cpp_1  |                               ^
cpp_1  | /usr/src/OF/apps/project/core-lib/io/AbstractInstallCommunicator.hpp:17:35: note: in instantiation of template class 'std::deque<const ColumnState, std::allocator<const ColumnState> >' requested here
cpp_1  |     std::deque<const ColumnState> states;
cpp_1  |                                   ^
cpp_1  | /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/stl_deque.h:464:21: note: declared protected here
cpp_1  |         rebind<_Tp>::other _Tp_alloc_type;
cpp_1  |                            ^
cpp_1  | /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/stl_deque.h:844:31: error: '_Alloc_traits' is a protected member of 'std::_Deque_base<const ColumnState, std::allocator<const ColumnState> >'
cpp_1  |       typedef typename _Base::_Alloc_traits             _Alloc_traits;
cpp_1  |                               ^
cpp_1  | /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/stl_deque.h:465:58: note: declared protected here
cpp_1  |       typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type>  _Alloc_traits;
cpp_1  |                                                          ^
cpp_1  | /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/stl_deque.h:845:31: error: '_Map_pointer' is a protected member of 'std::_Deque_base<const ColumnState, std::allocator<const ColumnState> >'
cpp_1  |       typedef typename _Base::_Map_pointer              _Map_pointer;
cpp_1  |                               ^
cpp_1  | /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/stl_deque.h:544:47: note: declared protected here
cpp_1  |       typedef typename iterator::_Map_pointer _Map_pointer;
cpp_1  |                                               ^
cpp_1  | 4 errors generated.
cpp_1  | make[2]: *** [core-lib/CMakeFiles/core-lib.dir/controllers/MainController.cpp.o] Error 1
cpp_1  | core-lib/CMakeFiles/core-lib.dir/build.make:88: recipe for target 'core-lib/CMakeFiles/core-lib.dir/controllers/MainController.cpp.o' failed
cpp_1  | make[1]: *** [core-lib/CMakeFiles/core-lib.dir/all] Error 2
cpp_1  | CMakeFiles/Makefile2:114: recipe for target 'core-lib/CMakeFiles/core-lib.dir/all' failed
cpp_1  | make: *** [all] Error 2
cpp_1  | Makefile:83: recipe for target 'all' failed
docker_cpp_1 exited with code 2

两者都是用相同的命令编译的:

cmake .. && make

错误本身来自std::deque<const ColumnState> states的定义。在 C+11 之前,std::deque 的元素类型必须是 CopyAssignable 和 CopyConstructible(因此不能像您的代码中那样是 const 限定的)- 参见 std::deque.

在 C+11 之后,约束实际上是由对双端队列执行的操作强加的:

The requirements that are imposed on the elements depend on the actual operations performed on the container. Generally, it is required that element type is a complete type and meets the requirements of Erasable, but many member functions impose stricter requirements.

因此,在没有看到操作 states 的代码的情况下,很难看出到底是什么触发了它。一个简单的解决方法(如果上下文允许的话)是删除那里的常量。当然,两个编译器的行为应该相同(如果它们都支持 c++17,并且实际上有一个违反常量的变量操作),但如果您只需要快速修复,这可以做到。