2

Process_struct.h //header file

#define MAX_PROCS 5
#define EXIT 1
#define TRUE 1
/*******************************/
FILE *file=NULL;
/*******************************/
static FILE *outLog=NULL;
pthread_t producer;//Producer Thread ID
pthread_t consumer[MAX_PROCS];//consumer thread ID

This is the error I get when I go to run my Makefile:

    /tmp/ccvDJUQI.o:(.bss+0x8): multiple definition of `file'
    /tmp/cc4RWdZ4.o:(.bss+0x8): first defined here
    collect2: error: ld returned 1 exit status
    make: *** [Multiprocessor] Error 1

The build tells me that I have multiple definition of file in my program. Well the only section that I'm calling my file is in my header file. With my header file code above, there is no multiple definition of file in the file. The file is used throughout both of my .c program files but is only appears in my header file as a variable. I'm not sure why this error appears. Can anyone help me with fixing this error?

5
  • I can imagine such errors if the header file is left include-unguarded and then included into more than one source file compiled together into a library or something ... Is this the case? Defining that file into a header file looks weird enough though. Commented May 26, 2014 at 7:27
  • 2
    possible duplicate of redefinition c++ Commented May 26, 2014 at 7:33
  • @dragosht It isn't specifically that file. I've just narrowed it down to where the error would be at and posted that code. Commented May 26, 2014 at 7:36
  • Mike's possible duplicate topic is also very useful for understanding these concepts. It also covers the C++ #pragma once directive. Commented May 26, 2014 at 7:58
  • I know the concepts, I was taught by my teacher. What he didn't teach me was fixing collect2 errors. Commented May 26, 2014 at 7:59

2 Answers 2

3

If you have more than one .c file and include your header into them then each one of them will have that file defined inside (as the preprocessor just takes your header code and stuffs it into it before compilation). If you then compile them together (as in a library) you will get that particular linker error.

The solution is to move these definitions: FILE *file=NULL; static FILE *outLog=NULL; pthread_t producer;//Producer Thread ID pthread_t consumer[MAX_PROCS];//consumer thread ID

into just one of your .c files. Eventually you could leave them into the header marked with the extern keyword to let the linker know they're defined elsewhere. Having definitions inside header files is usually not such a good practice. The header files should also be guarded against multiple inclusions conflicts as in:

#ifndef MY_HEADER_H__
#define MY_HEADER_H__

... your code here

#endif

Another solution would be to make all of them static into your header. However this would mean that each .c file will access its own data.

Sign up to request clarification or add additional context in comments.

4 Comments

I've tried adding static and extern to FILE and all that has done is give me more errors that just show errors that aren't suppose to be there.
If you mark them extern in your header you should then define them in one of your .c files. Otherwise the linker will see no definition for them and fail.
they only need to be define once. They are defined the same because they are using the same variable so why not define them in the header file where they need to be
Here's some other useful SO topic on the subject. Perhaps this would be more helpful: stackoverflow.com/questions/2216765/…
0

Ideally fix the code to avoid duplicate definitions. If that's not an option and if they're identical definitions, one option could be to tell the linker to ignore the duplicate definitions and use the first one by adding this directive

LDFLAGS="-Wl,-allow-multiple-definition"

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.