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

LVL 6
letharionAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZoppoCommented:
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,

ZOPPO
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
0
ZoppoCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.