Multiple Definition, First Defined Here Errors

I have 3 projects: server, client and community. Creating header and source folders in Commons does not cause any problems, and I can freely access functions from the server and client.

However, for some reason, creating additional source / header files in a Server or Client project always causes multiple definition of (...) and first defined here errors.

Example:

commands.h (in the root directory of the Client project)

 #ifndef COMMANDS_H_ #define COMMANDS_H_ #include "commands.c" void f123(); #endif /* COMMANDS_H_ */ 

commands.c (in the root directory of the Client project)

 void f123(){ } 

main.c (in the root directory of the Client project)

 #include "commands.h" int main(int argc, char** argv){ } 

Errors:

 make: *** [Client] Error 1 Client first defined here Client multiple definition of `f123' commands.c 

Cleaning, index recovery, project restoration do not help. Does not restart the computer.

+13
c eclipse include definition multiple-definition-error
source share
4 answers

The problem here is that you include commands.c in commands.h before the function prototype. Therefore, the C preprocessor inserts the contents of commands.c into commands.h before the function prototype. commands.c contains a function definition. As a result, a function definition ends earlier than a function declaration that causes an error.

The contents of commands.h after the preprocessor phase looks like this:

 #ifndef COMMANDS_H_ #define COMMANDS_H_ // function definition void f123(){ } // function declaration void f123(); #endif /* COMMANDS_H_ */ 

This is an error because you cannot declare a function after defining it in C. If you interchanged #include "commands.c" and the declaration of the function, the error should not happen, because now the prototype of the function precedes the declaration of the function,

However, including a .c file is bad practice and should be avoided. The best solution for this problem would be to include commands.h in commands.c and link the compiled version of the command to the main file. For example:

commands.h

 #ifndef COMMANDS_H_ #define COMMANDS_H_ void f123(); // function declaration #endif 

commands.c

 #include "commands.h" void f123(){} // function definition 
+17
source share

You should not include command.c in your header file. In general, you should not include .c files. Rather, command.c should include .h commands. As defined here, the C preprocessor inserts the contents of .c commands into .h commands, where included. You get two definitions of f123 in .h commands.

commands.h

 #ifndef COMMANDS_H_ #define COMMANDS_H_ void f123(); #endif 

commands.c

 #include "commands.h" void f123() { /* code */ } 
+4
source share

You may have included the .c file in the makefile several times.

+1
source share

I add this A, because I came across a fancy version of it that really made me scratch my head for about an hour until I noticed the root cause. My download failed due to repeated repetition of this format

 <path>/linit.o:(.rodata1.libs+0x50): multiple definition of `lua_lib_BASE' <path>/linit.o:(.rodata1.libs+0x50): first defined here 

I ended up making a mistake in the magic of the Makefile, where I had a list of C files and using vpath, etc., so compilers will select them from the correct directory in the hierarchy. However, one C file was repeated in the list, at the end of one line and at the beginning of the next, so the gcc download generated by make had the .o file twice on the command line. Durrrrh. Multiple definitions were obtained from multiple instances of the same file. The compiler ignored duplicates, except for static initializers!

0
source share

All Articles