LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-27-2006, 11:27 PM   #1
kranti
LQ Newbie
 
Registered: Oct 2005
Location: Pune
Posts: 17

Rep: Reputation: 0
using extern variable


Hi,

I've 2 'C' files in two different folders namely,

/home/data/file1.c
/home/info/file2.c

I've declared a variable in file1.c and I want to use it in file2.c. If I use 'extern' in file2.c 'undefined reference' error gets generated.

How such a variable could be used?

Thanks,
Kranti
 
Old 02-28-2006, 03:22 AM   #2
nguyennh
Member
 
Registered: Feb 2006
Location: Vietnam
Distribution: FC , RH , SuSE
Posts: 106

Rep: Reputation: 15
Show us how did you compile your code .
 
Old 02-28-2006, 03:28 AM   #3
kranti
LQ Newbie
 
Registered: Oct 2005
Location: Pune
Posts: 17

Original Poster
Rep: Reputation: 0
I've compiled the code by using 2 different makefiles for those two C files, which contain separate compilation details for respective files.

Last edited by kranti; 02-28-2006 at 10:10 PM.
 
Old 02-28-2006, 11:30 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in Programming, and has been moved accordingly to help your thread/question get the exposure it deserves.

That aside: if you want help, give more detail.
Quote:
'm compiled the code by using 2 different makefiles for those two C files, which contain separate compilation details for respective files.
doesn't really mean anything to anyone but you.


Cheers,
Tink
 
Old 02-28-2006, 02:35 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by kranti
I'm compiled the code by using 2 different makefiles for those two C files, which contain separate compilation details for respective files.
Then what did make output?
 
Old 02-28-2006, 04:50 PM   #6
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Such errors occurs at the linking stage, where you need to have the two files (in your case) linked together.

Edit: In simple words it means linking must happen in a common makefile (and using two files).
 
Old 02-28-2006, 10:37 PM   #7
kranti
LQ Newbie
 
Registered: Oct 2005
Location: Pune
Posts: 17

Original Poster
Rep: Reputation: 0
make gave following results for the code in which i'm making the respective variable extern i.e. "info"

make SPARC=1
/opt/SUNWspro/bin/CC -DSPARC -I../include -I/usr/local/pgsql/include
-I../../include -DDEBUG_ERROR_ENBLED -DDEBUG_WARNING_ENABLED
/opt/SUNWspro/bin/CC -DSPARC -I../include -I/usr/local/pgsql/include
-I../../include -DDEBUG_ERROR_ENBLED -DDEBUG_WARNING_ENABLED
-DDEBUG_NOTICE_ENABLED -DDEBUG_INFO_ENABLED app.c
-o ../../../bin/myApp/sparc/app -lpthread -lsocket
-lnsl
Undefined first referenced
symbol in file
info app.o
ld: fatal: Symbol referencing errors. No output written to
../../../bin/myApp/sparc/app
make: *** [../../../bin/myApp/sparc/app] Error 1
 
Old 03-01-2006, 01:38 PM   #8
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
Hi,

Quote:
/opt/SUNWspro/bin/CC -DSPARC -I../include -I/usr/local/pgsql/include
-I../../include -DDEBUG_ERROR_ENBLED -DDEBUG_WARNING_ENABLED
-DDEBUG_NOTICE_ENABLED -DDEBUG_INFO_ENABLED app.c
-o ../../../bin/myApp/sparc/app -lpthread -lsocket
Where do you include the object file making up your previously compiled second file (in object format)? It looks like you are just compiling and linking a single file?
 
Old 03-02-2006, 04:19 PM   #9
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
You need something like this:
/opt/SUNWspro/bin/CC -c -DSPARC -I../include -I/usr/local/pgsql/include -I../../include -DDEBUG_ERROR_ENBLED -DDEBUG_WARNING_ENABLED file1.c
/opt/SUNWspro/bin/CC -c -DSPARC -I../include -I/usr/local/pgsql/include -I../../include -DDEBUG_ERROR_ENBLED -DDEBUG_WARNING_ENABLED file2.c
/opt/SUNWspro/bin/CC -c -DSPARC -I../include -I/usr/local/pgsql/include -I../../include -DDEBUG_ERROR_ENBLED -DDEBUG_WARNING_ENABLED app.c
/opt/SUNWspro/bin/CC -o ../../../bin/myApp/sparc/app -lpthread -lsocket -lnsl file1.o file2.o app.o
 
Old 03-04-2006, 05:30 PM   #10
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You may have just made a slight error in your extern declaration; is the type the EXACT same as in the real declaration?
ta0kira
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
extern static variable alaios Programming 9 09-11-2005 04:11 AM
extern when to use or not alaios Programming 7 07-14-2005 05:47 PM
cannot find EXTERN.h chandan@bio Mandriva 0 04-10-2005 09:58 AM
extern between C and C++ linfan Programming 1 11-04-2003 05:08 PM
extern variables in c++ abi_sh Programming 1 08-11-2002 01:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration