从 LLVM 位码编译对象时无法链接到静态库。

标签 c llvm clang

我正在开发一个 LLVM 编译器 channel 。我通过以下方式运行通行证:

  1. 编译为 LLVM 位码

    clang  foo.c -emit-llvm -c -o foo.bc
    
  2. 通过opt运行foo.bc(没有这一步还是报错)

  3. 编译回目标文件

    clang  -c -o foo.o foo.bc
    
  4. 现在 foo.o 可能是静态库的一部分。

    ar rc libfoo.a foo.o
    
  5. 当我的所有 c 文件都以这种方式编译时,我无法链接到 libfoo.a。

    clang libfoo.a linkme.o -o linkme
    
    linkme.o:linkme.bc:function main: error: undefined reference to 'foo'
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    

源文件:

foo.c:

int foo(int a)
{
    return a;
}

foo.h

int foo(int a);

链接我.c

#include "foo.h"

int main(int argc, char *argv[])
{
   foo(6);

   return 0;
}

最佳答案

现在我觉得很傻。它与 .bc 文件无关,只是参数的顺序。

作品:

clang  linkme.o  libfoo.a -o linkme

失败:

clang  libfoo.a linkme.o -o linkme

关于从 LLVM 位码编译对象时无法链接到静态库。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14613091/

相关文章:

c - 当使用 -Wmissing-prototypes 时,如何抑制 GCC 对中断例程没有原型(prototype)的警告?

llvm - 构建 LLVM 会耗尽所有 RAM

compiler-construction - 绑定(bind) LLVM 3.8.4 no getGlobalContext

c++ - 为什么 Clang 将显式强制转换解释为隐式强制转换?

c - 如何编辑和使用包含双引号和转义字符的字符串?

python - 使用 swig 包装自定义哈希表

c - C中每次将一对数字存储在一个一维数组中

object - 如何使用 LLVM 实现动态对象?

c++ - 使用 lldb 和 -fsanitize=address 调试问题

c++ - cmake 和 libcxx 的链接器错误