“静态链接时出现“多重定义”错误”

huangapple go评论69阅读模式
英文:

"Multiple definition" error when statically linking

问题

I'm trying to compile a link together these simple example programs:

main.cpp:

#include "server.hpp"

int main()
{

}

server.hpp:

#ifndef SERVER_HPP
#define SERVER_HPP

#include <boost/asio.hpp>

class Server
{
    boost::asio::io_context context;

    void listen();
};

#endif

server.cpp:

#include "server.hpp"

void Server::listen()
{
    static boost::asio::ip::tcp::acceptor acceptor {
        context,
        {boost::asio::ip::tcp::v4(), 1000}
    };
}

I'm compiling all this with gcc 13.1:

g++ -static-libgcc -static-libstdc++ -std=c++23 .\main.cpp .\server.cpp -lws2_32

However, for some reason I cannot understand, I get this linker error:

D:/Programmi/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Programmi/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../lib\libstdc++.a(tinfo.o):(.text$ZNKSt9type_infoeqERKS+0x0): multiple definition of `std::type_info::operator==(std::type_info const&) const'; C:\Users\jayok\AppData\Local\Temp\cc4dUKMC.o:server.cpp:(.text$ZNKSt9type_infoeqERKS[ZNKSt9type_infoeqERKS]+0x0): first defined here
collect2.exe: error: ld returned 1 exit status

If I make the body of the "listen" function empty, or if I make the linking not-static by omitting those arguments, however, this error no longer appears, and the compilation is successful.

英文:

I'm trying to compile a link together these simple example programs:

main.cpp:

#include &quot;server.hpp&quot;

int main()
{

}

server.hpp:

#ifndef SERVER_HPP
#define SERVER_HPP

#include &lt;boost/asio.hpp&gt;

class Server
{
    boost::asio::io_context context;

    void listen();
};

#endif

server.cpp:

#include &quot;server.hpp&quot;

void Server::listen()
{
    static boost::asio::ip::tcp::acceptor acceptor {
        context,
        {boost::asio::ip::tcp::v4(), 1000}
    };
}

I'm compiling all this with gcc 13.1:
> g++ -static-libgcc -static-libstdc++ -std=c++23 .\main.cpp .\server.cpp -lws2_32
>

However, for some reason I cannot understand, I get this linker error:
> D:/Programmi/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Programmi/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../lib\libstdc++.a(tinfo.o):(.text$ZNKSt9type_infoeqERKS+0x0): multiple definition of `std::type_info::operator==(std::type_info const&) const'; C:\Users\jayok\AppData\Local\Temp\cc4dUKMC.o:server.cpp:(.text$ZNKSt9type_infoeqERKS[ZNKSt9type_infoeqERKS]+0x0): first defined here
> collect2.exe: error: ld returned 1 exit status

If I make the body of the "listen" function empty, or if I make the linking not-static by omitting those arguments, however, this error no longer appears, and the compilation is successful.

答案1

得分: 1

> So, could it be a compiler bug?
确实,这似乎是一个与 GCC 相关的问题,更确切地说是与 libstdc++ 相关的问题。

> How do I signal it to the developer?
根据 libstdc++ 文档

> ### 实现错误
>
> 已知错误的信息、修复这些错误的努力细节以及已修复的错误都可在 GCC 缺陷跟踪系统 的“libstdc++”组件中找到。

在这里,现在已经报告了这个问题。(最初找不到,但后来找到了更新。)

解决方法:

如果可以不使用运行时类型信息(RTTI),那么使用 -fno-rtti 编译选项可以节省您的构建时间。

英文:

> So, could it be a compiler bug?

Indeed, this seems to be a GCC — or rather libstdc++ — related issue.

The reason there's nothing much to find about it is that it only seems to occur with c++23 and static linking, so probably not very many of us have hit it.

> How do I signal it to the developer?

From the libstdc++ docs:

> ### Implementation Bugs
>
> Information on known bugs, details on efforts to fix them, and fixed
> bugs are all available as part of the GCC bug tracking system, under
> the component “libstdc++”.

Here, it's been reported now, actually. (Couldn't find it initially, but came back with the update.)

Workaround:

If you can do without RTTI, then compiling with -fno-rtti can save your build.

huangapple
  • 本文由 发表于 2023年5月21日 04:48:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297301.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定