Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 10-08-2009, 08:03 AM   #1
LQ Newbie
Registered: Oct 2009
Posts: 8

Rep: Reputation: 0
Question Shared object constructor not called when library loaded

OS Version: Linux red hat EL5
g++ version: 4.1.1
ld version:

I have a shared library and a unit test executable that depends on the shared library. Both compile and link without warning or error. The g++ options for the shared library are:

compile: -fPIC -Wall -Wno-unused-function -fno-strict-aliasing -g -O0
link: -shared -lstdc++

The options for the unit test executable are the same without the -shared.

In the shared library I have designated a constructor function as so:

#define ZENCORE_CONSTRUCTOR __attribute__ ((constructor))
#define ZENCORE_DESTRUCTOR __attribute__ ((destructor))

void ZENCORE_CONSTRUCTOR ZencoreInit(void);

void ZENCORE_DESTRUCTOR ZencoreCleanup(void);
However, when I run the test executable, the ZencoreInit function is not called. Rather, what I get in the stack trace is this:

#1 0x002a89b1 in global constructors keyed to _ZN3zen12g_UUIDRandomE() at zenuuiinitialize.cpp: 93
#2 0x002a8b46 in __do_global_ctors_aux() from /usr/local/lib/ngs/
#3 0x00234c2d in _init() from /usr/local/lib/ngs/
#4 0x00886f03 in call _init() from /lib/
#5 0x00887013 in _dl_init_internal() from /lib/
#6 0x0087984f in _dl_start_user() from /lib/

I compiled a test program with a similar structure and with the same compile/link options and it worked just fine. Also, another shared library that the unit test executable depends on loads the construction function correctly.

I am at a loss and appreciate any help or ideas.
Old 10-09-2009, 01:32 PM   #2
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I assume you're linking with ld because you use -lstdc++. You should link with g++ instead.
Kevin Barry

Last edited by ta0kira; 10-09-2009 at 01:35 PM.
Old 10-12-2009, 05:51 AM   #3
LQ Newbie
Registered: Oct 2009
Posts: 8

Original Poster
Rep: Reputation: 0
I'm using g++ to link. The -lstdc++ had no effect as it functions (or doesn't) the same with and without it.


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
shared library : cannot open shared object file error 40 x_gaurav Programming 2 06-28-2009 07:29 AM
unable to step through function in shared object library using gdb markhod Programming 2 01-03-2009 03:00 AM
Stopped due to shared library event - Failed to read a valid object file... tatchan Programming 3 01-16-2007 02:13 PM
Shared object library that uses another shared object library Nightfox Programming 1 02-15-2006 11:00 PM
shared object library won't load (libstdc++) sleepylight Linux - General 4 04-27-2004 05:24 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:06 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration