Link to home
Start Free TrialLog in
Avatar of letharion
letharionFlag for Sweden

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.
#include "Socket.cpp"
#pragma comment(lib, "wsock32.lib")
 
int main() {
	return 0;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of letharion

ASKER

Bah, ofc it was simple. But I still don't really get it. Since socket.cpp includes the .h file, and not the other way around, I thought that was the "proper" way to do it.
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