HELP: linking several object files

This is a discussion on HELP: linking several object files within the C Programming forums, part of the General Programming Boards category; I want to link some object files. I use the attached "makefile", but I got the following error when making ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    Question HELP: linking several object files

    I want to link some object files. I use the attached "makefile", but I got the following error when making the executable file:

    UX:ld: ERROR: Doctors.o: fatal error: symbol `scr_name` multiply-defined, also in file centre.o
    collect2: ld returned 1 exit status
    *** Error code 1 (bu21)
    UX:make: ERROR: fatal error

    Actually, the symbol scr_name is declared in medico.h file, which is included in all the .c files.

    I even had to use -include option for compiling each .c file because the line

    centre.o : centre.c medico.h


    does not work.

    tanx.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    7
    scr_name is a variable defind in (and only in!) medico.h as followed:


    char * scr_name[MAX_SCREEN_CNT] = {
    "ERROR"
    ...


    and as you may see in the attached makefile, all the .c programs include the header file. The header file and all the c files are in the same directory.

    When I remove the -include parameter from gcc -c commands, it does not include the header file, and gives error message for all the varaibles and functions declared in medico.h.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>extern char * scr_name[MAX_SCREEN_CNT] ;
    Put that in the header, and this:
    >>char * scr_name[MAX_SCREEN_CNT] { /*whatever */ };
    in one of the .c files, and see how you get on.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Originally posted by Salem
    scr_name() should be defined in the .h file, and declared ONCE in one of the .c files.
    Shouldn't it be the other way around?
    scr_name() should be declared in the .h file, and defined ONCE in one of the .c files.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    7
    I changed the medico.h file as below:


    extern int INSERT_MODE;
    extern int INQUIRY; /* will be true or false */
    extern int PRINT_HARD_COPY; /* hard copy on printer */
    extern char LOG_FILE_NAME[MED_MAX_SIZE];
    extern char * typ_ary[];
    extern char * scr_name[MAX_SCREEN_CNT];


    And I added a function to initialze these vars. This function is called at the main function:


    int Init_Global_Vars(void) {

    int rc = TRUE;

    int INSERT_MODE = TRUE;
    int INQUIRY;
    int PRINT_HARD_COPY = FALSE;

    char * scr_name[MAX_SCREEN_CNT] = {
    "ERROR"
    ,"DOCTORS"
    ...


    Now, I get the following error:

    Undefined first referenced
    symbol in file
    INSERT_MODE Capture.o
    scr_name common.o
    INQUIRY common.o
    typ_ary Capture.o
    PRINT_HARD_COPY Reports.o
    LOG_FILE_NAME common.o

    UX:ld: ERROR: medico: fatal error: Symbol referencing errors. No output written to medico
    collect2: ld returned 1 exit status
    *** Error code 1 (bu21)
    UX:make: ERROR: fatal error.


    I have included medico.h in the above .c files.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with linking files
    By magda3227 in forum C Programming
    Replies: 11
    Last Post: 06-18-2008, 02:00 AM
  2. linking files?
    By wart101 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-20-2007, 05:40 PM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 05:24 PM
  4. windows header files not linking correctly
    By skorman00 in forum Windows Programming
    Replies: 2
    Last Post: 04-13-2005, 12:14 PM
  5. Linking C files (Unix)
    By f97tosc in forum C Programming
    Replies: 3
    Last Post: 03-23-2003, 05:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21