【问题标题】:Compiles in clang but not gcc?在clang中编译但不是gcc?
【发布时间】:2014-11-25 17:38:53
【问题描述】:

我创建了a library,我正在尝试为它创建一个测试客户端来测试我的 Debian 软件包。此测试是在 Ubuntu 14.04 上完成的。

我安装了the binary 和the 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 的行为。

【问题讨论】:

  • 检查你的 gcc 版本。可能已经过时了。

标签: c gcc shared-libraries clang ld


【解决方案1】:

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

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

另请参阅this 和that。

【讨论】:

  • 这行得通。我知道 gcc 可能对参数很挑剔,但我一直认为源文件必须放在最后。 gcc -Wall -std=c11 main.c `mysql_config --libs --cflags` `pkg-config --libs --cflags cquel` 工作。
  • 使用 gcc,在链接时,库必须在目标文件之后列出
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-02
  • 1970-01-01
  • 2020-06-03
  • 1970-01-01
  • 2022-10-18
  • 1970-01-01
  • 2017-10-12
相关资源
最近更新 更多