导致 ld: 重复符号的原因是什么

发布于 2024-12-27 00:28:05 字数 1432 浏览 2 评论 0原文

函数中的重复符号 StringFunctions::intToString(int)

/Build/Intermediates/Y36PJC-mrvikmil.build/Debug/Y36PJC-mrvikmil.build/Objects-normal/x86_64/ServerSocket.o and 
/Build/Intermediates/Y36PJC-mrvikmil.build/Debug/Y36PJC-mrvikmil.build/Objects-normal/x86_64/main.o 

ld:体系结构 x86_64

StringFunctions::intToString(int) 位于 StringFunctions.h 中,

包含来自 ma​​in.cpp

#include <iostream>
#include <string>
#include "Exception.h" //does not include anything more
#include "ServerConsole.h"
    /* 
       which includes ServerSocket.h which includes ClientSocket.h which includes StringFunctions.h
       AND
       ServerSocket.h includes SocketException.h which includes StringFunctions.h
    */

包含来自 ServerSocket.cpp

#include <iostream>
#include <string>
#include "InvalidPortException.h" //does not include anything more
#include "SocketException.h" //which includes StringFunctions.h
#include "ClientSocket.h"    //which includes SocketException.h which includes StringFunctions.h
#include "StringFunctions.h" //StringFunctions.h

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <errno.h>

我相信只要我在 .h 文件中包含内容,我就可以在任何地方包含几乎任何内容...

我的所有文件都包含警卫(#ifndef ... #define ...)

请帮忙。

ld: duplicate symbol StringFunctions::intToString(int) in

/Build/Intermediates/Y36PJC-mrvikmil.build/Debug/Y36PJC-mrvikmil.build/Objects-normal/x86_64/ServerSocket.o and 
/Build/Intermediates/Y36PJC-mrvikmil.build/Debug/Y36PJC-mrvikmil.build/Objects-normal/x86_64/main.o 

for architecture x86_64

function StringFunctions::intToString(int) is in StringFunctions.h

includes from main.cpp:

#include <iostream>
#include <string>
#include "Exception.h" //does not include anything more
#include "ServerConsole.h"
    /* 
       which includes ServerSocket.h which includes ClientSocket.h which includes StringFunctions.h
       AND
       ServerSocket.h includes SocketException.h which includes StringFunctions.h
    */

includes from ServerSocket.cpp:

#include <iostream>
#include <string>
#include "InvalidPortException.h" //does not include anything more
#include "SocketException.h" //which includes StringFunctions.h
#include "ClientSocket.h"    //which includes SocketException.h which includes StringFunctions.h
#include "StringFunctions.h" //StringFunctions.h

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <errno.h>

I believe that as long as i have things in .h files, i can include pretty much anything anywhere...

All my files has include guard (#ifndef ... #define ...)

Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情勿念 2025-01-03 00:28:05

您必须将函数定义为内联函数或将其实现移至 cpp 文件。否则它将同时存在于两个对象中,并且 C++ 将不知道要链接哪一个(尽管它们是相同的)。

包含防护将避免在同一代码文件/对象中多次使用相同的标头。然而,他们不会避免在多个目标文件中使用相同的代码,因为每个目标文件都是单独创建的,并且所有定义都会重置。

You have to define the function as inline or move its implementation to a cpp file. Otherwise it will exist in both objects and C++ won't know which one (despite them being identical) to link.

Include guards will avoid having the same header multiple times in the same code file/object. However they won't avoid having the same piece of code in multiple object files, as each one is create on its own with all defines reset.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文