I don't understand Linker error 2005

Posted on 2008-11-19
Last Modified: 2013-12-14
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

Question by:letharion
    LVL 30

    Accepted 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,

    LVL 6

    Author Comment

    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.
    LVL 30

    Expert Comment

    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

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    maze travler 6 37
    Fast data scrub 19 85
    VS can't find System.IO.Packaging 5 231
    Copying WordPress Pages 5 49
    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now