LinuxQuestions.org
Visit Jeremy's Blog.
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-16-2011, 09:31 AM   #1
micropanther
LQ Newbie
 
Registered: Oct 2007
Location: Charlotte, NC
Distribution: Ubuntu 10.04, Davinci (MacOS/X)
Posts: 20

Rep: Reputation: 0
Question __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.
 
Old 03-19-2011, 01:33 AM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Can you post a short piece of code that can reproduce the problem? A makefile would also be helpful.
Kevin Barry
 
Old 03-21-2011, 08:00 AM   #3
micropanther
LQ Newbie
 
Registered: Oct 2007
Location: Charlotte, NC
Distribution: Ubuntu 10.04, Davinci (MacOS/X)
Posts: 20

Original Poster
Rep: Reputation: 0
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
 
Old 03-23-2011, 09:23 AM   #4
micropanther
LQ Newbie
 
Registered: Oct 2007
Location: Charlotte, NC
Distribution: Ubuntu 10.04, Davinci (MacOS/X)
Posts: 20

Original Poster
Rep: Reputation: 0
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
 
Old 04-13-2011, 09:45 AM   #5
micropanther
LQ Newbie
 
Registered: Oct 2007
Location: Charlotte, NC
Distribution: Ubuntu 10.04, Davinci (MacOS/X)
Posts: 20

Original Poster
Rep: Reputation: 0
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
 
  


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
GCC - constrain for function with __attribute__((constructor)) ? Oaks Programming 1 10-13-2009 10:57 AM
not calling copy constructor on function return jhorvath Programming 7 09-22-2009 12:43 PM
c++ template function, expected constructor, destructor, or type conversion parv Programming 18 01-25-2008 02:54 PM
C++, indefinite function arguments and multiple constructor passing @_@ R00ts Programming 2 04-08-2005 03:33 PM

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

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