views:

1007

answers:

2

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.

+3  A: 

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.

Andrew Grant
Very cute. Why isn't `__declspec(dllexport)` good enough?
jeffamaphone
+1  A: 

How is the DLL going to call the functions from your lib at runtime? That sounds a bit hard to believe.

Now if users of the DLL are going to call your library functions, your question makes sense. Windows compilers (unlike Unix compilers) only export functions from a DLL if explicitly requested. The most common way to do this is to declare the function "dllexport", but you can also name the functions in a .DEF file as pass it to the linker. Note that you need to list the C++ mangled name in the .DEF file.

MSalters
because they are global class which reg them selves with a controller and they are not being linked in the dll.
Lodle
Would be useful to put that in the question, then. It's a good question, with that addition, but different from what you asked now.
MSalters