Míla Mrvík
Míla Mrvík

Reputation: 535

What is causing ld: duplicate symbol

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.

Upvotes: 2

Views: 1595

Answers (1)

Mario
Mario

Reputation: 36487

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.

Upvotes: 5

Related Questions