ceclipseincludedefinitionmultiple-definition-error

"Multiple definition", "first defined here" errors


I have 3 projects: Server, Client and Commons. Making header & source pairs in Commons doesn't cause any problems and I can access the functions freely from both Server and Client.

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

Example:

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

#ifndef COMMANDS_H_
#define COMMANDS_H_

#include "commands.c"

void f123();

#endif /* COMMANDS_H_ */

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

void f123(){

}

main.c (in root dir 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, rebuilding index, rebuilding projects doesn't help. Neither does restarting the computer.


Solution

  • The problem here is that you are including commands.c in commands.h before the function prototype. Therefore, the C pre-processor inserts the content of commands.c into commands.h before the function prototype. commands.c contains the function definition. As a result, the function definition ends up before than the function declaration causing the error.

    The content of commands.h after the pre-processor 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 can't declare a function after its definition in C. If you swapped #include "commands.c" and the function declaration the error shouldn't happen because, now, the function prototype comes before the function declaration.

    However, including a .c file is a bad practice and should be avoided. A better solution for this problem would be to include commands.h in commands.c and link the compiled version of 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