LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 03-02-2015, 02:33 PM   #1
multiplex22
Member
 
Registered: Dec 2014
Location: ny, us
Distribution: most
Posts: 56
Blog Entries: 1

Rep: Reputation: Disabled
Post Compiling, binary w Dll


Like in Windows systems, I want to make dynamic link libraries that can be reused without rebuilding binaries. If I make a change to a library and the header is unaltered, I dont have to rebuild the entire project. This is the simplest way of patching/updating.

On Linux I have found that my libraries from the ar program are being directly injected into the binary. I have to rebuild any binary that uses the slightly altered library.

First, I build an object with complex arguments. Second I call ar with its arguments on the object. Third, i build the binary. All seemed satsfactory, even trying debug and release versions. However, this is confusing from the size of the binary to viewing its contents, to getting new segment errors. Im sure im missing something.

Has anyone tried building a library that is truly dynamic like Windows?

I guess im getting what I deserve.
 
Old 03-03-2015, 04:26 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Yes, shared libraries are quite common in Unix. For example, try this:
Code:
ldd /bin/bash
every 'so'-file you see is a 'shared object' (or DLL)
 
Old 03-03-2015, 06:14 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by multiplex22 View Post
On Linux I have found that my libraries from the ar program are being directly injected into the binary. I have to rebuild any binary that uses the slightly altered library.
ar builds .a libraries in Linux, which correspond to .lib in Windows.

You want .so libraries in Linux, which correspond to .dll in Windows.

If you are coding C and/or C++, then the easiest way to create the .so file is with appropriate options to GCC (which will then invoke the linker for you to create the .so). Alternately, you could create .o files and invoke the linker yourself. But you still need to change your GCC options, because a .o intended for use in a .so is different from an ordinary .o

Also be aware of another Linux vs. Windows difference. When you create a .DLL in Windows, you also create a stub .lib and that stub lib is used when linking executables that use the .DLL. When you create a .so, there is no stub .a; The .so itself is used in linking the final executable.

Depending on how you set things up, an automated build system may think the final executable needs to be relinked every time the .so is relinked. But if you changed only implementation, not interface within the .so, an executable linked against an older version of the .so will work with a newer version.
 
Old 03-03-2015, 07:35 AM   #4
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,511

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
Dynamic / shared libraries :

. http://tldp.org/HOWTO/Program-Librar...libraries.html
 
1 members found this post helpful.
Old 03-03-2015, 03:31 PM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by knudfl View Post
Quite a lot of that just applies to those creating .so files for inclusion in packages in distributions and/or for general purpose utilities to be used by multiple other projects.

All of that seems to be mixed there with the basic things you **need** to do in order to build/use a .so at all.

I'm just worried all that would make a beginner think .so files in Linux are far more complicated than DLL files in Windows, and so might give up on the idea entirely. In fact building and using .so files in Linux is slightly simpler than DLL in Windows.

If you are building a piece of what you hope to be a popular open source project, then you do need to do all that stuff the specified way, rather than some easier way that works just as well for just your own use.

I'm guessing the OP is working on a smaller and/or less public project and would be better off doing things a simpler way. If that is incorrect, please ignore this post.
 
1 members found this post helpful.
Old 03-03-2015, 03:44 PM   #6
multiplex22
Member
 
Registered: Dec 2014
Location: ny, us
Distribution: most
Posts: 56

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
That explains a lot. Ive tried those before, but maybe by-passed through it. I was thinking for some reason, .so files require object files with them. Like a debug version. When I explore the contents, they tend ro leave specific details in them (such as my systems library path). Theres not a lot of explanations on these areas in the docs.

im gonna experiment with this again, and look for some comparison data.
 
Old 03-03-2015, 08:40 PM   #7
genss
Member
 
Registered: Nov 2013
Posts: 741

Rep: Reputation: Disabled
Quote:
Originally Posted by multiplex22 View Post
I was thinking for some reason, .so files require object files with them. Like a debug version. When I explore the contents, they tend ro leave specific details in them (such as my systems library path).
debug data (example symbol names) get put directly in the ELF file (DWARF format usually)

the only full path in a ELF shared object is the loader path (ld-linux-whatever)
the loader then finds the required library
http://tldp.org/HOWTO/Program-Librar...libraries.html

it is cleaner, compared to windows
 
Old 03-05-2015, 10:54 AM   #8
multiplex22
Member
 
Registered: Dec 2014
Location: ny, us
Distribution: most
Posts: 56

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
So I wouldnt have to install my own libraries in /usr/lib?
 
Old 03-05-2015, 11:09 AM   #9
genss
Member
 
Registered: Nov 2013
Posts: 741

Rep: Reputation: Disabled
if you use LD_LIBRARY_PATH or LD_PRELOAD you can use any library from anywhere

an example of a program using those is steam
it does it so it can ship a set of common libraries for games to use, without being distro dependent
(some say having many distros is a "big problem", when it's really not and never was)

Last edited by genss; 03-05-2015 at 11:11 AM.
 
Old 03-05-2015, 11:50 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Instead of /usr/lib, I would suggest /usr/local/lib or $HOME/lib to install shared objects.
 
1 members found this post helpful.
Old 03-06-2015, 02:14 PM   #11
multiplex22
Member
 
Registered: Dec 2014
Location: ny, us
Distribution: most
Posts: 56

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
The shared libraries do store a path. If you specify a folder name with the so build, it is stored in the so library. This causes a problem in ldconfig. It will take the library folder in addition to the path in the so to itself. Picture attached...

I honestly misunderstood so. This stands for shared object, so I understood this to mean debugged.

I also overlooked ldconfig. Even after a successful build, programs ran did not locate the so. Any new libraries without ldconfig ran will be missed.

I didnt intend this as a world library, just personal things of sorts.

To remedy:

1. Run gcc -fpic on my object, then a gcc -Wl,-soname,... on the object. The so is built in the same directory (leaving clutter)
2. Install library in /usr/local/lib or edit /etc/ld.so.conf with install directory.
3. Build binary with full path or -l<name>

notes: make sure prefix is lib and suffix is .so as required.
Attached Thumbnails
Click image for larger version

Name:	shared_library_path_included.png
Views:	13
Size:	8.0 KB
ID:	17741  
 
Old 03-06-2015, 03:05 PM   #12
genss
Member
 
Registered: Nov 2013
Posts: 741

Rep: Reputation: Disabled
SHARED LIBRARIES DO NOT STORE A PATH !!!!

argh
use strings if you don't believe me (or readelf or ldd)

it is ld, the loader, that looks in a few directories for the requested library
try running this:
ld --verbose | grep SEARCH_DIR | tr -s ' ;' \\012
 
1 members found this post helpful.
Old 03-07-2015, 12:39 AM   #13
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
stored libs and executables might store directories, where the dynamic loader will search for dependent shared libraries
See gcc option -Wl,-rpath,<path>
 
Old 03-09-2015, 11:42 AM   #14
fred2014
Member
 
Registered: Mar 2015
Posts: 70

Rep: Reputation: Disabled
just a personal opinion - life is so much easier with fpc/lazarus

I build both dll and .so files with it and I found even the first one I did quite simple
(although I was familiar with lazarus itself beforehand so that may have helped)

If you're wondering I'm not especially advocating lazarus over C - I also do embedded work
in C - I just find the above to be much simpler for other work.

The only issues I've found with the various nix implementations is that there seem
to be multiple ways of handling special characters on the command line in some of
the older versions. This had me tearing my hair out to get all of them compatible
but I dont think thats an issue with recent releases.
 
  


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
Compiling vs Binary Mercury305 Linux - Software 8 08-22-2012 06:23 AM
What Happens if You replace all the Wine .dll's with Native Windows .dll's? The Dude Linux - Software 1 10-26-2009 11:05 AM
Shadowbane under wine is not working UBUNTU says needs dll but the dll is there ? zonemikel Linux - Games 2 04-13-2009 10:30 AM
Compiling to make a "Linux dll" using windows cpp's, .h's, and .LIB a1drich Linux - Software 4 05-11-2007 04:41 AM
compiling from source vs. binary packages oldi Linux - Newbie 4 06-04-2004 10:54 AM

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

All times are GMT -5. The time now is 07:05 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