C ++ Linker Problems

I have it:

a.cpp

int localfunction () { return 1; } int local_symbol = localfunction(); 

b.cpp

 void thirdfunction () {}; 

main.cpp

 void main () { thirdfunction (); } 

When I compile this into the main executable, everything works (even with optimization), and the local function is executed at startup, even if I do not call it directly.

Now, in Visual C ++ and GCC, I put a.cpp and b.cpp in a static library (.lib). the local function is no longer executed / defined.

From what I understand, the character is recognized as "not in use" and it is deleted. But that sounds weird because:

  • Why is it not deleted when I do not use the .lib file?
  • Since lib is bundled, why does the linker reset initialization code?

What I'm trying to do is have a set of startup functions in every .lib file that I use to automatically log some data. The main executable should not know which files are linked or explicitly refer to the "local function" (/ INCLUDE works, but this is not optimal)

BTW: using various VC ++ options (OPT: NOREF, etc.) does not solve the problem.

Thanks! QbProg

+1
source share
4 answers

A static library β€” basically β€” a library or archive of object files compiled from the source files of the library components.

When the linker uses the static library to resolve dependencies when creating the application, it follows the process of finding object files in the library that help it resolve any undefined characters in the program. It does not automatically include all object files in the library.

In your instance, the object file generated from main.cpp refers to third_function() . This dependency can be resolved by linking in the object file generated from b.cpp . This object file does not introduce any additional undefined characters, so the linker can (and does) stop here.

Oh, and for maximum portability, main should return int .

+3
source

When using gcc and a start function that starts β€œautomatically” before main () starts, I would just use __attribute__((constructor)) .

Perhaps there is a similar way (pragma?) For defining a function in VC ++, then you can do some macromarom preprocessor to have a common way to declare these startup functions.

+1
source

Your linker will take steps to reduce the size of the executable. It will determine that your exe does not use the "said" function and does not include it in its final generated code. Although it also depends on what kind of optimization you use in your project.

Instead, if you want to dynamically do this, the best alternative is to use LoadLibrary and create a DLL that all your programs can load, and dynamically load this function and register what you need.

0
source

You must remember the old days when size was a premium.

You have a massive math library with 10,000 different functions. Now your application uses sin () and you are referencing libm.a (or -lm).

You definitely do not want to use your application with 9999 features that you are not using. Therefore, when using static libraries, it only extracts from the library what is used (no more).

On the other hand, shared libraries are created so that all this is drawn into memory. Also (although it is not defined in any standard), most dynamic loaders will also run any static initialization code as they load. (Note that if you reference them, they usually load when the application starts).

0
source

Source: https://habr.com/ru/post/956239/


All Articles