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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-16-2011, 09:31 AM
|
#1
|
LQ Newbie
Registered: Oct 2007
Location: Charlotte, NC
Distribution: Ubuntu 10.04, Davinci (MacOS/X)
Posts: 20
Rep:
|
__attribute__ ((constructor)) function does not execute OR where's an example source
My setup is Ubuntu 10.04, eclipse 3.5.2
I have created the source for a shared object and a test program to test it. The program executes dlopen() and I get back a valid library handle. I can use that handle and dlsym() to get the address of my C exported functions just as I expect.
I recently added a change to have the so instantiate an object at load time by using a __attribute__ ((constructor)) function named so_init(). I also added a main() function (one wasn't required before). Adding a bunch of printf()s and using the debugger in eclipse I watch the code run and the so_init() and main() functions are never called.
As far as I can tell, eclipse has -shared, -fPIC and -Wl setup and specified correctly.
Am I missing a compile/link flag? Is there some magic I need in the so_main.cpp to get this working.
FWIW, I have this written as dual source code. The same code works quite nicely in Windows which calls dllMain() as expected.
Wes
Last edited by micropanther; 03-16-2011 at 09:33 AM.
|
|
|
03-19-2011, 01:33 AM
|
#2
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078
Rep: 
|
Can you post a short piece of code that can reproduce the problem? A makefile would also be helpful.
Kevin Barry
|
|
|
03-21-2011, 08:00 AM
|
#3
|
LQ Newbie
Registered: Oct 2007
Location: Charlotte, NC
Distribution: Ubuntu 10.04, Davinci (MacOS/X)
Posts: 20
Original Poster
Rep:
|
Here is the So part. I actually have two so's but their setups are identical.
EDIT: to add .h items
Code:
soMain.h
void __attribute__ ((constructor)) so_init(void); // Linux entry point
void __attribute__ ((destructor)) so_fini(void); // Linux exit point
Code:
soMain.cpp
int main( void )
{
printf( "MAIN WAS RUN/n" );
}
void so_init()
{
printf( "SO_INIT WAS RUN/n" );
}
void so_fini()
{
printf( "SO_FINI WAS RUN/n" );
}
Here is the call from test.cpp:
Code:
dlerror(); // Call dlerror() to clear any previous error code
hLib = dlopen( m_libName.c_str(), RTLD_NOW | RTLD_GLOBAL );
m_ProcAddr_LibObject = (PROCADDR_LIBOBJECT)GetFuncAddress( "GetLibraryObjectPtr" );
As for the makefile. This is Eclipse so it/they are splattered all over the place. Tell me what you need to see or that I need to search for. We'll end up using pastebin.com since LinuxQuestions won't accept the extensions associated with make.
Wes
Last edited by micropanther; 03-21-2011 at 02:12 PM.
Reason: Add lines from .h file
|
|
|
03-23-2011, 09:23 AM
|
#4
|
LQ Newbie
Registered: Oct 2007
Location: Charlotte, NC
Distribution: Ubuntu 10.04, Davinci (MacOS/X)
Posts: 20
Original Poster
Rep:
|
New Details
Interesting new detail.
I actually have two SO's I'm building. They started out nearly identical. Discover.so is the one that doesn't find so_init, etc. Edge.so works just fine. Nearly as I can tell their makefiles are "identical", but they are in Eclipse and who knows what Eclipse is hiding.
Wes
|
|
|
04-13-2011, 09:45 AM
|
#5
|
LQ Newbie
Registered: Oct 2007
Location: Charlotte, NC
Distribution: Ubuntu 10.04, Davinci (MacOS/X)
Posts: 20
Original Poster
Rep:
|
It turns out that the behavior I was seeing is caused by my libraries being statically linked rather than dynamically (at runtime). Because the libs were statically oaded before the main program, and both had an so_init(), the first one found was executed twice, once per lib load, the second one just being the wrong so_init.
No I have to figure out how to get dynamic linking to work!
Wes
|
|
|
All times are GMT -5. The time now is 10:03 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|