I'm trying to learn a little bit about c++ modules. I wrote simple source code which I'm trying to compile without luck. For some reason I'm experiencing linker issues with multiple definitions but I don't understand the reason. My g++ version is 11.2 from cygwin. Do I have a bug in my code? Is it something else?
$ 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
}