Clang-3.8在从全局变量编译来自llvm字节码的目标文件时对`main'的未定义引用

问题描述:

我试图通过C++ API在llvm中实现嵌套函数。在我执行语义检查后(为了确保要编译的程序具有正确的嵌套引用等),我在llvm的同一个作用域中定义了所有函数,并将所有变量设置为全局变量。我期待这个工作,但我发现有很大的问题,而自己从.o文件将得到一个可执行文件,命令:Clang-3.8在从全局变量编译来自llvm字节码的目标文件时对`main'的未定义引用

"llvm-as-3.8 output.ll| llc-3.8 -filetype=obj | clang-3.8 ../library/library.a -v -o out" 

通过使用任一llvm::GlobalValue::WeakAnyLinkagellvm::GlobalValue::ExternalLinkage联动全局变量我得到:

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12

...

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2

/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':

(.text+0x20): undefined reference to `main'

这似乎已经与主要的定义,谁在我的红外代码的定义来做为代表:

; Function Attrs: nounwind uwtable define void @main() #0 {

问题是,当我没有使用全局变量时,同样的命令没有任何问题,当然,在子函数中使用变量的情况下,这是我尝试将alloca更改为全局的原因。

帮助找到解决此问题的方法。 在此先感谢。 注1:我用LLVM-3.8

UPDATE: 当我从铛命令的结果是删除-v:

refined_output.o: In function bsort': refined_output.ll:(.text+0x4): undefined reference to x' refined_output.ll:(.text+0xa): undefined reference to n' refined_output.ll:(.text+0x10): undefined reference to changed' refined_output.ll:(.text+0x22): undefined reference to i' refined_output.ll:(.text+0x2c): undefined reference to changed' refined_output.ll:(.text+0x42): undefined reference to i' refined_output.ll:(.text+0x48): undefined reference to n' refined_output.ll:(.text+0x50): undefined reference to i' refined_output.ll:(.text+0x59): undefined reference to x' refined_output.ll:(.text+0x60): undefined reference to i' refined_output.ll:(.text+0x75): undefined reference to x' refined_output.ll:(.text+0x7c): undefined reference to i' refined_output.ll:(.text+0x95): undefined reference to changed' refined_output.ll:(.text+0xa3): undefined reference to changed' refined_output.o: In function swap': refined_output.ll:(.text+0xc3): undefined reference to x.1' refined_output.ll:(.text+0xca): undefined reference to y' refined_output.ll:(.text+0xd1): undefined reference to x.1' refined_output.ll:(.text+0xd9): undefined reference to t' refined_output.ll:(.text+0xe0): undefined reference to x.1' refined_output.ll:(.text+0xe7): undefined reference to y' refined_output.ll:(.text+0xf2): undefined reference to y' refined_output.ll:(.text+0xf8): undefined reference to t' refined_output.o: In function main': refined_output.ll:(.text+0x102): undefined reference to i.4' refined_output.ll:(.text+0x10c): undefined reference to seed' refined_output.ll:(.text+0x123): undefined reference to x.2' refined_output.ll:(.text+0x12a): undefined reference to i.4' refined_output.ll:(.text+0x130): undefined reference to seed' refined_output.ll:(.text+0x15d): undefined reference to seed' refined_output.ll:(.text+0x166): undefined reference to i.4' refined_output.ll:(.text+0x16c): undefined reference to i.4' refined_output.ll:(.text+0x177): undefined reference to x.2' refined_output.ll:(.text+0x18d): undefined reference to x.2' refined_output.ll:(.text+0x19e): undefined reference to x.2' refined_output.o: In function printArray': refined_output.ll:(.text+0x1c4): undefined reference to msg' refined_output.ll:(.text+0x1cb): undefined reference to x.2' refined_output.ll:(.text+0x1d1): undefined reference to n.3' refined_output.ll:(.text+0x1d8): undefined reference to msg' refined_output.ll:(.text+0x1e3): undefined reference to i.4' refined_output.ll:(.text+0x1f3): undefined reference to x.2' refined_output.ll:(.text+0x1fa): undefined reference to i.4' refined_output.ll:(.text+0x208): undefined reference to i.4' refined_output.ll:(.text+0x20e): undefined reference to i.4' refined_output.ll:(.text+0x214): undefined reference to n.3' refined_output.ll:(.text+0x21c): undefined reference to i.4'

clang: error: linker command failed with exit code 1 (use -v to see invocation)

,所有这个变量在我的计划中LLVM IR开始定义作为:

@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 

我希望这可以帮助你,帮助我---或者在我的红外代码生成中发现问题。

我期待着它..

+0

void main?你确定? – kfsone

+0

将其更改为int main()不会更改任何内容。 –

@x = external global i32* 

等都是全局变量声明,而不是定义。因此,“未定义参考”错误是有效的,您确实需要定义它们。

有关更多信息,请参见http://llvm.org/docs/LangRef.html#global-variables

还要注意,LLVM-AS/LLC一部分是多余的 - 铛可以编译.ll/.BC文件就好了。

+0

我应该在每个使用变量的函数中使用它,或者至少使用一个函数(比如第一个使用它的函数)?其次,你可以说一些C++ API调用,我可以成功地定义一个定义吗?提前致谢! –

+0

解决问题!解决方法是使用C++ api call gvar-> setInitializer()在每个全局变量中设置初始值设定项。要找到正确的方法,可以使用clang,如下所示:clang-3.8 -c -emit-llvm input.cpp -o input.ll及之后的llc-3.8 -march = cpp -o output.ll.cpp输入。二 。用C++编写你想要的全局变量,并查看每个变量类型与哪个C++ api调用关联。 –