Linker error

This is a discussion on Linker error within the C Programming forums, part of the General Programming Boards category; My C program consists 3 files: common.h, common.c and testmain.c In common.h file I defined as: /*global variables- variable declaration*/ ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    85

    Question Linker error

    My C program consists 3 files: common.h, common.c
    and testmain.c

    In common.h file I defined as:
    /*global variables- variable declaration*/
    extern int global_var; /*tell compiler do not
    optimize at compile time*/
    /*public interface function*/
    extern int FUN1(int A);
    ...
    -------------------------------------------
    In common.c file I created as:
    #include "C:\common.h" /*where the file locate*/
    /* global variables -variable definition*/
    int global_var;

    /*public function definition*/
    int FUN1(int A)
    {
    global_var = 20;
    printf("global_var at FUNC is: %d",global_var);
    return(A + global_var);

    }
    -----------------------------------------------
    And in testmain.c file:
    #include "common.h"

    int main()
    {
    int result;
    int global_var = 40; /*global variable define*/
    int a = 50;
    printf("Begin global_var is %d",global_var);
    result = FUN1(a);
    return 0;
    }

    I compiled my program without any error, but when
    I link/build, it gave linker's errors as follows:
    Linker error: undefined symbol global_var in module C:\common.c.....
    Is anything related with wrong setting about the compiler(I use Borland C/C++ 5.1 compiler)?
    You guys know what wrong with my prog?
    I really appreciate your help!
    DV007

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >In common.c file I created as:
    >#include "C:\common.h" /*where the file locate*/

    Leave out this line. You don't need to:
    #include "C:\common.h"
    in common.c

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    85
    Thank you for the tip, Swoopy.
    Linker error for global_var disappear after I
    removed #include "C:\common.h" in the file common.c. However, linker error for FUN1 still
    display when I build my program:
    linker error: undefined symbol FUN1....

    I have also tried to remove the "#include common.h" in testmain.c file, but still raise
    an linker error.
    Any idea???? Thanx again!
    DV007

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Well, you don't need the header file at all:
    Code:
    /* testmain.c */
    
    int global_var = 40; /* global variable define */ 
    
    extern void FUN1();
    
    int main() 
    { 
    	FUN1(); 
    	printf("global_var is: %d\n", global_var);
    	return 0; 
    }
    Code:
    /* common.c */
    
    extern int global_var;
    
    void FUN1() 
    { 
    	printf("global_var is: %d\n", global_var);
    	global_var = 60;
    }
    B.t.w. I think your link error is because the extern keyword before int FUN1(int A) in common.h.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    85
    Monster, I have tried your way but it still
    have the same problem with linker error for
    undefine FUN1 in module testmain.c
    I have read some reference C books and I have
    learned that it's legal to declare EXTERN
    functions and variables in .h file(header file)
    such as the file fun.h below

    #ifndef FUN_H
    #define FUN_H

    /*global variable declaration here,
    *but not create here variable definition yet */
    extern int glob_var /*declar a share global var*/

    /*Public function interface*/
    extern void FUN(void);

    #endif

    So, as you mentioned about removing all EXTERN in
    common.h file. I think it's not right. I think
    it's ok to leave them there in common.h files.
    Thank you anyway!
    DV007

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Leave testmain.c like you have it, and in common.h:

    >extern int FUN1(int A);

    just use:

    int FUN1(int A);

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    85
    Thanx for your advice, swoopy.
    I tried it, but it still does not fix the error.
    Have you tried to compile my program on Borland
    C/C++ compiler? Do you get any error?
    Any C expert out there can help please!
    DV007

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I compiled your program with an older Borland compiler, and it ran fine and printed out 20.

    Remember you need to create (open) a project and add two files to the project, testmain.c and common.c.

    If you are compiling from the command line, use something like:
    bcc32 testmain.c common.c

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    85

    Cool

    Ah hah, thanks for pointed it out
    about creating a PROJECT(or makefile) for my
    program. I have been trying to compile
    and build the testmain.c file in Borland editor.
    I though it will automaticly pull up all modules
    (common.h,common.c, maintest.c) together when build. It's my BAD!
    I really appreciate u guys's idea and help!
    DV007
    Here is final working program I post here to share
    with someone who might need for reference in the future.
    /*============common.h file=============*/
    ...
    /* Declare share global variable */
    extern int global_var;

    /* Delare public inteface function */
    extern void FUN() /*I change FUN1() to FUN() */
    /*remember use extern FUN()*/ /*is work fine */
    ....
    /*--end common.h-----*/

    /*============common.c===================*/
    #include <stdio.h>
    ...
    /*Define globle variable*/
    int gloval_var;

    /*Function definition*/
    void FUN()
    {
    global_var = 10;
    printf("global_var in FUN function is %d\n",global_var); /
    }
    ....
    /*-----end common.c------*/

    /*==========testmain.c==========*/
    #include <stdio.h>
    #include "common.h"

    /*== Data structures== */
    int global_var = 40;

    /*-MAIN-*/
    int main()
    {
    printf("global_var before call FUN function is %d\n", global_var); /* global_var = 40*/
    FUN(); /* call FUN() funtion, global_var =10 */

    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 02:29 PM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 12:54 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM

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