2

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?

7
  • 1
    isn't it the work of the build-system to determine which libs are available and whether to link them? Not sure how it's done in Makefiles but it's rather simple in CMake, so I'm sure it can be done there too. Commented Oct 31, 2018 at 14:22
  • Typically, when the library is linked to the application, it is done so for a reason. If it is unused by the code (so that not linking against a library will make the code to compile) - it can be removed altogether. If it is used by the code, and you remove such linker directive: you would get unresolved external symbol errors, making it not compile for another reason. Commented Oct 31, 2018 at 14:23
  • @DanM. Yes you are right. But as I was saying - I can fix the makefile but it needs a lot of work in many areas.... I was just asking the question (as a quick hack) if it can be done via gcc :) Commented Oct 31, 2018 at 14:23
  • 1
    @code_fodder you are going to change gcc invocation anyway. So you could just replace somelibrary with $SOMELIB var, and only set it if the check that such lib exists succeeds. Commented Oct 31, 2018 at 14:25
  • @DanM. ah, sorry - I see what you mean, yeah that could work :) Commented Oct 31, 2018 at 14:27

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.