gpt4 book ai didi

在 clang 中编译而不是 gcc?

转载 作者:太空宇宙 更新时间:2023-11-04 07:12:41 25 4
gpt4 key购买 nike

我做了 a library ,我正在尝试为它制作一个测试客户端来测试我的 Debian 软件包。本次测试在 Ubuntu 14.04 上进行。

我安装了 the binarythe developer files及其依赖项。

这是我的测试程序的源代码:

#include <stdio.h>
#include <cquel.h>

int main(int argc, char *argv[])
{
cq_init(1024, 128);

struct dbconn mydb = cq_new_connection(u8"pattstest.delwink.com", u8"patts",
u8"patts", u8"pattsdb");

struct dlist *users;
int rc = cq_select_all(mydb, u8"User", &users, u8"");
if (rc)
return 2;

for (size_t i = 0; i < users->fieldc; ++i)
printf("%s\n", users->fieldnames[i]);

cq_free_dlist(users);
return 0;
}

该程序应该连接到测试服务器并从数据库中获取列标题(不,该服务器不是生产服务器,不需要特别安全)。

我尝试使用 gcc 进行编译:

$ gcc -Wall `pkg-config --cflags --libs cquel` `mysql_config --cflags --libs` -std=c11 main.c 
/tmp/ccjd21kP.o: In function `main':
/home/mac/c/main.c:6: undefined reference to `cq_init'
/home/mac/c/main.c:8: undefined reference to `cq_new_connection'
/home/mac/c/main.c:12: undefined reference to `cq_select_all'
/home/mac/c/main.c:19: undefined reference to `cq_free_dlist'
collect2: error: ld returned 1 exit status

我知道出事了,所以我尝试用 clang 做同样的事情:

$ clang -Wall `pkg-config --cflags --libs cquel` `mysql_config --cflags --libs` -std=c11 main.c

Clang 编译的很好!我运行了我的 a.out 二进制文件,它按预期打印了列标题。

为什么 gcc 无法链接到我的库?

编辑:我想检查我的 LD_LIBRARY_PATH 发现它是空白的。但是,将其设置为 /usr/lib/x86_64-linux-gnu(这是我的共享对象的位置)并没有改变 gcc 的行为。

最佳答案

顺序arguments gcc 很重要;你应该使用

   gcc -Wall $(pkg-config --cflags cquel) $(mysql_config --cflags) \
-std=c11 main.c \
$(pkg-config --libs cquel) $(mysql_config --libs)

另见 this & that .

关于在 clang 中编译而不是 gcc?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27133203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com