0

In C I get the linker error multiple definition of `main'. Yes, that is true but:

Why does the linker try to include the second (ext.c) main function although I have just included the header ext.h? I'd expect, the linker only links the functions whose prototypes have been found or which are needed by the initial main?

How can I solve this that a) test compiles and gets linked without issues (just use the func() from ext.c) and b) also ext.c can be compiled and linked as separate application?

The (example) code:

//file: test.c
#include "/home/stefanm/test/test.h"

void main (int argc, char * argv[])
{
    uint8_t var = 123;
    printf ("main(): var= %i\n", var);
    func (var);
                                                                                                                    }
//file: test.h
#ifndef TEST_H
#define TEST_H
#include <the rest>
#include "/home/stefanm/test/ext.h"                                                                                                                     
#endif

...and the external module:

//file: ext.c
#include "/home/stefanm/test/ext.h"
uint8_t func (uint8_t i){    
    printf ("func(): Variable i is %i", i); 
    return 0;
}

void main () {
    printf ("ext main func");
}   
//file: ext.h
#ifndef EXT_H
#define EXT_H
#include "all needed headers"  

uint8_t func (uint8_t);
#endif    

I call the compiler with gcc test.c ext.c -o test

4
  • 1
    No, you think wrong. Compiler will create a symbol for every function it founds (generally, unless inlined and/or optimized) and than linker will attempt to resolve those symbols, and it won't like duplicates.
    – SergeyA
    Commented Mar 15, 2019 at 18:51
  • What command are you using to compile this? If you compile only one of these .c files, the compiler will find only one main function.
    – ForceBru
    Commented Mar 15, 2019 at 18:52
  • 1
    In addition to the answer given, I'd like to point out that separating testing code and the final executable is nearly always a good idea, especially if you have test-only dependencies on external libraries.
    – user6564029
    Commented Mar 15, 2019 at 18:56
  • 2
    Addititionally, the return value of main should be int Commented Mar 15, 2019 at 19:12

3 Answers 3

6

Your external module should not have main() because it's a module and not an application. You should just move main() from your module to a separate file:

//file: app.c
#include "/home/stefanm/test/ext.h" // <-- BTW, using absolute paths is not a good idea

void main () {
    //use function from ext here
    printf ("app main func");
}

And then compile your application like this:

gcc app.c ext.c

and your test like this:

gcc test.c ext.c
1
  • This sounds like the way to go. It fits perfectly into my project (this thread was just an example to show my problem). And the absolute path and void main() were just result of the quick and dirty example. BTW this solution fits easily into the makefile that is part of my project. Commented Mar 15, 2019 at 19:34
1

In C, you can only have one definition of a function in all of the files you link into your executable. There's no good way to tell the compiler "I want to use this main() and not all the others". (There's a bad way, using macros, but it would be messy).

If you want to use a function with two different main() functions, put it in a separate file.

0

I suppose your compile/link call goes like

gcc test.c ext.c

In this case, test.c and ext.c (resp., to be exact, the .o files created out of them) are peers, i. e. on the same level. How should the linker know which version of the symbol main to take and which to discard? The linker doesn't know about the include files used.

In the case of a main function, the correct way to go is to have exactly one of them in your project.

For any other function where you have this requirement, there are several ways to go:

Either, you could declare one of them as "weak". It will be discarded when there is a "strong one".

Or you put your function into a library, e. g. libext.a. If you link that with -ext, only the object files which define symbols which are undeined are taken out of it. But then again, name clashes can occur if another name defined by that object file is defined already. So it is the best to only define as few symbols per object file as possible.

4
  • No, one should most certainly not declare main function as a weak symbol.
    – SergeyA
    Commented Mar 15, 2019 at 19:08
  • @SergeyA So the way to go is creating a library? Is this something that I can automatically do in the makefile? The "real" project I am working on is way more complicated and I am more a hardware designer having to add functionality to an embedded system. Would you describe the process of creating a .a a bit more? Commented Mar 15, 2019 at 19:14
  • @glglgl Putting ext.o into a library, as is, will not prevent the multiple definition error on Unix (maybe it does on Windows, I don't know). The Unix linker takes entire object files out of an .a library, so when test.o refers to func, and that reference is satisfied from ext.o, it will bring in ext.o's definition of main as well.
    – zwol
    Commented Mar 15, 2019 at 19:25
  • @SergeyA Except when experimenting and testing the limits of your tools. But mainly you are right, so I rephrased my answer.
    – glglgl
    Commented Mar 15, 2019 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.