我试图通过C++ API在llvm中实现嵌套函数。在执行语义检查(以确保将要编译的程序具有正确的嵌套引用等)之后,我在llvm中将所有函数定义在同一个作用域中,并将所有变量设置为全局变量。我原以为这会起作用,但我发现自己在使用命令从.o文件获得可执行文件时遇到了很大的问题:
"llvm-as-3.8 output.ll| llc-3.8 -filetype=obj | clang-3.8 ../library/library.a -v -o out"通过对全局变量使用llvm::GlobalValue::WeakAnyLinkage或llvm::GlobalValue::ExternalLinkage链接,我得到:
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info):重新定位0的符号索引11无效 /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info):重新定位1的符号索引12无效 /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info):重新定位8的符号索引12无效 ..。 /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line):重新定位0的符号索引2无效 函数/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In
\_start': (.text+0x20): undefined reference tomain‘
这似乎与main的定义有关,在我的ir代码定义中,main的定义如下:
; Function Attrs: nounwind uwtable define void @main() #0 {
问题是,当我不使用全局变量时,相同的命令没有任何问题,当然,除了在子函数中使用变量的情况外,这就是我试图将alloca更改为全局变量的原因。
帮助找到解决这个问题的方法。提前谢谢。Note1:我使用llvm-3.8
更新:当我从clang命令中删除-v时,结果是:
refined_output.o:在函数
bsort': refined_output.ll:(.text+0x4): undefined reference tox‘refined_output.ll:(.text+0xa)中:未定义的对n' refined_output.ll:(.text+0x10): undefined reference to的引用更改了refined_output.ll:(.text+0x22):未定义的对i' refined_output.ll:(.text+0x2c): undefined reference to更改的引用: refined_output.ll:(.text+0x42):未定义的对i' refined_output.ll:(.text+0x48): undefined reference ton’refined_output.ll的引用:(.text+0x50):未定义的对i' refined_output.ll:(.text+0x59): undefined reference tox‘refined_output.ll的引用:(.text+0x60):未定义的对i' refined_output.ll:(.text+0x75): undefined reference tox’refined_output.ll的引用:对i' refined_output.ll:(.text+0x95): undefined reference to的引用更改为“refined_output.ll:(.text+0xa3):对changed' refined_output.o: In function交换的未定义引用”:refined_output.ll:(.text+0xc3):未定义的对x.1' refined_output.ll:(.text+0xca): undefined reference toy‘refined_output.ll的引用:(.text+0xd1):未定义的对x.1' refined_output.ll:(.text+0xd9): undefined reference tot’refined_output.ll的引用:(.text+0xe0):未定义的对x.1' refined_output.ll:(.text+0xe7): undefined reference toy‘refined_output.ll的引用:(.text+0xf2):未定义的对y' refined_output.ll:(.text+0xf8): undefined reference tot’refined_output.o的引用output.ll:(.text+0x10c):未定义的对seed' refined_output.ll:(.text+0x123): undefined reference tox.2‘refined_output.ll的引用:(.text+0x12a):未定义的对i.4' refined_output.ll:(.text+0x130): undefined reference to种子’refined_output.ll的引用:(.text+0x15d):未定义的对seed' refined_output.ll:(.text+0x166): undefined reference toi.4‘refined_output.ll的引用:(.text+0x16c):未定义的对i.4' refined_output.ll:(.text+0x177): undefined reference tox.2’refined_output.ll的引用:(.text+0x18d):未定义的对x.2' refined_output.ll:(.text+0x19e): undefined reference tox.2‘refined_output.o的引用:在functionprintArray': refined_output.ll:(.text+0x1c4): undefined reference tomsg’refined_output.o中:(.text+0x1cb):未定义的引用x.2' refined_output.ll:(.text+0x1d1): undefined reference ton.3‘refined_output.ll:(.text+0x1d8):未定义的对msg' refined_output.ll:(.text+0x1e3): undefined reference toi.4’refined_output.ll的引用:(.text+0x1f3):未定义的对x.2' refined_output.ll:(.text+0x1fa): undefined reference toi.4‘refined_output.ll的引用:(.text+0x208):未定义的对i.4' refined_output.ll:(.text+0x20e): undefined reference toi.4’refined_output.ll的引用:(.text+0x214):未定义的对n.3' refined_output.ll:(.text+0x21c): undefined reference toi.4‘的引用 clang: error:链接器命令失败,退出代码1(使用-v查看调用)
在llvm中,在我的程序开始时将所有这些变量定义为:
@x = external global i32*
@n = external global i32
@i = external global i32
@changed = external global i1
@x.1 = external global i32*
@y = external global i32*
@t = external global i32
@msg = external global i8*
@x.2 = external global i32*
@n.3 = external global i32
@i.4 = external global i32
@const_string_temp = private constant [3 x i8] c", \00", align 1
@const_string_temp.5 = private constant [2 x i8] c"\0A\00", align 1
@i.6 = external global i32
@x.7 = external global i32
@seed = external global i32
@const_string_temp.8 = private constant [16 x i8] c"Initial array: \00", align 1
@const_string_temp.9 = private constant [15 x i8] c"Sorted array: \00", align 1我希望这能帮我--或者在我的IR代码生成中找到问题。
我期待着..。
发布于 2016-08-28 15:56:40
@x = external global i32*等等是全局变量声明,而不是定义。因此,“未定义引用”错误是有效的,您确实需要定义它们。
有关更多信息,请参见http://llvm.org/docs/LangRef.html#global-variables
还请注意,llvm/ llc部件是多余的- clang可以编译.ll / .bc文件。
https://stackoverflow.com/questions/39172236
复制相似问题