letharion
asked on
I don't understand Linker error 2005
I take the implementation, and the header files from this website:
http://www.adp-gmbh.ch/win/misc/sockets.html
I open a new empty c++ project in Visual Studio 2008.
I add two files, Socket.cpp and Socket.h, and paste the respective code in there.
I add main.cpp, which looks like below.
Since the project should be blank, except the code I just pasted, I don't understand why I get a load of error LNK2005: "private: static void __cdecl Socket::Start(void)" (?Start@Socket@@CAXXZ) already defined in Socket.obj in file: main.obj
Probably pretty basic stuff, but I'm rather new to cpp.
http://www.adp-gmbh.ch/win/misc/sockets.html
I open a new empty c++ project in Visual Studio 2008.
I add two files, Socket.cpp and Socket.h, and paste the respective code in there.
I add main.cpp, which looks like below.
Since the project should be blank, except the code I just pasted, I don't understand why I get a load of error LNK2005: "private: static void __cdecl Socket::Start(void)" (?Start@Socket@@CAXXZ) already defined in Socket.obj in file: main.obj
Probably pretty basic stuff, but I'm rather new to cpp.
#include "Socket.cpp"
#pragma comment(lib, "wsock32.lib")
int main() {
return 0;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi letharion,
well, the error itself was '... already defined in Socket.obj in file: main.obj'
This simply means a function with same name and declaration was already compiled, so the linker can't decide which one to use.
That in your case happens since once 'socket.cpp' is compiled for itself, then it's compiled again while included in 'main.cpp'.
In general you can say including a .cpp file is very unusual and mostly wrong. The header (which you should include) just contains the declaration of everything other .cpp files need to get compiled. The implementation of the functions which reside in the .cpp file should be compiled only once.
ZOPPO
well, the error itself was '... already defined in Socket.obj in file: main.obj'
This simply means a function with same name and declaration was already compiled, so the linker can't decide which one to use.
That in your case happens since once 'socket.cpp' is compiled for itself, then it's compiled again while included in 'main.cpp'.
In general you can say including a .cpp file is very unusual and mostly wrong. The header (which you should include) just contains the declaration of everything other .cpp files need to get compiled. The implementation of the functions which reside in the .cpp file should be compiled only once.
ZOPPO
ASKER