有了gcc,是否可以链接库,但前提是它存在

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

本文关键字:前提 存在 链接 gcc 是否 有了      更新时间:2023-10-16

我们有一个有点混乱的makefile系统,我们要修复它。但与此同时,我需要添加一个变通方法,我不确定是否可以要求编译器(或链接器(链接库,但前提是它存在。我知道如何修复makefile,但这需要一些时间,同时我想快速破解。。。

所以我有这样的想法:

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

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

您可以替换

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

附带说明一下,您可以简单地执行./some/path/libsomelibrary.so,而不是-L ./some/path -l somelibrary

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