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

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
negcit.K
Member
Member
Posts: 34
Joined: Fri Dec 07, 2007 9:57 am

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

Post 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 !
Last edited by negcit.K on Mon Dec 10, 2007 10:11 am, edited 1 time in total.
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Post 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.
User avatar
negcit.K
Member
Member
Posts: 34
Joined: Fri Dec 07, 2007 9:57 am

Post by negcit.K »

Thanks to korona !
i've done it correctly.
:wink:
Post Reply