[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • 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.


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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