LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-16-2011, 05:54 AM   #1
Aniketk
LQ Newbie
 
Registered: Dec 2011
Posts: 10

Rep: Reputation: Disabled
Problem with linking linux shared library


I'm c++ programmer, but fairly new to developing on linux platforms.
I am trying to build an application that uses a share library providing API implementation.

To test library linking, I have written a simplest main.cpp that calls most basic function from this shared library.
I get following error while linking main.o
>>>>>>>>
$ g++ main.o -o main.exe -L /usr/lib -l :libmylib.so
/usr/lib/gcc/pc/i686-pc-cygwin/../../../libmylib.so: Could not read symbols: File in wrong format
>>>>>>>


/usr/lib/libmylib.so is symbolic link to the actual shared library.
The 'file' command gives below output
>>>>>>>
$ file -L <actual lib name>
<Actual libname>: ELF 32-bit LSB shared object, Intel 80386, Version 1(SYSV), dynamically linked, stripped
>>>>>>>>


Also, I can see all the functions using "nm -D" or "readelf" tools.

What might be wrong here? I am using Cygwin environment to emulate linux.
 
Old 12-16-2011, 07:20 AM   #2
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Hello Aniket,

I'm assuming that you want to link statically,

If you want to link, say, libapplejuice statically, but not, say, liborangejuice, you can link like this:

gcc object1.o object2.o -Wl,-Bstatic -lapplejuice -Wl,-Bdynamic -lorangejuice -o binary

There's a caveat - if liborangejuice uses libapplejuice, then libapplejuice will be dynamically linked too.

You'll have to link liborangejuice statically alongside with libapplejuice to get libapplejuice static.

And don't forget to keep -Wl,-Bdynamic else you'll end up linking everything static, including libc (which isn't a good thing to do).

Last edited by Satyaveer Arya; 12-16-2011 at 07:27 AM.
 
Old 12-16-2011, 07:26 AM   #3
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Hello Aniket,

Other thing you can do is, just take an example :

Suppose you have an application which dynamically loads liba.so (with dlopen).
liba.so uses libb.so so you want to link liba.soagainst libb.so

So you need to link liba.so with -l option

gcc -o liba.so liba.o -L/libb/path -lb

If you don't have liba sources, perhaps you could create libawrapper.so linked against liba and libb and to load dynamically this library

gcc -o libawrap.so -L/liba/ -L/libb/ -la -lb
 
Old 12-16-2011, 08:33 AM   #4
Aniketk
LQ Newbie
 
Registered: Dec 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thanks!

I just have one library which is .so
I don't have source code for that.

When I try static linking (-Wl,-Bstatic), the error says "Attempted static link of dynamic object <libname>"
When I try dynamic linking (-Wl,-Bdynamic), the error says "Could not read symbols: File in wrong format".

Is this because I am using cygwin?
I don't think it should be a problem.
 
Old 12-16-2011, 09:07 AM   #5
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Aniket,

No, the problem is not because of cygwin.

Have you installed the static libraries before running the code you tried? Because if you need static link you have to install static libraries.
 
Old 12-16-2011, 09:26 AM   #6
Aniketk
LQ Newbie
 
Registered: Dec 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
Yes I have the library placed in /usr/lib (I think that's what you mean by install, right?)
I'm not even trying to run the app, the problem is in the build stage.

Also, I don't need static linking, I'm good with dynamic linking.

I checked the architecture of my object (main.o) and library file - both are 32-bit.

These are some one the commands I tried, but all fail with same error
$ gcc maino -o main -L/usr/lib -lmylib
$ gcc maino -o main -L/usr/lib -l:libmylib.so
$ gcc maino -o main -L/usr/lib -Wl,-Bdynamic -l:libmylib.so

The error always says "could not read symbols: file in wrong format"
 
Old 12-16-2011, 09:43 AM   #7
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Aniket,

Just check this link if it helps you :

http://www.yolinux.com/TUTORIALS/Lib...ndDynamic.html
 
Old 12-17-2011, 03:29 AM   #8
Aniketk
LQ Newbie
 
Registered: Dec 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thanks so much for your effort to see me thru this!

I checked the page you mentioned and lot of other stuff on net.
Generally, it is beleived that this can happen only if library is originally compiled with different architecture.

So I'm going with the theory that something is wrong with the library itself and not the way I'm trying to link it.

Just to be sure, I'm going to install linux VM (instead of cygwin) and see if i can get it to work on it.
 
Old 12-17-2011, 11:31 PM   #9
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
You're welcome Aniket. You can click on reputation to give reputation if you find this thread helpful.

Thank You!
 
1 members found this post helpful.
Old 12-22-2011, 09:39 AM   #10
Aniketk
LQ Newbie
 
Registered: Dec 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
So I found the problem and it was 'cygwin' I guess.
I was under the impression that cygwin emulates linux shell and build environment on windows.
But seems compilers in cygwin build windows executable and expect windows DLLs as 'shared libraries'.


I installed coLinux with debian FS, it took me a while to figure out and fix various issues.
But it working great and I'm able to build linux binaries using coLinux.

Now I need to hunt for nice GUI based IDE for linux based c++ projects.
 
Old 12-22-2011, 01:04 PM   #11
Satyaveer Arya
Senior Member
 
Registered: May 2010
Location: Palm Island
Distribution: RHEL, CentOS, Debian, Oracle Solaris 10
Posts: 1,415

Rep: Reputation: 305Reputation: 305Reputation: 305Reputation: 305
Really? Was it the problem because of cygwin?

Can you please tell me what was the issue with cygwin?

Last edited by Satyaveer Arya; 12-22-2011 at 01:05 PM.
 
Old 12-23-2011, 08:16 AM   #12
Aniketk
LQ Newbie
 
Registered: Dec 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
No, I don;t think there is any issue with cygwin.
It's just that ti was a wrong choice.

Cygwin (as far as I understand it) does not provide linux cross compiler.
It more or less provides ease of application porting from Linux to Windows.

What I really needed was crosscompiling env on linux that will allow me to build linux applications on windows.

I'm currently going with coLinux with debian image. Hopefully, this works.
 
  


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
Linking an executable to a shared library (C++) lennyk Programming 4 06-25-2009 05:45 AM
Need help linking shared library g++ 4.1.2 x86_64 UlamTheLucky Programming 5 09-23-2007 05:55 PM
LINUX - linking archive (static library) with shared (dynamic) library gurkama Programming 5 03-05-2007 12:11 AM
Commandline legth problem linking a shared library DavePrince Linux - Software 0 07-20-2005 09:19 AM


All times are GMT -5. The time now is 02:33 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration