首页 > 解决方案 > How does `declare` in LLVM IR works

问题描述

For example

@.str = private unnamed_addr constant [13 x i8] c"hello world\0A\00"

declare i32 @puts(i8* nocapture) nounwind

define i32 @main() {
  %cast210 = getelementptr [13 x i8], [13 x i8]* @.str, i64 0, i64 0

  call i32 @puts(i8* %cast210)
  ret i32 0
}

I don't understand where the function puts comes from. It seems to be a C function in stdio.h, but what does this have to do with LLVM? Where is its implementation?

标签: llvm

解决方案


Speaking in C/C++ terms, each translation unit can reference external symbols. For compiler it doesn't matter where symbol is actually defined as far as you have a declaration for it.

After compiling your .c files you get something that needs to be linked together (object files .o or LLVM IR .ll/.bc). On the linking stage all symbol definitions are resolved (in different ways).

In your example, the puts function is usually located in the system libc, which gets linked automatically by default. You won't find LLVM IR code for this function unless you somehow compiled whole libc into LLVM IR.

Read some general tutorial on the "compilation and linking" topic.


推荐阅读