LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 12-13-2007, 06:58 AM   #1
golden97
LQ Newbie
 
Registered: Nov 2007
Location: Rochester, Minnesota, USA
Posts: 1

Rep: Reputation: 0
What is the correct way to write a working shared library in C++?


Hi, I am wondering how to write a shared library in C++.
I already know a little bit about the dlopen() API.
How do you get shared libraries to work with programs written in C++?
 
Old 12-14-2007, 04:01 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Well, there are two ways to use shared libraries (regardless of whether you’re dealing with C or C++): dynamic linking and dynamic loading. The easiest of the two (both conceptually and in terms of practical use in C++) is dynamic linking.

When you create a shared object for dynamic linking, it is much like creating a static object archive for static linking (a static archive is just an amalgam of object files into a single object file). When you use it, from a program, you place it in the library search path and pass the soname to the linker (e.g., “-lfoo” would dynamically link to the shared object libfoo.so or the static object archive libfoo.a). You can use the same classes and functions as if they were in a different object, the only thing is that they will not be resolved until you begin to run the program. As long as any name mangling is the same in both the library and the program using it, nothing bad should happen. Notice that the actual linking occurs at compile time, but symbol resolution occurs at runtime.

Dynamic loading is more difficult (at least in C++). This sort of shared object use occurs for example if you are using functions from a “plugin” or something. Instead of linking at compile time, you actually instruct your program to load a shared object during runtime (this is not done by ld.so at initialization, but by your program itself during any point in its execution). The API used here is usually dlopen(). The problem is that you give dlopen() a symbol’s name to resolve. The symbol’s name has most likely been mangled from the original name, and it is not usually useful to try to guess the mangling (which happens to be implementation-specific, and may even vary between different versions of implementations). The easiest way to overcome this is to explicitly disable name mangling when creating the shared object (i.e., by using “extern "C"”). The problem here is that you lose things like the ability to overload. The other problem is with classes (which are not covered by dlopen()). In the case of classes, you most likely need to create separate “helper” or “factory” functions whose sole purpose is to instantiate and return a class, and then delete/clean up the class when it’s finished.
 
  


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
list library function of a shared library .so powah Linux - General 7 10-25-2011 04:47 AM
Making a static library from a given shared library vro Programming 1 07-27-2007 04:07 PM
LINUX - linking archive (static library) with shared (dynamic) library gurkama Programming 5 03-04-2007 11:11 PM
How to write makefile to create shared library in 2.6 kernel yogeshwar_s Linux - Software 1 12-29-2004 12:39 AM
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 04:32 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
Open Source Consulting | Domain Registration