我正在从我的Linux主机交叉编译一个windows应用程序,并且我得到了std中两个文件之间的多个定义的链接错误!
/usr/lib/gcc/i686-w64-mingw32/7.3-win32/libstdc++.a(cow-stdexcept.o):(.text$_ZNSt11logic_errorC2ERKS_+0x0): multiple definition of `std::logic_error::logic_error(std::logic_error const&)'
/home/user1/work/windows-release/test/test.o:/usr/lib/gcc/i686-w64-mingw32/7.3-win32/include/c++/stdexcept:113: first defined here
collect2: error: ld returned 1 exit status在stdexcept:113中,我找到了以下类的定义
class logic_error : public exception
{
__cow_string _M_msg;
public:
/** Takes a character string describing the error. */
explicit
logic_error(const string& __arg) _GLIBCXX_TXN_SAFE;
#if __cplusplus >= 201103L
explicit
logic_error(const char*) _GLIBCXX_TXN_SAFE;
#endif
#if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS
logic_error(const logic_error&) _GLIBCXX_USE_NOEXCEPT;
logic_error& operator=(const logic_error&) _GLIBCXX_USE_NOEXCEPT;
#endif
virtual ~logic_error() _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT;
/** Returns a C-style character string describing the general cause of
* the current error (the same string passed to the ctor). */
virtual const char*
what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_USE_NOEXCEPT;
# ifdef _GLIBCXX_TM_TS_INTERNAL
friend void*
::_txnal_logic_error_get_msg(void* e);
# endif
};这些是我的建筑旗子
-g -Wall -fno-strict-aliasing -D_WIN32_WINNT=0x0501 -D_GLIBCXX_USE_CXX11_ABI=0 -std=c++11 -Wno-unused-parameter -Wno-deprecated-declarations -Wno-placement-new -Wno-unused-local-typedefs -Wno-deprecated -Wextra -DBOOST_LOG_DYN_LINK -O3 -O -MMD -MP -MT发布于 2022-05-18 08:45:35
使用CXX_FLAGS += -D_GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS构建代码解决了这个问题。
通过使用nm -C test.o查看test.o的符号,我发现复制构造函数是定义的,它旁边有一个地址,我假设编译器会自动为我创建复制构造函数,因为由于#if _GLIBCXX_USE_CXX11_ABI || _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS的存在,编译器在类声明中找不到它的原型,这就是为什么通过定义_GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS,编译器会知道它在其他地方实现了,所以不要创建它!
https://stackoverflow.com/questions/72134088
复制相似问题