Can I dynamically link a library with static initialization code?

  • Thread starter Thread starter Hurkyl
  • Start date Start date
Click For Summary
SUMMARY

The discussion centers on the challenge of dynamically linking a library containing static initialization code in a UNIX environment. The user successfully links object files using the command g++ program.o public.o private.o, but encounters issues when attempting to link with a static library created using ar crsu libtest.a public.o private.o. The static initialization code in private.o does not execute when linking with the library using g++ program.o -L. -ltest. The solution proposed is to use static linking with the command g++ -static program.o libtest.a, which should ensure that the static initialization code is executed.

PREREQUISITES
  • Understanding of UNIX command line operations
  • Familiarity with C++ compilation and linking processes
  • Knowledge of static vs. dynamic linking
  • Experience with the ar command for creating static libraries
NEXT STEPS
  • Research the -static option in g++ for static linking
  • Explore the differences between static and dynamic libraries in C++
  • Learn about static initialization in C++ and its implications
  • Investigate the ar command options for managing static libraries
USEFUL FOR

C++ developers, UNIX system programmers, and anyone involved in library management and linking in C++ applications.

Hurkyl
Staff Emeritus
Science Advisor
Gold Member
Messages
14,922
Reaction score
28
Here's my (UNIX) situation.

I have three object files:

program.o
public.o
private.o


public.o is "standalone" -- it is entirely self-contained.

program.o refers to functions defined in public.o

private.o is "hidden" -- neither program.o nor private.o make any reference to anything contained in private.o. However, private.o contains some static initialization that calls functions in public.o. (Essentially registering itself with the features that public.o provides)


Linking them together is no problem. I simply do:

g++ program.o public.o private.o

and a.out does exactly what I expect it to do.


Then, I tried a variation. I put public.o and private.o into a library. I think the command was

ar crsu libtest.a public.o private.o


Now, I link program.o with this library

g++ program.o -L. -ltest

This doesn't work, because private.o's startup code is never executed.

I also tried

g++ program.o libtest.a

and got the same result.



Can I do anything to have this all behave like I want? (But without having program.o or public.o reference anything in private.o?)
 
Technology news on Phys.org
Have you tried using -static?

- Warren
 
I'm not familiar with that option. I can look it up in the man page, if you don't want to explain it further.
 
Static linking means you want to include the library's code inside your executable. The alternative is dynamic linking, where you leave the code in the library, and reference it only at runtime. I suspect that if you don't compile your library statically into your executable, the static initialization code never has any reason to be called.

Try `g++ -static program.o libtest.a` and let me know what it does.

- Warren
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 15 ·
Replies
15
Views
5K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K