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.

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).

You can use the --undefined option when you build B: g++ -Wl,--undefined,Af -o libB. So ...

Try putting those lines into B/initB. Cpp so that they're (hopefully) forced into the libB. So library at link time.

But why do you have to do it in this way at all? Can't you set it up so that the executable references that function (or a caller of it), causing the linker to do the right thing automatically?

C is actually a scripting language frontend, B is the language engine, and A is a set of native code methods for the engine to use. We're implementing a pre-existing language that has a well-defined foreign function interface. A and B are produced by separate teams; we'd prefer to keep everything that the A-team writes in the A directory.

– Brian Bassett Jun 7 '10 at 20:02.

If you can use C++0x features of gcc (-std=c++0x), then the function default template arguments may do the trick. As of the current c++ standard, default arguments are not allowed for function templates. With these enabled in c++0x, you can do something like :- In some header file of static library ... template void Af() { } Then in its corresponding cpp file use explicit template instantiation... template void Af(); This will generate the symbols for the function Af though it is not yet called/referenced.

This won't affect the callers due to the fact that because of the default template argument, you need not specify a type. Just add the template before the function declaration and explicitly instantiate it in its implementation file. HTH.

Unfortunately, this won't work because Af is actually a Fortran procedure. – Brian Bassett Jun 7 '10 at 20:16 Well then, make a 'C' wrapper that calls Af_(...) you can call fortran functions from 'C' i.e. Function name appended by underscore; google nitty gritty details for arguments, I donno abt C++ calling fortran.

Call this 'C' wrapper from C++. BTW, thats how one of the leading financial company that I worked for, managed its antique/crappy code :-) – Abhay Jun 7 '10 at 20:23.

MSVC #pragma comment(linker, "/include:__mySymbol") gcc -u symbol RTFM.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions