Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

force visual studio to link all symbols in a lib file

Tags:

Is there any way to force visual studio to link all symbols from a lib file into the dll as atm it is optimizing "unused" functions which are needed by the program using the dll at run time.

I tried using the /OPT:NOREF and /OPT:NOICF but they dont seem to work.

The reason i need them is because they are global class which register them selves with a controller and they are not being linked in the dll.

like image 297
Lodle Avatar asked Mar 01 '09 01:03

Lodle


2 Answers

I don't know if there's a more elegant way in Visual Studio, but the cross-platform solution we use it to have two macros that force the problamatic object file to be linked.

One is placed in the source file of functions that are being excluded, the other is placed in a function that the linker knows will be called.

Something like;

#define FORCE_LINK_THIS(x) int force_link_##x = 0;  #define FORCE_LINK_THAT(x) { extern int force_link_##x; force_link_##x = 1; } 

It's not exactly elegant, but we haven't found a better solution that works across platforms.

like image 198
Andrew Grant Avatar answered Sep 20 '22 18:09

Andrew Grant


There is actually a half-official solution to it, and here it is.

TL;DR:

'Use library dependency inputs'.

In VS lingo 'library dependency inputs' is a name for the obj files that constitute a library. You can actually control this behaviour in two scopes:

  1. Per refernce: by the 'use library dependency inputs' combo in the reference properties. This is the solution I used personally, and the one mentioned in the post. Use Library Dependency Inputs

  2. Per the entire executable: in the exe project properties /C++/Linker/General/ Use library dependency inputs -> Yes

The historical motivation for these arcane settings is enabling incremental linking in places it wasn't available before, but it has the useful side effect of linking directly against the obj files that are packaged in a lib, thereby constructing also unreferenced global objects.

like image 34
Ofek Shilon Avatar answered Sep 18 '22 18:09

Ofek Shilon