I'm currently building object files using swiftc, with:
swiftc -emit-object bar.swift
where bar.swift is something simple like:
class Bar {
var value: Int
init(value: Int) {
self.value = value
}
func plusValue(_ value: Int) -> Int {
return self.value + value
}
}
When I then move on to linking this against my main object to create an executable, I get the following error:
$ cc -o foobar foo.o bar.o
duplicate symbol '_main' in:
foo.o
bar.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
This indicates that swiftc is adding a main implementation to the object file, which can be confirmed with:
$ nm bar.o | grep _main
0000000000000000 T _main
As far as I can tell, this added function does very little:
$ otool -tV bar.o
bar.o:
(__TEXT,__text) section
_main:
0000000000000000 pushq %rbp
0000000000000001 movq %rsp, %rbp
0000000000000004 xorl %eax, %eax
0000000000000006 movl %edi, -0x4(%rbp)
0000000000000009 movq %rsi, -0x10(%rbp)
000000000000000d popq %rbp
000000000000000e retq
000000000000000f nop
....snip...
Is there a way to tell swiftc -emit-object to not add this vestigial implementation of main?