How to force inclusion of unused object definitions in a library
Well, the other answers where good attempts but ultimately fruitless. I am going to use the refit trick but the rest appears to have been a red herring; it kind of makes sense since the template in question isn't actually used anywhere else so the fact that it's not explicitly instantiated shouldn't make a difference...the declaration of the global still happens in A translation unit, it has side effects...I don't think the standard allows it to be optimized away.
The unfortunate bit about the standard not saying whether or not it is required to include a translation unit at all is the ultimate issue. I think C++0x is doing something about this but maybe not... At any rate, MS feels free to not include the unit at all, and since it doesn't the global isn't ultimately included in the executable and thus none of the other crap happens.
What I've decided to do, and there are of course many other ways, is to create a file 'tag' variable. That tag is then assigned to in a function that is globally accessible (it HAS to assign or assign from or the reference is optimized away). Then that function has to be called from the executable.
I decided to do it this way because then the rest still works the same as it always has. I don't end up ultimately changing behavior like I could if I simply wrote a registration function that hand-registered the types. Plus I can do other things this way...I just have to make sure that anything that might fall into this classification of fucktardery has a tag and that tag is accessed.
I'll be writing a bunch of helper macros to make this mostly painless.
How to force inclusion of an object file in a static library when linking into executable?
It turns out my original attempt was mostly there. The following works:
extern "C" void Af(void);
void (*Af_fp)(void) = &Af;
For those that want a self-contained preprocessor macro to encapsulate this:
#if defined(_WIN32)
# if defined(_WIN64)
# define FORCE_UNDEFINED_SYMBOL(x) __pragma(comment (linker, "/export:" #x))
# else
# define FORCE_UNDEFINED_SYMBOL(x) __pragma(comment (linker, "/export:_" #x))
# endif
#else
# define FORCE_UNDEFINED_SYMBOL(x) extern "C" void x(void); void (*__ ## x ## _fp)(void)=&x;
#endif
Which is used thusly:
FORCE_UNDEFINED_SYMBOL(Af)
How to force gcc to link an unused static library
Use --whole-archive
linker option.
Libraries that come after it in the command line will not have unreferenced symbols discarded. You can resume normal linking behaviour by adding --no-whole-archive
after these libraries.
In your example, the command will be:
g++ -o program main.o -Wl,--whole-archive /path/to/libmylib.a
In general, it will be:
g++ -o program main.o \
-Wl,--whole-archive -lmylib \
-Wl,--no-whole-archive -llib1 -llib2
How to force gcc to link unreferenced, static C++ objects from a library
You can use -Wl,--whole-archive -lyourlib
, see the manpage for ld
for more info.
Any static libraries mentioned after -Wl,--whole-archive on the command line gets fully included, you can turn this off again too if you need to , as in e.g. -Wl,--whole-archive -lyourlib -Wl,--no-whole-archive -lotherlib
Related Topics
Two Classes That Refer to Each Other
Forward Declaration with Vector of Class Type - Pointer to Incomplete Class Type Not Allowed
Getting the Size of a C++ Function
Opencv 2.4.3 iOS Framework Compiler Trouble Recognising Some C++ Headers
Compare Equality of Char[] in C
Timestamps for Embedded System
Passing Constexpr Objects Around
What Is the Reason Behind Cbegin/Cend
What Is the Recommended Way to Align Memory in C++11
How Copy from One Stringstream Object to Another in C++
C++ Static_Cast Runtime Overhead
How to Load & Call a Vbscript Function from Within C++
How to Enforce C++ Compiler to Use Specific Crt Version
Why Can't I Replace Std::Map with Std::Unordered_Map
Mixing Templates with Polymorphism
What Happens When I Assign a Number Larger Than Int_Max to an Int