导致 ld: 重复符号的原因是什么
函数中的重复符号 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 中,
包含来自 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
*/
包含来自 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将函数定义为内联函数或将其实现移至 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.