LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 04-06-2016, 08:14 PM   #1
dayalan_cse
Member
 
Registered: Oct 2006
Posts: 132

Rep: Reputation: 15
Help Me: gcc constructor / destructor is not working


Hi All,


I had implemented my_init (gcc constructor) and my_fini (destructor) and compiled it in to shared object (i.e. temp.so file).


I was expecting that the following my_init and my_fini methods will be executed but those methods are not executing for some reason when my ELF is trying to load this temp.so using dlopen.

Code:
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>

void main()
{
  void *handle;

  handle = dlopen("./temp.so", RTLD_LAZY | RTLD_GLOBAL);

  if ( !handle ) {
	  fprintf(stderr, "Error in opening .so file\n");
	  exit(1);
  }

  dlclose(handle);
}

Constructor / Destructor code:
-------------------------------

#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>

void __attribute__ ((constructor)) my_init(void)
{

  fprintf(stderr, "start of my_init constructor called\n");
  
 
  fprintf(stderr, "end of my_init constructor called:\n");
}

void __attribute__ ((destructor)) my_fini(void)
{
  fprintf(stderr, "my_fini destructor called\n");
}

Could you please share your views on this issue?
 
Old 04-06-2016, 09:30 PM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Quote:
Could you please share your views on this issue?
You're not holding your mouth right.

Seriously, give the section 'Dynamically Linked "Shared Object" Libraries' a read and see if you can figure it out: http://www.yolinux.com/TUTORIALS/Lib...ndDynamic.html

Post back if you have problems.
 
Old 04-07-2016, 11:01 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Probably more useful:

https://gcc.gnu.org/onlinedocs/gcc/C...ion-Attributes
Quote:
The constructor attribute causes the function to be called automatically before execution enters main (). Similarly, the destructor attribute causes the function to be called automatically after main () completes or exit () is called. Functions with these attributes are useful for initializing data that is used implicitly during the execution of the program.

You may provide an optional integer priority to control the order in which constructor and destructor functions are run. A constructor with a smaller priority number runs before a constructor with a larger priority number; the opposite relationship holds for destructors. So, if you have a constructor that allocates a resource and a destructor that deallocates the same resource, both functions typically have the same priority. The priorities for constructor and destructor functions are the same as those specified for namespace-scope C++ objects (see C++ Attributes).

These attributes are not currently implemented for Objective-C.
The Program Library HOWTO at tldp.org contains further information ... and cautions ... in sections 5.2 and 5.2.1.

Also, as a general rule, I don't like to use things like this. If my library needs an initialization routine, I provide one and stipulate that it must be called by user programs at the appropriate moment (i.e. "before you try to do anything else"). Likewise finalization. It can be very unpredictable when "magic" routines actually get called, and I don't like anything at all to be unpredictable.
 
  


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
expected constructor, destructor, or type conversion before â;â token Navjot Arora Programming 4 06-28-2012 03:08 AM
Compile error: expected constructor, destructor, or type conversion before class ilnli Programming 8 07-12-2011 09:32 PM
GCC - constrain for function with __attribute__((constructor)) ? Oaks Programming 1 10-13-2009 10:57 AM
c++ template function, expected constructor, destructor, or type conversion parv Programming 18 01-25-2008 02:54 PM
Does derivated class inherit base class destructor (constructor)? kornerr Programming 2 08-23-2006 08:05 AM

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

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