Can I dynamically link a library with static initialization code?

  • Thread starter Hurkyl
  • Start date
In summary, the conversation discusses a scenario where the user is trying to link together three object files (program.o, public.o, and private.o) in order to create an executable. The first attempt is successful, but when the user tries to create a library and link it with the program.o file, the static initialization code in private.o is not executed. The solution suggested is to use the -static option when compiling in order to include the library's code in the executable.
  • #1
Hurkyl
Staff Emeritus
Science Advisor
Gold Member
14,981
26
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
  • #2
Have you tried using -static?

- Warren
 
  • #3
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.
 
  • #4
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
 

Related to Can I dynamically link a library with static initialization code?

What is the difference between initialization and lib.a?

Initialization refers to the process of setting up a program or system by defining the initial values and states of variables, functions, and other components. Lib.a, also known as a static library, is a collection of pre-compiled code that can be linked to a program during initialization to provide additional functionality.

Which one is executed first, initialization or lib.a?

Initialization is executed first, as it is the process of setting up the program before it can run. Lib.a can be linked to the program during initialization, but it is not executed until it is called upon by the program.

Can a program run without initialization or lib.a?

In most cases, no. Initialization is necessary to set up the program and define the initial values of variables and functions. Lib.a may not be necessary for a program to run, but it often provides important functionality that the program needs to function properly.

Can multiple lib.a files be linked to a single program?

Yes, multiple lib.a files can be linked to a single program. This allows the program to access a variety of pre-compiled code and functionality from different libraries.

Are both initialization and lib.a necessary for all programs?

No, not all programs require both initialization and lib.a. Some programs may only require initialization, while others may only require lib.a. It depends on the specific needs and functionality of the program.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
12
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Computing and Technology
Replies
8
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
2
Replies
54
Views
4K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
2
Replies
49
Views
10K
  • Programming and Computer Science
Replies
8
Views
12K
Back
Top