使用gcc是否可以链接库,但前提是存在该库?

时间:2018-10-31 14:18:27

标签: c++ gcc g++ shared-libraries

我们有一些混乱的makefile系统,我们将对其进行修复。但是与此同时,我需要添加一种解决方法,如果您可以要求编译器(或链接器)链接库(但前提是它存在),那我就在徘徊。我知道如何修复Makefile,但这需要一些时间,与此同时,我想快速进行破解...

所以我有点像:

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

libsomelibrary.so不存在时,将产生错误。在这种情况下,我希望它继续而不链接。那可能吗? -一些链接器选项?

2 个答案:

答案 0 :(得分:1)

您可以替换

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

在Makefile中使用

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

作为旁注,您可以简单地-L ./some/path -l somelibrary来代替./some/path/libsomelibrary.so

答案 1 :(得分:0)

make程序对被调用程序(在本例中为gcc)的返回值作出反应。如果返回0,则表示成功。所有其他值均视为错误。因此,您可以调用bash脚本进行链接作为中间解决方案。无论如何,只要让bash脚本返回0。