0

do not understand this thing: I got a program in c and I am linking some second party static libs to that (as far as I know lhis libs can be also some wrappers on dlls) - does it can bring my program to 'implicitely' (I mean without explicit call to that in my code) run some initialisation code from whithin those libs (that will execute before my main() routine ? - or it cannot ?)

I am asking here about about c code (eventualy about c code without any c++ feature but compiled in c++ mode by c++ compiler - linked static libs can be written in any language)

tnx for answer

grunge fightr
  • 1,360
  • 2
  • 19
  • 38

2 Answers2

1

Linked libs of any sort can't run initialisation code without an explicit call from your code in either c or c++.

Paul Evans
  • 27,315
  • 3
  • 37
  • 54
1

It depends on your platform.

If you use GCC, you can have function declared with __attribute__((constructor)). This function WILL be called before your main, even from dynamic library.

__attribute__((constructor))
void my_init()
{
    /* do stuff; */
}

You can find more details in GCC documentation and in this SO question

There's some way how to do it in VC as well, even though not as simple. (See this SO question)

EDIT: If you link to some third party library, it may call some init function. Even if the library is in C. And there's no portable and generic way, how to detect it. And you probably don't want to mess with that as the library may depend it it being called before main starts.

If you really want to find out if it calls something, you'd have to look inside the binary. In ELF files, there's a section .init_array containing "pointers" to functions that will be invoked at start and it can be dumped using objdump (objdump -s -j .init_array <binary>)

I assume there's similar section in PE files in windows, but I never worked with those, sorry.

EDIT2: The main() function starts your code. But there's stuff to do even before it's executed. When you compile your program, the compiler adds some code that is executed before the main() and initializes environment for program (stack, C library...).

Under Linux this will be provided mainly by functions _start and _init. And as a feature, you can instruct it the compiler to call some your function(s) as well, inside the _init function.

Dynamic library won't have the _start function, but there's still _init that will be called when the library's loaded. And a call to some user function can be included there as well.

In the case of static library it gets a bit more complicated, as the linker may remove some functions, when they're never called from you program. But once it's called (even indirectly from the library code) or just referenced somewhere in the library and never actually called, it will end up in you binary and will be called before main().

Some info about _start and _init can be found here.

Under windows the compiler and linker is different, but it should work in a similar way.

Community
  • 1
  • 1
v154c1
  • 1,698
  • 11
  • 19
  • I am asking from perspective of main program coder using external libs - so can i be sure that linked lib inits are not called or I can suspect they are - Is there a way to check it? – grunge fightr Aug 14 '13 at 15:23
  • If the implementation is a conforming C++ implementation, constructors for all objects used in the program, including those in library files, must be called before `main` starts. I can imagine Windows having conformance bugs here like it does in most other places, but if you're using gcc or icc rather than MSVC, I really doubt they would get something this important wrong. – R.. GitHub STOP HELPING ICE Aug 14 '13 at 15:26
  • I am speaking more about c program (but compiled in c++ mode) and linking many small c-like libraries - what with that?, will they call inits or not? - I do not understand your answer :C – grunge fightr Aug 14 '13 at 16:13
  • See the edited answer. TL;DR: Initialization code may get called, even before your main(). – v154c1 Aug 14 '13 at 16:48
  • sadly i do not fully understand how it works and why it is called even in c – grunge fightr Aug 19 '13 at 16:37
  • I edited the answer to explain how it works, hope it will help. – v154c1 Aug 19 '13 at 18:15