-
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
-
>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
-
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
-
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.
-
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
-
Leave testmain.c like you have it, and in common.h:
>extern int FUN1(int A);
just use:
int FUN1(int A);
-
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
-
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
-
:rolleyes: 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;
}