80

I defined a special file: config.h

My project also has files:

t.c, t.h
pp.c, pp.h
b.c b.h
l.cpp

and #includes:

in t.c:

    #include "t.h"
    #include "b.h"
    #include "pp.h"
    #include "config.h"

in b.c:

    #include "b.h"
    #include "pp.h"

in pp.c:

    #include "pp.h"
    #include "config.h"

in l.cpp:

    #include "pp.h"
    #include "t.h"
    #include "config.h"

there are no include directives in my *.h files, only in *.c files. I defined this in config.h:

const char *names[i] =
        {
            "brian", "stefan", "steve"
        };

and need that array in l.cpp, t.c, pp.c but Im getting this error:

pp.o:(.data+0x0): multiple definition of `names'
l.o:(.data+0x0): first defined here
t.o:(.data+0x0): multiple definition of `names'
l.o:(.data+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [link] Error 1

I have include guards in every *.h file I use in my project. Any help solving this?

mazix
  • 2,540
  • 8
  • 39
  • 56
  • possible duplicate of [multiple definition linker error after adding a function to a previously linking file](http://stackoverflow.com/questions/3136616/multiple-definition-linker-error-after-adding-a-function-to-a-previously-linking) –  Jul 20 '13 at 18:36
  • 1
    Is there a recent change in debian's? Up until a few days my project was compiling fine.... I started to get this error (even for revisions that I tagged and were working fine a few days ago) after recent updates (I'm on debian testing). – eftshift0 Aug 15 '20 at 04:04

3 Answers3

163

Don't define variables in headers. Put declarations in header and definitions in one of the .c files.

In config.h

extern const char *names[];

In some .c file:

const char *names[] = { 
  "brian", "stefan", "steve" };

If you put a definition of a global variable in a header file, then this definition will go to every .c file that includes this header, and you will get multiple definition error because a varible may be declared multiple times but can be defined only once.

Also, one more thing you can do if you have to define your variables inside of a header file you can use the static keyword.

static const char *names[] = {
  "brian", "stefan", "steve" };

This way variable names will be defined only once in your entire program and can be accessed multiple number of times.

Ayush
  • 457
  • 8
  • 16
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 1
    solved! :) Could you please tell me why I shouldnt put declarations in `h*.` files? – mazix Jul 20 '13 at 17:30
  • 7
    So it won't do this. Use *.h files just to reference information. You have to remember that "include" means it is including all the *.h information in that file, so you would be copying that information (not just the reference) into every file using the *.h. This can get linkers confused, and does. – Jiminion Jul 20 '13 at 17:34
  • 4
    @mazix No, the **definition** of a global varible should go to .c file, and put the **declaration** in the header. Remember that you can declare a varible many times but define it only once. – Yu Hao Jul 20 '13 at 17:34
  • 1
    Just to make the explanation slightly clearer, the header file is likely to be included in more than one c file, so you'll end up with several declarations of a variable with the same name – Spalteer Jul 20 '13 at 18:17
  • is it possible to use `extern` for class constructor? – Ebrahim Karimi Oct 27 '18 at 20:07
  • The `extern` is actually redundant: `extern` linkage is the default. – Kotauskas Jun 05 '19 at 12:38
51

Declarations of public functions go in header files, yes, but definitions are absolutely valid in headers as well! You may declare the definition as static (only 1 copy allowed for the entire program) if you are defining things in a header for utility functions that you don't want to have to define again in each c file. I.E. defining an enum and a static function to translate the enum to a string. Then you won't have to rewrite the enum to string translator for each .c file that includes the header. :)

  • 4
    I actually find this answer far more convenient than the top answer. It is more convenient and appealing just to have one header to store all global variables for example, instead of having .h and .cpp files. Thank you. – Saik May 28 '17 at 02:25
  • I think also this solution is better than the most voted because the other needs to define values in each *.c file, and furthermore yours, @JonathanWhittenberg, looks more elegant. Thanks a lot – Alejandro Galera May 03 '18 at 08:59
  • 1
    Better and more elegant solution than the AA. +1 for adding why it's not always a bad idea. – SimonC Jan 08 '19 at 16:16
  • 8
    This isn't an accurate description of what static does. "You may declare the definition as static (only 1 copy allowed for the entire program)" NO! static makes the definition scoped to that compile unit. So it gives you one copy _per_ _compile_ _unit_. So if you have some large list of strings, your program will end up having many duplicate copies of this list ... one for each c file. This is even worse for static functions if you later go to debug this, because there is no longer a single function to place a breakpoint on, but many copies of that function in the resulting linked program. – PPenguin Jun 07 '19 at 23:20
0

Make your definitions static in the header files.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 07 '22 at 12:10