LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-24-2010, 09:50 AM   #1
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Rep: Reputation: 43
finding if a library is linked into my executable


Hi all,

is there a way to find if a static lib is linked into my executable. For example I have say

liba.a
libb.a
libc.ko (dynamic)

I link as per normal in my makefile, it reports no errors. In my code I add
#include "liba.h"
#include "libb.h"
#include "libc.h"

everything is ok.

I call a function from lina

libafunc()

it's OK

However when I try to call any function from b it reports that the linbfunc() is missing. I think libb is linking from make output. I know it sees linb.h as if I declare a structure defined in it its OK. I know my libbfunc() func is spelt write, I've cut and pasted from the libb.h prototype declaration.

So it looks like libb is not linked even though it says it is from the make file?

Is there some command I can type to my executable to find out what static libs have been lined in? Thanks

Last edited by knobby67; 03-24-2010 at 09:51 AM.
 
Old 03-24-2010, 11:10 AM   #2
slinx
Member
 
Registered: Apr 2008
Location: Cleveland, Ohio
Distribution: SuSE, CentOS, Fedora, Ubuntu
Posts: 106

Rep: Reputation: 23
man ldd
 
Old 03-24-2010, 01:19 PM   #3
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by knobby67 View Post
Is there some command I can type to my executable to find out what static libs have been lined in? Thanks
I'm afraid not - ldd will show you the dynamic dependencies. No information about the statically linked libraries ends up in the executable.

What's the actual error you get - could you post it here? Is it at link-time or run-time?

It sounds like it's at link-time. You can use nm to list the symbols in your libb.a file - make sure the symbol you need (i.e. the one the linker complains about) is defined there.



Quote:
Originally Posted by knobby67 View Post
I've cut and pasted from the libb.h prototype declaration
You've got a declaration, fine - but is it defined anywhere? Check the definition matches the declaration, and that the relevant object file's been linked into libb.a.
 
Old 03-25-2010, 03:44 AM   #4
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Original Poster
Rep: Reputation: 43
Hi

well the prototype is shown as application_start

so I do a test call
Code:
application_start(&test_struct,0);
this give the following error

main.c.text+0xc): undefined reference to `application_start'
collect2: ld returned 1 exit status
make: *** [all] Error 1


when I nm the library I can't see a pure application_start (like I would in my own libs) however I can see

00000d20 T _Z22application_startP11data_ti

Which seems a little odd?

Anyway nm demangle works and shows

00000d20 T application_start(data_t*, int)

Last edited by knobby67; 03-25-2010 at 04:02 AM.
 
Old 03-25-2010, 04:46 AM   #5
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by knobby67 View Post
main.c.text+0xc): undefined reference to `application_start'
collect2: ld returned 1 exit status
make: *** [all] Error 1


when I nm the library I can't see a pure application_start (like I would in my own libs) however I can see

00000d20 T _Z22application_startP11data_ti

Which seems a little odd?
It looks like the library was compiled with a C++ compiler, which will produce the mangled symbol "_Z22application_startP11data_ti", and then you're compiling your program with a C compiler, which will look for the unmangled symbol "application_start".

There are several ways around this:

Probably the best way is to just compile everything, including your application, with a C++ compiler - it will always use name mangling, and you'll then be able to find your symbol. This may cause the reverse problem if liba.a is compiled with a C compiler, but just compile that as C++ instead. As a bonus, you'll get the extra type checking of C++.

You could also prevent libb.a from mangling its exported symbols by using extern "C" - the usual way to do this is like the following in the C++ header file:

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

// declarations you want to get from C...

#ifdef __cplusplus
}  // closes extern "C"
#endif
John G
 
Old 03-25-2010, 05:20 AM   #6
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Original Poster
Rep: Reputation: 43
Thanks, yes it's a C++ lib, the programmer had absolutely assured me it was standard C. I'm not happy with them!
 
Old 03-25-2010, 05:30 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,118

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Perhaps a click on the "thanks" button might be appropriate then ...
 
Old 03-25-2010, 05:40 AM   #8
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by knobby67 View Post
Thanks, yes it's a C++ lib, the programmer had absolutely assured me it was standard C. I'm not happy with them!
It probably is written in standard C - it's just not compiled with a standard C compiler. Compiling C with a C++ compiler is generally not a bad idea - it's much stricter, and discards only a few features of C that are "generally" considered dangerous anyway.

You just need to remember not to use any C++ features and to use extern "C", of course but you can always check this by occasionally making sure it builds with a C compiler.

Last edited by JohnGraham; 03-25-2010 at 05:41 AM.
 
  


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
[SOLVED] g++ dynamically linked library not being found posop Programming 6 04-14-2010 03:20 PM
C open source library for storage items ( trees, double linked lists ) zeelog Programming 1 08-17-2009 03:52 PM
library linked twice okhan Programming 1 08-12-2005 10:33 PM
howto compile bin with my library using all-static and shared linked standart library stpg Programming 4 06-29-2004 04:20 AM

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

All times are GMT -5. The time now is 11:23 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
Open Source Consulting | Domain Registration