Page 1 of 1

(Solved) how to compile a .asm file together with a c file ?

Posted: Fri Dec 07, 2007 10:21 am
by negcit.K
if i wrote a asm file and a c file. i wanna call the c function in the asm file. say void main() function in test.c , and i worte this in the test.asm:
call _main
does it work ?

and how can i compile the two files together into a new file like test.bin ?

this is my first question , if you can't get the meaning (oh ,my poor english ) , i will say like this:
i have a file called main.c :
#include <stdio.h>
void main()
{
int total;
total = sum( 12, 12);
printf("%d\n",total);
},
and the sum function is defined in the sum.c file:
int sum( int a, int b)
{
return a + b;
}

how can i compile this two files correctly. if i just compile main.c using " gcc main.c", the gcc will write somethings as follows:
/tmp/ccoHLQeq: In function `main`:
main.c:(.text +0xf): undefined reference to `sum'
collect2: ld returned 1 exit status

i guess the ld will do it , but i really know nothing about ld ,and the gcc opptional paramters. can someone give some tips ?

thanks !

Posted: Fri Dec 07, 2007 12:20 pm
by Korona
Compile the C file and the assembler source code to object files using
gcc -c -o firstObj.o csource.c
nasm -f elf -o secondObj.o asmsource.asm
If you are going to run the program under your host os, you can link the files together by invoking gcc firstObj.o secondObj.o
If you want to build a freestanding binary, you will have to use ld and a linker script which tells ld where to put your code and data. The wiki should have infos about that.

Posted: Mon Dec 10, 2007 10:05 am
by negcit.K
Thanks to korona !
i've done it correctly.
:wink: