Object file from .a is not included in .so

I created a .c file that is converted to a .o file along with about 300 other .c files and is included in the .a static library. This library, along with many others, is used to create the .so dynamic library. When analyzing .a and .so files with nm I found that for some reason the characters defined in the .c file are present in the .a file, but not in the .so file. I do not think that this is not so. Can someone please help me here? The steps used to create two binary files:

 gcc -fvisibility=hidden -c foo.c -o foo.co ar cr libbar.a foo.co ... gcc -fvisibility=hidden -fPIC -o libfinal.so libbar.a xo ya ... 

The reason I hide visibility is because I want to set only a few selected characters. To output characters from foo.c , I specified a visibility attribute so that the signature functions in the foo.h header look like this:

extern int _____attribute_____ ((visibility ("default"))) func ();

EDIT: nm libbar.a | grep Ctx team nm libbar.a | grep Ctx nm libbar.a | grep Ctx gives:

 000023c5 T CtxAcquireBitmap 000026e9 T CtxAcquireArray 00001e77 T CtxCallMethod 

However nm libfinal.so | grep Ctx nm libfinal.so | grep Ctx nothing.

UPDATE: Found another post discussing the use of the --whole-archive option. Also, I came across the --export-dynamic option, which apparently tells the linker to save characters without links. Consequence

+4
source share
3 answers

Creating a dummy link for required characters in my main file did not solve the problem. The indicated characters appeared in a binary dump (obtained using nm ) with the marker U (= undefined). I managed to solve the problem by linking the object file directly when creating the .so file, rather than pasting it into the .a library. Since these functions were marked extern , they were included in .so , although they were not referenced in the library. If they were not extern marked, they would not be included, as sylvainulg said.

Thanks to Dmitry for specifying the --whole-archive option. I did not know that such an option exists.

-1
source

Try using the --whole-archive linker option to include all objects in your shared library when linking

 gcc -o libfinal.so -Wl,--whole-archive libbar.a xo ya -Wl,--no-whole-archive 

From man ld :

- all-archive
For each archive specified on the command line after the -whole-archive option, include each object file in the archive in the link, rather than search the archive for the necessary object files. This is usually used to turn an archive file into a shared library, forcing each object to be included in the resulting shared library. This option can be used several times.

Two notes when using this option from gcc: firstly, gcc does not know about this option, so you need to use -Wl, -all-archive. Secondly, do not forget to use -Wl, -no-whole-archive after the list of archives , because gcc will add its own list of archives to yours and you may not want this flag to affect them either.

+4
source

As far as I know, when compiling with .a, gcc pull out only objects referenced by other modules. If your intention is to include all .a content in .so, the simple "compile / link xc in libfinal.so using the contents in libbar.a" is not what you want.

+1
source

All Articles