Linking function from another module: undefined reference to xxx

In my toy-language, I’m trying to add support for multiple modules (multiple source files).

In my example, I have fileB which defines a function “functionInFileB”, which fileA imports.
FileA also defines a function “functionInFileA”. Both takes an int and does nothing.
My understanding of importing functions, is that the importing module must create an empty declaration of the function - is that correct?

The IR I produce is this:

FileA:

; ModuleID = 'fileA'
source_filename = "fileA"

define i32 @main() {
entrypoint:
  call void @functionInFileA(i32 25)
  call void @functionInFileB(i32 25)
  ret i32 0
}

define internal void @functionInFileA(i32 %a) {
functionInFileA_body:
  ret void
}

declare internal void @functionInFileB(i32)

FileB:

; ModuleID = 'fileB'
source_filename = "fileB"

define i32 @root() {
entrypoint:
  ret i32 0
}

define internal void @functionInFileB(i32 %a) {
functionInFileB_body:
  ret void
}

If I try to link these with clang, I get an error:

$ clang++-12 -o program fileA.o fileB.o

fileA.o: In function `main':
fileA:(.text+0x11): undefined reference to `functionInFileB'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

So my question is, is the IR correct or am I linking it incorrectly? Can you point me to something examples?
Thank you in advance!

The IR is almost correct :slight_smile:
Your functions are marked as internal, therefore they are not exposed and the linker cannot see them. Mark them as external and it should just work.

Ugh, that is almost embarrassing - not sure how I overlooked that. Thank you, it works!