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?

user3664250
  • 31
  • 2
  • 2
  • 8
  • 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. – dragosht May 26 '14 at 07:27
  • 2
    possible duplicate of [redefinition c++](http://stackoverflow.com/questions/2823330/redefinition-c) – Mike Kinghan May 26 '14 at 07: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. – user3664250 May 26 '14 at 07:36
  • Mike's possible duplicate topic is also very useful for understanding these concepts. It also covers the C++ #pragma once directive. – dragosht May 26 '14 at 07:58
  • I know the concepts, I was taught by my teacher. What he didn't teach me was fixing collect2 errors. – user3664250 May 26 '14 at 07:59

1 Answers1

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.

dragosht
  • 3,237
  • 2
  • 23
  • 32
  • 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. – user3664250 May 26 '14 at 07:43
  • 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. – dragosht May 26 '14 at 07:47
  • 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 – user3664250 May 26 '14 at 07:51
  • Here's some other useful SO topic on the subject. Perhaps this would be more helpful: http://stackoverflow.com/questions/2216765/variable-definition-in-header-files – dragosht May 26 '14 at 07:53