0

I need to compile and, most importantly, link a C program that uses a proprietary function present in a shared library file. Because of lack of communication with the previous development team, there is no proper documentation. I declared a function prototype (because I know the number and type of arguments):

int CustomFunction(unsigned char *in, int size);

Since that function name can be grepped from /customlibs/libcustom.so, I tried to compile the code and link it like this:

gcc -L/customlibs testing.c -o testing -lcustom

Which throws a few error messages looking like this:

/customlibs/libcustom.so: undefined reference to `AnotherCustomFunction'

Obviously, I need to tell linker to include other libraries as well, and, to make things worse, they need to be in certain order. I tried exporting LD_LIBRARY_PATH, using -Wl,-rpath=, -Wl,--no-undefined and -Wl,--start-group. Is there an easy way to give the linker all the .so files without the proper order?

Ulrik
  • 1,131
  • 4
  • 19
  • 37

3 Answers3

1

I found the solution (or a workaround) to my problem: adding -Wl,--warn-unresolved-symbols, which turns errors to warnings. Note that this works only if you are ABSOLUTELY certain your function does not depend on the symbols mentioned in undefined refernce to: messages.

Ulrik
  • 1,131
  • 4
  • 19
  • 37
0

Add them on the command line is a way to do it. Something like this below. The LD_LIBRARY_PATH tells gcc where to look for libraries, but you still need to say what libraries to include.

gcc -L/customlibs testing.c -o testing -lcustom -lmylib1 -lmylib2 -lmylib3

jhagen
  • 62
  • 2
  • I tried that, and it complains about more undefined references – Ulrik Mar 29 '17 at 02:06
  • 3
    Ulrik, Which references? You should add more libs... And you may try `ldd` command on every shared library to extract which libraries it depend on, and then use [topological sort](https://en.wikipedia.org/wiki/Topological_sorting) or just try to add them all to the link command ([between `-(` and `-)` option pair](http://stackoverflow.com/a/5651895/196561)). – osgx Mar 29 '17 at 02:09
  • When I add all the shared libraries with `-Wl,--start-group -lcustom1 -lcutom2 ... -Wl,--end-group` I get even more messages like `/customlibs/libcustom.so: undefined reference to `AnotherCustomFunction'` – Ulrik Mar 29 '17 at 02:34
0

You should also include all the header files of your shared library by adding the -I option of gcc, for example : gcc [...] -I/path/to/your/lib/header/files [...]

Fryz
  • 2,119
  • 2
  • 25
  • 45