• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

I don't understand Linker error 2005

I take the implementation, and the header files from this website:

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

  • 2
1 Solution
Hi letharion,

I guess the error is the first line, you include a .cpp file instead of a .h file - it should be:

> #include "Socket.h"

Hope that helps,

letharionAuthor Commented:
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now