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.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top