开发者

how do i avoid this linking error?

if i have defined a global variable(with initialization) in header file, and includ开发者_如何学Ced this file in two file and try to compile and link, compiler gives linking error

headers.h:

#ifndef __HEADERS
#define __HEADERS
int x = 10;
#endif

1.c:

#include "headers.h"
main ()
{
}

2.c:

#include "headers.h"
fun () {}


The linker complains because there will be multiple definitions of x once it puts all the object files together to create the executable. You have two different source files including the same header file, and that header file defines a variable x having a value of 10, so you end up with two definitions of x (one in 1.c and another in 2.c).

To avoid multiple definition linker errors, put this in a header file (for example globals.h):

#ifndef GLOBALS_H
#define GLOBALS_H

/* 
 * The "extern" keyword means that this variable is accessible everywhere
 * as long as this declaration is present before it is used. It also means
 * that the variable x may be defined in another translation unit. 
 */
extern int x;

#endif

Then put this in one source file:

#include "globals.h"

int x = 10; 


This is a classic case where you want either the variable declared or declared-and-defined.

If you define it in both source files, you will get a double-definition linker error. One way to handle this is to only set __HEADERS for one of the source files so that it is the one where the variable is defined.

All other source files get the declaration only.

>>headers.h
    #ifndef __HEADERS
        int x = 10;
    #else
        extern int x;
    #endif

>>1.c
    #define __HEADERS
    #include "headers.h"
    int main (void) {
        return 0;
    }

>>2.c
    #include "headers"
    void fun (void) {
    }

Of course, it's best to leave definitions out of header files altogether in case you accidentally define __HEADERS in two source files. Try:

>>headers.h
    extern int x;

>>1.c
    #include "headers.h"
    int x = 10;
    int main (void) {
        return 0;
    }

>>2.c
    #include "headers"
    void fun (void) {
    }


#include works exactly the same as if you copied and pasted the text from the header file.

Consider it in that way and you will see that you have therefore put the line int x=10 into both your source files.

A fixed version is below:

>>headers.h
#ifndef __HEADERS
#define__HEADERS
extern int x;  // extern tells the compiler it will be linked from another file
#endif
-----------------
>>1.c
#include "headers.h"
int x = 10;  // must have it in ONE file for linking purposes
main ()
{
}
---------------------
>>2.c
#include "headers"
fun () {}


  1. Define __HEADERS in the ifndef.
  2. Put declarations, not definitions, in your headers:
// header

extern int x;

 // implementation

int x = 10;

3. 2.c has the include wrong.

So:

// headers.h

#ifndef __HEADERS
#define __HEADERS
    extern int x;
#endif

// 1.c

#include "headers.h"

int x = 10;

main ()
{
}

// 2.c

#include "headers.h"
fun () {}

You can define x anywhere. Just make it one place.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜