<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: C++ shared library loading not calling constructor for global and static const members not being set in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/c-shared-library-loading-not-calling-constructor-for-global-and/m-p/4882641#M704306</link>
    <description>See message above.</description>
    <pubDate>Mon, 31 Jan 2005 10:34:05 GMT</pubDate>
    <dc:creator>Steven J. Saunders</dc:creator>
    <dc:date>2005-01-31T10:34:05Z</dc:date>
    <item>
      <title>C++ shared library loading not calling constructor for global and static const members not being set</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/c-shared-library-loading-not-calling-constructor-for-global-and/m-p/4882635#M704300</link>
      <description>We are trying to port some code that works fine on WIN32, AIX, Solaris, and Linux to HP-UX.  This shared library loads fine but we have noticed that the one global variable for a class is not getting it's constructor called.   We tried working around this with +init/+fini and with +I linker options with problems.  &lt;BR /&gt;&lt;BR /&gt;We changed the code to have the global initialized with the first call into the main entry point/C function in the shared object.  Then we noticed a related problem, all our static const members that we initialized with a value are not getting initialized, almost as if these const members are not getting setup either when the shared object loads.  &lt;BR /&gt;&lt;BR /&gt;We are using the /usr/bin/ld linker, the commercial ansic and aCC compilers on HP-UX PA-RISC 11i v1 that is brand new with the GOLDBUNDLE 11i from June 04 and with lots of new patches.   &lt;BR /&gt;&lt;BR /&gt;What are we doing wrong.  We tried building the shared object with the linker directly and with the ansic complier and then with the aCC compiler to call the linker.  No difference.  We did this because of different info in different locations in documentation about how to do it.  &lt;BR /&gt;&lt;BR /&gt;Help!</description>
      <pubDate>Thu, 27 Jan 2005 23:15:10 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/c-shared-library-loading-not-calling-constructor-for-global-and/m-p/4882635#M704300</guid>
      <dc:creator>Steven J. Saunders</dc:creator>
      <dc:date>2005-01-27T23:15:10Z</dc:date>
    </item>
    <item>
      <title>Re: C++ shared library loading not calling constructor for global and static const members not being set</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/c-shared-library-loading-not-calling-constructor-for-global-and/m-p/4882636#M704301</link>
      <description>the correct way to do this is to use only aCC for compiling AND linking. but you say you have tried it already.&lt;BR /&gt;&lt;BR /&gt;is it possible to reduce the problem to a minimal piece of code and compile/link line ? for example, this script shows you what works and what does not:&lt;BR /&gt;-----&lt;BR /&gt;[12:39:35] $ cat con.tst&lt;BR /&gt;cat &amp;gt; 1.h &amp;lt;&amp;lt; \!&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;class A {&lt;BR /&gt; private:&lt;BR /&gt;  int a;&lt;BR /&gt; public:&lt;BR /&gt; A (int i) { a=i; printf("constructor called with %d\n", i); }&lt;BR /&gt; ~A () { printf("destructor called for %d\n", a); }&lt;BR /&gt;};&lt;BR /&gt;!&lt;BR /&gt;cat &amp;gt; 1.c &amp;lt;&amp;lt; \!&lt;BR /&gt;#include "1.h"&lt;BR /&gt;static A a(5);&lt;BR /&gt;static A *b;&lt;BR /&gt;extern "C" void function () { puts("function called"); }&lt;BR /&gt;#ifdef USE_INITFINI&lt;BR /&gt;extern "C" void init () { a=A(5);  }&lt;BR /&gt;extern "C" void fini () { (&amp;amp;a)-&amp;gt;~A(); }&lt;BR /&gt;#endif&lt;BR /&gt;!&lt;BR /&gt;cat &amp;gt; 2.c &amp;lt;&amp;lt; \!&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;#include &lt;DLFCN.H&gt;&lt;BR /&gt;int main () {&lt;BR /&gt; void *dl;&lt;BR /&gt; void (*func)();&lt;BR /&gt; puts("before dlopen");&lt;BR /&gt; dl = dlopen("lib1.sl", RTLD_LAZY);&lt;BR /&gt; puts("after dlopen");&lt;BR /&gt; if (dl == NULL)&lt;BR /&gt;  return 1;&lt;BR /&gt; func = (void(*)())dlsym (dl, "function");&lt;BR /&gt; if (func != NULL) {&lt;BR /&gt;  (*func)();&lt;BR /&gt; }&lt;BR /&gt; puts("before dlclose");&lt;BR /&gt; dlclose(dl);&lt;BR /&gt; puts("after dlclose");&lt;BR /&gt; return 0;&lt;BR /&gt;}&lt;BR /&gt;!&lt;BR /&gt;aCC 1.c -b +z -o lib1.sl&lt;BR /&gt;aCC 2.c&lt;BR /&gt;echo&lt;BR /&gt;echo ---- compiled and linked with aCC ----&lt;BR /&gt;./a.out&lt;BR /&gt;&lt;BR /&gt;aCC +z -c 1.c&lt;BR /&gt;ld -b 1.o -o lib1.sl&lt;BR /&gt;echo&lt;BR /&gt;echo ---- linked plain with ld ----&lt;BR /&gt;./a.out&lt;BR /&gt;&lt;BR /&gt;aCC +z -DUSE_INITFINI -c 1.c&lt;BR /&gt;ld -b 1.o +init init +fini fini -o lib1.sl&lt;BR /&gt;echo&lt;BR /&gt;echo ---- linked with ld, with init and fini ----&lt;BR /&gt;./a.out&lt;BR /&gt;echo&lt;BR /&gt;----&lt;BR /&gt;&lt;BR /&gt;in my environment:&lt;BR /&gt;----&lt;BR /&gt;&lt;BR /&gt;[12:39:35] $ ksh con.tst&lt;BR /&gt;&lt;BR /&gt;---- compiled and linked with aCC ----&lt;BR /&gt;before dlopen&lt;BR /&gt;constructor called with 5&lt;BR /&gt;after dlopen&lt;BR /&gt;function called&lt;BR /&gt;before dlclose&lt;BR /&gt;destructor called for 5&lt;BR /&gt;after dlclose&lt;BR /&gt;&lt;BR /&gt;---- linked plain with ld ----&lt;BR /&gt;before dlopen&lt;BR /&gt;after dlopen&lt;BR /&gt;function called&lt;BR /&gt;before dlclose&lt;BR /&gt;after dlclose&lt;BR /&gt;&lt;BR /&gt;---- linked with ld, with init and fini ----&lt;BR /&gt;before dlopen&lt;BR /&gt;constructor called with 5&lt;BR /&gt;destructor called for 5&lt;BR /&gt;after dlopen&lt;BR /&gt;function called&lt;BR /&gt;before dlclose&lt;BR /&gt;destructor called for 5&lt;BR /&gt;after dlclose&lt;BR /&gt;&lt;BR /&gt;[12:39:39] $ aCC -V&lt;BR /&gt;aCC: HP ANSI C++ B3910B A.03.55&lt;BR /&gt;[12:40:57] $ /usr/sbin/swlist | grep -i linker&lt;BR /&gt;  PHSS_19866                    1.0            ld(1) and linker tools cumulative patch&lt;BR /&gt;  PHSS_28433                    1.0            linker startup code / SLLIC ELF support&lt;BR /&gt;  PHSS_30969                    1.0            ld(1) and linker tools cumulative patch&lt;BR /&gt;----&lt;/DLFCN.H&gt;&lt;/STDIO.H&gt;&lt;/STDIO.H&gt;</description>
      <pubDate>Fri, 28 Jan 2005 02:17:19 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/c-shared-library-loading-not-calling-constructor-for-global-and/m-p/4882636#M704301</guid>
      <dc:creator>ranganath ramachandra</dc:creator>
      <dc:date>2005-01-28T02:17:19Z</dc:date>
    </item>
    <item>
      <title>Re: C++ shared library loading not calling constructor for global and static const members not being set</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/c-shared-library-loading-not-calling-constructor-for-global-and/m-p/4882637#M704302</link>
      <description>Thanks, tried the simple program approach.  Now the problem seems to be that I am linking the EXE with ld.  If I link it by using cc I recreate the problem in my simple program.  &lt;BR /&gt;&lt;BR /&gt;Can I link C libraries and exes with aCC even though they are straight ansi C?</description>
      <pubDate>Fri, 28 Jan 2005 11:13:26 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/c-shared-library-loading-not-calling-constructor-for-global-and/m-p/4882637#M704302</guid>
      <dc:creator>Steven J. Saunders</dc:creator>
      <dc:date>2005-01-28T11:13:26Z</dc:date>
    </item>
    <item>
      <title>Re: C++ shared library loading not calling constructor for global and static const members not being set</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/c-shared-library-loading-not-calling-constructor-for-global-and/m-p/4882638#M704303</link>
      <description>One additional problem.  When trying to link the EXE with aCC it is not finding my shared objects but the -L pathname -lsharedobj is in there.  &lt;BR /&gt;It says:&lt;BR /&gt;/usr/ccs/bin/ld: Can't find library: "ibase"&lt;BR /&gt;&lt;BR /&gt;The -L../../hpxbin where the shared obj is located is in there and the -libase is after it.  &lt;BR /&gt;&lt;BR /&gt;Is there a problem with aCC calling the linker to make an EXE with resolving the locations of the shared objects?&lt;BR /&gt;</description>
      <pubDate>Fri, 28 Jan 2005 11:38:37 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/c-shared-library-loading-not-calling-constructor-for-global-and/m-p/4882638#M704303</guid>
      <dc:creator>Steven J. Saunders</dc:creator>
      <dc:date>2005-01-28T11:38:37Z</dc:date>
    </item>
    <item>
      <title>Re: C++ shared library loading not calling constructor for global and static const members not being set</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/c-shared-library-loading-not-calling-constructor-for-global-and/m-p/4882639#M704304</link>
      <description>You are going to run into problems with name magling if you compile with one c++ compiler and link with another. Do an nm on the library and the binary you ate trying to link it to and you see the name decorations are probably different. You can either (a) compile and link with the same c++ compiler , or (b) in the header files surround the function declarations with extern "C" { func_defs } so that they don't get name mangled.</description>
      <pubDate>Mon, 31 Jan 2005 06:38:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/c-shared-library-loading-not-calling-constructor-for-global-and/m-p/4882639#M704304</guid>
      <dc:creator>Stephen Keane</dc:creator>
      <dc:date>2005-01-31T06:38:54Z</dc:date>
    </item>
    <item>
      <title>Re: C++ shared library loading not calling constructor for global and static const members not being set</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/c-shared-library-loading-not-calling-constructor-for-global-and/m-p/4882640#M704305</link>
      <description>I have this resolved now. Thanks for all your help.  &lt;BR /&gt;&lt;BR /&gt;The problem I was having with getting the compiler to call the linker and resolve linked in libraries uisng the -L path option was because of a TYPO, +AA instead of -AA.&lt;BR /&gt;&lt;BR /&gt;Also the problem is that the C shared object that loads other shared objects that can be C or C++ has to be linked with using aCC to call the linker for it to properly call the global's constructors and so forth. &lt;BR /&gt;&lt;BR /&gt;Thanks. &lt;BR /&gt;</description>
      <pubDate>Mon, 31 Jan 2005 10:33:12 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/c-shared-library-loading-not-calling-constructor-for-global-and/m-p/4882640#M704305</guid>
      <dc:creator>Steven J. Saunders</dc:creator>
      <dc:date>2005-01-31T10:33:12Z</dc:date>
    </item>
    <item>
      <title>Re: C++ shared library loading not calling constructor for global and static const members not being set</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/c-shared-library-loading-not-calling-constructor-for-global-and/m-p/4882641#M704306</link>
      <description>See message above.</description>
      <pubDate>Mon, 31 Jan 2005 10:34:05 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/c-shared-library-loading-not-calling-constructor-for-global-and/m-p/4882641#M704306</guid>
      <dc:creator>Steven J. Saunders</dc:creator>
      <dc:date>2005-01-31T10:34:05Z</dc:date>
    </item>
  </channel>
</rss>

