Reputation: 178
I am attempting to write very basic x86 code and call it in a C program. I'm running OSX 10.8.2. Here is the code I have:
start.c:
#include <stdio.h>
void _main(); // inform the compiler that Main is an external function
int main(int argc, char **argv) {
_main();
return 0;
}
code.s
.text
.globl _main
_main:
ret
I run the following commands to attempt compilation:
gcc -c -o code.o code.s
gcc -c -o start.o start.c
gcc -o start start.o code.o
Which then returns this output after the final command:
Undefined symbols for architecture x86_64:
"__main", referenced from:
_main in start.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Am I missing something in my compiler calls? Do I need to update something/install something different? I just can't find a definitive answer anywhere since this is such a general output. Thanks!
Upvotes: 0
Views: 139
Reputation: 212929
You need an extra underscore in your asm _main
symbol:
.text
.globl __main
__main:
ret
C symbols get an underscore prefix when compiled, so your C main
is actually _main
and an extern C _main
actually needs to be defined as __main
if you write it in asm.
Upvotes: 4