Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Just another "multiple definition first defined here" error

Okay, so I have been searching every forum on this "multiple definition first defined here" problem for the last 4 hours, and I guess I must be really stupid, but I can't seem to solve this problem.

I have the following files:

main.cpp
Socket.h
Socket.cpp
Server.h
Server.cpp
Packet.h
FileHandlerIn.h
FileHandlerOut.cpp

main.cpp includes only Server.h, which declare some functions, which are then defined in Server.cpp.
Server.cpp uses functions from Socket.cpp, FileHandlerOut.cpp and Packet.h, so the header files Socket.h, Packet.h and FileHandlerIn.h are included in Server.h. None of the other header files includes anything else than standard libraries.

What happens is that every function in Server.cpp and every function in Packet.h gets a multiple definition error. The reason I have functions inside Packet.h is that I have a struct defined, and the Packet.h functions all operate on the struct. I'm really confused about how I should correctly define these functions, so I can use them in other cpp files, and this might be part of my problem?

like image 231
user1419999 Avatar asked Feb 14 '26 09:02

user1419999


1 Answers

every function in Packet.h gets a multiple definition error

If you're going to define functions in a header (outside a class definition), you'll need to mark them inline. This relaxes the One Definition Rule to allow definitions in multiple translation units.

You're still only allowed one definition in each translation unit, so make sure the headers have include guards.

like image 90
Mike Seymour Avatar answered Feb 15 '26 23:02

Mike Seymour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!