链接器无法在使用 libbsd 的 Ubuntu 上找到 strnstr()

问题描述 投票:0回答:1

要将一些现有代码从 FreeBSD 移植到安装了 libbsd-dev 软件包的 Ubuntu (22.04),我在链接阶段遇到了问题:

undefined reference to 'strnstr'
。代码:

#include <bsd/string.h>
#include <stdio.h> 

int main(int argc, char **argv) {

    char * ppl = "World citizens";

    char * s = strnstr(ppl, "c", strlen(ppl));

    printf("Hello, %s\n", s);
}

用于构建和链接的命令是:

cc -lbsd test-strnstr.c
。该命令调用 gcc。

如果使用 clang (

clang -lbsd test-strnstr.c
) 显式调用,它就可以工作。它还可以在 MacOS 上编译、链接和运行良好(没有第一个 #include 中的“bsd/”部分)。

它无法与libbsd中的

strnstr()
链接(并且libbsd.a安装在/usr/lib/aarch64-linux-gnu/)中的原因是什么?

ubuntu gcc static-linking
1个回答
0
投票

有时是因为与 GCC 链接时的 库顺序。所以我认为你需要确保库的顺序是正确的。

尝试

cc test-strnstr.c -lbsd

记住。链接器从左到右搜索。需要符号的库必须在前,然后是解析符号的库。

© www.soinside.com 2019 - 2024. All rights reserved.