problem compiling with gcc -o option

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
Stephen

problem compiling with gcc -o option

Post by Stephen »

I was going through an assembly tutorial, and I am having a problem now when compiling my assembler code with my C code.

The ASM:
*******************************************
; file: test1.asm
; First assembly program testing the ability
; to store data in the EAX register and print
; it to the screen.
;
; Syntax to create executable using DJGPP:
; nasm -f coff test1.asm
; gcc -o test1 test.o driver.c asm_io.o

%include "e:\djgpp\asm_io.inc"
;
; initialized data should be placed under
; the .data segment

segment .data
;
output1 db "Hello World! ", 0 ;space after string = null terminator
;

;
segment .bss
;

;
segment .text
global _asm_main
_asm_main:
enter 0,0
pusha

; begin program code
mov eax, output1 ; prepare variable for output
call print_string ; invoke asm proc that calls C
; end program code

; all below information required
popa
mov eax,0 ; return back to C
leave
ret
*******************************************

The "C" code:
*******************************************
int asm_main(void);

int main()
{
int ret_status;
ret_status = asm_main();
return ret_status;
}
*******************************************

I guess the way the author intended this to work, was that even though the C funtion asm_main does not exist, when it is linked in or compiled with the ASM code, it'll see the _asm_main global reference and compile OK. I compile the ASM with this command:
nasm -f coff test1.asm

... works ok. However, I try to compile all my files together with gcc with this command:
gcc -o test1 test.o driver.c asm_io.o

... and I get this error:

e:/djgpp/tmp/ccIkRtvO.o(.text+0x11):driver.c: undefined reference to `_asm_main'

collect2: ld returned 1 exit status

Does anyone have any idea what I'm missing here? I am using the DJGPP program files to develop all this, as shown by the author.
MoneyCat

Re:problem compiling with gcc -o option

Post by MoneyCat »

Code: Select all

 
global _asm_main
_asm_main:
should be

Code: Select all

global _asm_main
asm_main:
Well at least thats how I have it in mine and it compiled and linked fine in aout and elf formats.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:problem compiling with gcc -o option

Post by Pype.Clicker »

Code: Select all

@moneycat : but stephen uses COFF format, and COFF prepends an underscore to any C symbol.

So if you have 
[code file.c]
   asm_main();
what you actually refer is _asm_main.

What troubles me more is that your test1.asm should be assembled as test1.o and that you specify test.o rather than test1.o to the linker ...

maybe just a typo in your post ... or in your compile script ;)
Stephen

Re:problem compiling with gcc -o option

Post by Stephen »

Sorry about that. The gcc command I am using is:

gcc -o test1 \djgpp\test1.o \djgpp\driver.c \djgpp\asm_io.o
Stephen

Re:problem compiling with gcc -o option

Post by Stephen »

Well, I managed to get it working now. I don't know what I did different, but it works. Thanks for the help.
Post Reply