code_fodder
code_fodder

Reputation: 16331

With gcc is it possible to link library but only if it exists?

We have a bit of a messed up makefile system which we are going to fix up. But in the mean time I need to add a workaround, and I am wandering if you can ask the compiler (or linker) to link a library, but only if it exists. I know how to fix the makefile but that will take some time and in the meantime I want a quick hack...

So I have somthng like:

gcc <...other options...> -L ./some/path -l somelibrary

When libsomelibrary.so does not exist this gives an error. I want it to continue in this case without linking. Is that possible? - some linker option?

Upvotes: 2

Views: 712

Answers (2)

tangoal
tangoal

Reputation: 769

The make program reacts on the return value of the program called (in this case gcc). If it returns 0, then it is successful. All other values are considered as error. So you could just call a bash script doing the linking as intermediate solution. Just let the bash script return 0 in any case.

Upvotes: 0

yugr
yugr

Reputation: 21878

You can replace

gcc <...other options...> -L ./some/path -l somelibrary

in Makefile with

gcc <...other options...> -L ./some/path -l somelibrary || gcc <...other options...> -Wl,--unresolved-symbols=ignore-in-object-files

As a side note, instead of -L ./some/path -l somelibrary you can simply do ./some/path/libsomelibrary.so.

Upvotes: 1

Related Questions