LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 08-10-2004, 09:53 AM   #1
xs2harpreet
LQ Newbie
 
Registered: Aug 2004
Posts: 2

Rep: Reputation: 0
Question Shared Library error : undefined reference


Hi
I am creating a shared library on Linux but when i link it with a program it gives undefined reference for a function which is defined in the shared library.

e.g.'
*********Header file function declarattion

<abc.h> int sum(int ,int);

******* definition is in the Shared Library
libabc.so
//definition
#include <abc.h>
int sum(int a,int b)
{
return a+b;

}

************program where i am using that shared library "libabc.so"


#include <abc.h>
int main()
{
int c;
c=sum(10,20);
cout<< c;

}


But when i build it . it gives an error undefined reference to sum(int,int)..
would you please solve my problem.
Thanks and Regard
Harpreet Singh
 
Old 08-10-2004, 09:59 AM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
well we really need to know how your building it and whether the undefined reference is at the compile or link stage, also i see your using cout without iostream?
 
Old 08-11-2004, 08:06 AM   #3
xs2harpreet
LQ Newbie
 
Registered: Aug 2004
Posts: 2

Original Poster
Rep: Reputation: 0
Hi all
I have solved my problem . Thanx
 
Old 03-11-2009, 04:18 PM   #4
hexaplus
LQ Newbie
 
Registered: Mar 2009
Posts: 2

Rep: Reputation: 0
C funtions from a shared library

This is the worst thing you can do in a forum IMO..if you have solved the problem post the answer for future generations pal!!
I am also new to linux development and have the same problem:
my make file follows:
a.out : hello.o
@echo Building Hello World
g++ hello.o -L. -lABC

hello.o : hello.cpp test.c libABC.so
@echo Building Hello
g++ -c hello.cpp -I${COTS_ECLIPSE_BOOST_INCDIR_38} -I/vobs/online/egse_api/egse_api/src/

libABC.so : ABC.o BC.o test.o
@echo Building Library
gcc -shared -Wl,-soname,libABC.so -o libABC.so ABC.o test.o ./BC.o -lc

ABC.o : ABC.cpp
@echo Building ABC
gcc -fPIC -g -c -Wall ABC.cpp

BC.o : BC.cpp
@echo Building ABC
gcc -fPIC -g -c -Wall BC.cpp

test.o : test.c
@echo Building ABC
gcc -fPIC -g -c test.c -o test.o

I've simplified the problem to the same level as the last poster test.c defines one c function sum(int i, int s);

hello.cpp uses that function. you'll notice that that ABC and BC are also in the library the hello.cpp uses those with out error (that is if I remove the sum call I compile fine):

bash-3.2$ make
Building ABC
gcc -fPIC -g -c test.c -o test.o
Building Library
gcc -shared -Wl,-soname,libABC.so -o libABC.so ABC.o test.o ./BC.o -lc
Building Hello
g++ -c hello.cpp -I/usr/include/boost/boost_1_38_0 -I/vobs/online/egse_api/egse_api/src/
Building Hello World
g++ hello.o -L. -lABC /usr/include/boost/boost_1_38_0/stage/lib/libboost_regex-gcc41-mt-1_38.a
hello.o: In function `main':
hello.cpp.text+0x9e): undefined reference to `sum(int, int)'
collect2: ld returned 1 exit status
make: *** [a.out] Error 1

Thanks
 
Old 03-11-2009, 04:34 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Superficially, it certainly all *looks* reasonable.

I noticed you're compiling some stuff with C (gcc), and other stuff with C++ (g++). You know that, by default, C++ names are "mangled".

Q: Are you using 'extern "C" {...}' to make sure your external functions are callable by both C and C++? Do you have an extern "C" in your header?

EXAMPLE:
Code:
#ifndef _ABC_H
#define _ABC_H

#ifdef __cplusplus
 extern "C" {
#endif

int sum (int a, int b);
#ifdef __cplusplus
}
#endif /* extern "C" */
#endif /* _ABC_H */
PS:
Another "trick" is to use "nm" to verify exactly what symbols are exported from your .so.
 
Old 03-11-2009, 05:39 PM   #6
hexaplus
LQ Newbie
 
Registered: Mar 2009
Posts: 2

Rep: Reputation: 0
I do in my actual code but I did not in my example here I will give it a shot in the simplified example.

... Great that worked thank you!

The code compiles on windows (VC7.1) because I have the extern "C" around the implementation of the source that is:

Code:
#ifdef __cplusplus
 extern "C" {
#endif

int sum (int a, int b)
{
   return a+b;
}

#ifdef __cplusplus
}

But did not extern "C" around the header file after adding that to the header it compiles and links fine.
Thanks paulsm4
 
Old 03-11-2009, 05:55 PM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Cool - glad it worked!

One additional note: you only need the extern "C" in the header, not around the code itself. All you need to do is make sure you #include your header in your source file, before "sum ()".
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Undefined reference error jacques83 Programming 20 04-11-2013 07:23 AM
Getting undefined reference to main error?? Mistro116@yahoo.com Programming 14 07-29-2011 08:28 AM
undefined reference error Quest101 Programming 3 01-01-2005 12:27 PM
Linking own library: undefined reference to otosigo Programming 1 12-02-2004 08:05 AM
undefined reference to a function included in a library i made!!! keos Programming 5 02-21-2004 04:02 PM

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

All times are GMT -5. The time now is 05:17 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration