Can I dynamically link a library with static initialization code?

  • Thread starter Thread starter Hurkyl
  • Start date Start date
AI Thread Summary
The discussion revolves around linking object files in a UNIX environment, specifically addressing issues with static initialization when using libraries. The user successfully links three object files—program.o, public.o, and private.o—directly, but encounters problems when attempting to link program.o with a library containing public.o and private.o. The static initialization code in private.o does not execute when linked through the library. Suggestions include using the -static option during compilation to include the library's code directly in the executable, which may resolve the issue by ensuring that the static initialization code is called. The user is encouraged to try the command g++ -static program.o libtest.a to see if it resolves the problem.
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
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top