Jimm
Jimm

Reputation: 8505

how to troubleshoot through gcc temporary file creation

While compiling C (not c++) code, I am getting error during link time, that certain identifiers have been defined at multiple places, but as shown below, the output is very cryptic.

Is there a way to get better messages from gcc, so that I can see which files are the cause of multiple definitions?

/tmp/cc8kgsLE.o:(.rodata+0x0): multiple definition of `PR_SZ'
/tmp/ccDfv6U4.o:(.rodata+0x0): first defined here
/tmp/cc8kgsLE.o:(.rodata+0x8): multiple definition of `PR_SEC_SZ'
/tmp/ccDfv6U4.o:(.rodata+0x8): first defined here
/tmp/cc8kgsLE.o:(.rodata+0x10): multiple definition of `PR_NSEC_SZ'
/tmp/ccDfv6U4.o:(.rodata+0x10): first defined here

collect2: ld returned 1 exit status

UPDATE: Based on responses, i clarify further that PR_SZ, PR_SEC_SZ, PR_NSEC_SZ are defined in ONE .h file, which is protected by #ifndef, #define and #endif macros..

In terms of compiling, i simply type:

gcc -Wall -I. -file1.c file2.c -o file2

UPDATE:

In addition to responses, i found the below link relevant global constants without using #define

Upvotes: 1

Views: 543

Answers (2)

user93353
user93353

Reputation: 14039

You can do this.

In the header file

/* a.h */
MYEXTERN int PR_SZ; /* or whatever your variable data type is */

In the first .c file

/* a.c */
/* MYEXTERN doesn't evaluate to anything, so var gets defined here */
#define MYEXTERN 
#include "a.h"

In the other .c files

/* MYEXTERN evaluates to extern, so var gets externed in all other C files */
#define MYEXTERN extern
#include "a.h"

So it gets defined in only one .c file & gets externed in all the others.

Upvotes: 2

benjarobin
benjarobin

Reputation: 4487

The output displayed doesn't look cryptic for me, it's very clear... The globals variables (this is bad) PR_SZ, PR_SEC_SZ, PR_NSEC_SZ are defined into more than one .c files

How do you compile your project ?

Here the main problem, it's that .o filename doesn't match the filename of the .c file. So to be able to see a better message you should improve your Makefile or whatever you are using to build your project.

For information :

  • In .h the prototype of global variables declarations must be prefixed by the keyword : extern
  • In only one .c file the variable should be declare normally

Upvotes: 4

Related Questions