尝试编译第 8 章的输出时出现 LLVM 教程 3.6 链接器错误

LLVM tutorial 3.6 linker error when trying to compile output from chapter 8

我正在学习 llvm 教程: http://llvm.org/releases/3.6.0/docs/tutorial/index.html

第 8 章的代码编译得很好并发出 IR,但是我无法编译发出的 IR。代码清单是复制和粘贴的,以减少我的代码中某处出现拼写错误的可能性。我唯一修改的是构建命令,因为 llvm-config 默认为旧版本。 构建命令:

clang++ -g -O3 toy.cpp `llvm-config-3.6 --cxxflags --ldflags --system-libs --libs core mcjit native` -o toy

到 运行 它(我从原来的命令中删除了 & 因为这似乎会引发错误):

./toy < programs/basic.ks | clang-3.6 -x ir -

输出如下:

; ModuleID = 'my cool jit'
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"

declare double @printd(double)

define double @main() {
entry:
  %calltmp = call double @printd(double 4.200000e+01), !dbg !14
  ret double %calltmp, !dbg !14
}

!llvm.module.flags = !{!0, !1}
!llvm.dbg.cu = !{!2}

!0 = !{i32 2, !"Debug Info Version", i32 2}
!1 = !{i32 2, !"Dwarf Version", i32 2}
!2 = !{!"0x11[=12=]2[=12=]Kaleidoscope Compiler[=12=]0[=12=][=12=]0[=12=][=12=]1", !3, !4, !4, !5, !4, !4} ; [ DW_TAG_compile_unit ] [./fib.ks] [DW_LANG_C]
!3 = !{!"fib.ks", !"."}
!4 = !{}
!5 = !{!6, !11}
!6 = !{!"0x2e[=12=]printd[=12=]printd[=12=][=12=]1[=12=]0[=12=]1[=12=]0[=12=]0[=12=]256[=12=]0[=12=]1", !3, !7, !8, null, double (double)* @printd, null, null, !4} ; [ DW_TAG_subprogram ] [line 1] [def] [printd]
!7 = !{!"0x29", !3}                               ; [ DW_TAG_file_type ] [./fib.ks]
!8 = !{!"0x15[=12=][=12=]0[=12=]0[=12=]0[=12=]0[=12=]0[=12=]0", null, null, null, !9, null, null, null} ; [ DW_TAG_subroutine_type ] [line 0, size 0, align 0, offset 0] [from ]
!9 = !{!10, !10}
!10 = !{!"0x24[=12=]double[=12=]0[=12=]64[=12=]64[=12=]0[=12=]0[=12=]4", null, null} ; [ DW_TAG_base_type ] [double] [line 0, size 64, align 64, offset 0, enc DW_ATE_float]
!11 = !{!"0x2e[=12=]main[=12=]main[=12=][=12=]2[=12=]0[=12=]1[=12=]0[=12=]0[=12=]256[=12=]0[=12=]2", !3, !7, !12, null, double ()* @main, null, null, !4} ; [ DW_TAG_subprogram ] [line 2] [def] [main]
!12 = !{!"0x15[=12=][=12=]0[=12=]0[=12=]0[=12=]0[=12=]0[=12=]0", null, null, null, !13, null, null, null} ; [ DW_TAG_subroutine_type ] [line 0, size 0, align 0, offset 0] [from ]
!13 = !{!10}
!14 = !MDLocation(line: 2, column: 8, scope: !11)
warning: overriding the module target triple with x86_64-apple-macosx10.10.0
1 warning generated.
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

示例代码使用 dump() 方法打印出 IR,该方法打印到 stderr。您应该在管道之前将 stderr 重定向到 stdout,如下所示:

./toy < programs/basic.ks 2>&1 | clang-3.6 -x ir -