C Clang static library symbol woes

Hi there everyone!
I have a problem. I have an application and a shared object. Now I know applications normally call functions in shared objects, but I’m doing the opposite - the shared object needs to call a function in the main program.
I must first stress that this works perfectly on FreeBSD with clang and also on Linux with gcc.
So the problem comes when I take the seemingly innocuous step of moving the called function into a static library created with the ar utility. Suddenly the shared object cannot find the function in the main program. I’ve not found this exact problem on stackoverflow but I tried a bunch of other stuff from there (—whole-archive and -fvisibility=default in particular) with no success.
Can anyone help?
 
Hi there everyone!
I have a problem. I have an application and a shared object. Now I know applications normally call functions in shared objects, but I’m doing the opposite - the shared object needs to call a function in the main program.

Also known as "callback", right?

So the problem comes when I take the seemingly innocuous step of moving the called function into a static library created with the ar utility. Suddenly the shared object cannot find the function in the main program.

Now there's also a static library; could you please elaborate what exactly you are doing, or provide a simple test case?
 
Hi,
In writing a test case for you I figured out what was wrong. The compiler doesn’t know that the .so will call a function in the static library, so it optimises the static library out completely. Putting a call in “main” to some function in the static library fixes everything. I hate it when programs think they know best like this.
Thanks anyway!
 
Note: if you want to force the compiler to include code that doesn’t appear to be in use, just make it an object file, not an archive. Then the compiler munches it quite happily.
 
How can your static library know what's in use?

The linker is a one pass linker, so the order of the library files you specify matters.

You can check what's in a library with nm, add -C for C++ demangling.
 
Back
Top