【发布时间】:2021-11-28 18:54:54
【问题描述】:
我正在尝试学习一些关于 c++ 模块的知识。我编写了简单的源代码,我试图在没有运气的情况下进行编译。出于某种原因,我遇到了多个定义的链接器问题,但我不明白原因。我的 g++ 版本是来自 cygwin 的 11.2。我的代码中有错误吗?是别的吗?
$ g++ -std=c++20 -fmodules-ts module_a.cpp module_b.cpp main.cpp
/usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: /tmp/ccfWIJyE.o:module_b.cpp:(.text+0xc): multiple definition of `std::string::_Alloc_hider::~_Alloc_hider()'; /tmp/cc8Ygf2b.o:module_a.cpp:(.text+0xc): first defined here
/usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: /tmp/ccfWIJyE.o:module_b.cpp:(.text+0xc): multiple definition of `std::string::_Alloc_hider::~_Alloc_hider()'; /tmp/cc8Ygf2b.o:module_a.cpp:(.text+0xc): first defined here
/usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: /tmp/ccfWIJyE.o:module_b.cpp:(.text+0x28): multiple definition of `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'; /tmp/cc8Ygf2b.o:module_a.cpp:(.text+0x28): first defined here
/usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: /tmp/ccfWIJyE.o:module_b.cpp:(.text+0x28): multiple definition of `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'; /tmp/cc8Ygf2b.o:module_a.cpp:(.text+0x28): first defined here
/usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: /tmp/ccfWIJyE.o:module_b.cpp:(.text+0x82): multiple definition of `std::string::_Alloc_hider::_Alloc_hider(char*, std::allocator<char> const&)'; /tmp/cc8Ygf2b.o:module_a.cpp:(.text+0x82): first defined here
/usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: /tmp/ccfWIJyE.o:module_b.cpp:(.text+0x82): multiple definition of `std::string::_Alloc_hider::_Alloc_hider(char*, std::allocator<char> const&)'; /tmp/cc8Ygf2b.o:module_a.cpp:(.text+0x82): first defined here
/usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: /tmp/ccfWIJyE.o:module_b.cpp:(.text+0xb8): multiple definition of `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string<std::allocator<char> >(char const*, std::allocator<char> const&)'; /tmp/cc8Ygf2b.o:module_a.cpp:(.text+0xb8): first defined here
/usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: /tmp/ccfWIJyE.o:module_b.cpp:(.text+0xb8): multiple definition of `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string<std::allocator<char> >(char const*, std::allocator<char> const&)'; /tmp/cc8Ygf2b.o:module_a.cpp:(.text+0xb8): first defined here
collect2: error: ld returned 1 exit status
main.cpp:
import module_a;
import module_b;
int main()
{
test_a();
test_b();
}
module_a.cpp:
module;
import <iostream>;
export module module_a;
export void test_a()
{
std::cout << std::string("test module a\n");
}
module_b.cpp:
module;
import <iostream>;
export module module_b;
export void test_b()
{
// std::cout << "test module b\n"; // <- this works
std::cout << std::string("test module b\n"); // <- this cause the issue
}
【问题讨论】:
-
您是否尝试先单独构建标准标头?像这样:
g++ -std=c++20 -fmodules-ts -xc++-system-header iostream。您是否也尝试在两个文件中对string和import <string>;执行相同的操作?您可能还希望将导入声明移出全局模块片段并移入模块中,以便同时导出std::cout和std::string()的功能。
标签: c++ gcc c++-modules