LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problem with linking linux shared library (https://www.linuxquestions.org/questions/linux-newbie-8/problem-with-linking-linux-shared-library-919019/)

Aniketk 12-16-2011 04:54 AM

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.

Satyaveer Arya 12-16-2011 06:20 AM

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).

Satyaveer Arya 12-16-2011 06:26 AM

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

Aniketk 12-16-2011 07:33 AM

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.

Satyaveer Arya 12-16-2011 08:07 AM

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.

Aniketk 12-16-2011 08:26 AM

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"

Satyaveer Arya 12-16-2011 08:43 AM

Aniket,

Just check this link if it helps you :

http://www.yolinux.com/TUTORIALS/Lib...ndDynamic.html

Aniketk 12-17-2011 02:29 AM

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.

Satyaveer Arya 12-17-2011 10:31 PM

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

Thank You!

Aniketk 12-22-2011 08:39 AM

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.

Satyaveer Arya 12-22-2011 12:04 PM

Really? Was it the problem because of cygwin?

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

Aniketk 12-23-2011 07:16 AM

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.


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