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

时间:2012-01-16 09:42:03

标签: c++ include header-files ld

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 

for architecture x86_64

function 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 ...)

请帮忙。

1 个答案:

答案 0 :(得分:5)

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

包含警卫将避免在同一代码文件/对象中多次使用相同的标头。但是,他们不会避免在多个目标文件中使用相同的代码段,因为每个目标文件都是自己创建的,并且所有定义都已重置。