gcc - link c and assembly -
i have simple main.c file:
#include <stdio.h> int cnt; extern void increment(); int main() { cnt = 0; increment(); printf("%d\n", cnt); return 0; } and simpler hello.asm:
extern cnt section .text global increment increment: inc dword [cnt] ret first main.o typing gcc -c main.c hello.o -- nasm -f macho hello.asm -ddarwin and, finally, executable ld -o main main.o hello.o -arch i386 -lc , error:
ld: warning: -macosx_version_min not specified, assuming 10.10 ld: warning: ignoring file main.o, file built unsupported file format ( 0xcf 0xfa 0xed 0xfe 0x07 0x00 0x00 0x01 0x03 0x00 0x00 0x00 0x01 0x00 0x00 0x00 ) not architecture being linked (i386): main.o undefined symbols architecture i386: "_main", referenced from: implicit entry/start main executable "cnt", referenced from: increment in hello.o ld: symbol(s) not found architecture i386 how fix linking error?
- specify architecture (32/64 bit options
m32orm64) - link
crtfiles, these files contain runtime - that's code calls main function
modify asm file:
extern _cnt section .text global _increment _increment: inc dword [_cnt] ret so final command lines should be:
gcc -c -m32 main.c nasm -f macho hello.asm -ddarwin ld hello.o main.o /usr/lib/crt1.o -lc -o main check arch , execute:
file main main: mach-o executable i386 ./main 1
Comments
Post a Comment