Page 1 of 1

gcc/ld linking unresolved references problem?

Posted: Mon Apr 19, 2010 1:22 pm
by Sam111
Ok , I have created these two files

prog1.c

Code: Select all

#include <stdio.h>
extern int func1( int ) ;

int main()
{


int i = func1( 3 ) ; 
printf( "Hi this is  %d " , i ) ;
return 0 ;
}


and

prog2.asm

Code: Select all

global _func1
_func1:
mov ax , [esp + 4]
ret 4


I do

Code: Select all

nasm -f coff  prog2.asm
which gives me prog2.o

Then I compile prog1.c

Code: Select all

gcc -c prog1.c
which gives me prog1.o

But my problem is when I link the 2 likes I get

Code: Select all


C:\asm2>ld prog1.o prog2.o -o output.exe
c:/djgpp/bin/ld.exe: warning: cannot find entry symbol start; defaulting to 0000
18d0
prog1.o(.text+0x22):prog1.c: undefined reference to `func1'
prog1.o(.text+0x39):prog1.c: undefined reference to `printf'


I thought at first I had to include the libc so I tried this using ld [same stuff] -L\..\djgpp\lib\libc.a
but that just gave me a ton of unresolved references...etc

I declared the func1 external in my c program so the linker should find it in prog2.o.

I just don't get what is going wrong. I have tried coff because when I objdump the 2 files they seem to be of the coff type.

Note I didn't make a linker script for it put I thought the default entry point would be main.
I can clear it up by setting the default to main I guess.

As for the unresolved references I don't understand it they are in the files being linked.
I used the _func1 for func1 since that is the mangled nameing convention.

Thanks for any help on this one
They are both coff-go32 object format.

If I remove the extern func1 in the main program and just use gcc prog1.c -o output.exe
I get the output.exe which executes fine And displays Hi this is 0

like this

Code: Select all

#include <stdio.h>
//extern int func1( int ) ;

int main()
{

int i = 0 ;
//int i = func1( 3 ) ; 
printf( "Hi this is  %d " , i ) ;
return 0 ;
}
So their must be something with ld or I am not correctly linking in someway?
Either way it is compiling fine it is linking issues that I cann't figure out.

Re: gcc/ld linking unresolved references problem?

Posted: Mon Apr 19, 2010 2:10 pm
by Gigasoft
In GCC, names aren't prefixed with an underscore, that's only in Microsoft C. So, you should just call your function func1.

When you specify a library, you must use a lowercase L. The uppercase L option is for specifying a search path for libraries.

Re: gcc/ld linking unresolved references problem?

Posted: Mon Apr 19, 2010 3:05 pm
by Hangin10
Link with gcc instead of ld.

Main is not the entry point. Usually start is the label for an assembly language routine that at the very least calls main and passes its return value to the OS's exit syscall.

Re: gcc/ld linking unresolved references problem?

Posted: Mon Apr 19, 2010 5:21 pm
by Sam111
it doesn't work.

I have tried what you said I still get the

Code: Select all

: undefined reference to `func1'
: undefined reference to `printf'
Are you sure your not suppose to proceed every external asm function with an _
I am on a windows machine using gcc from djgpp so I think you have to use the _.

Eitherway it is not working and the weirder thing is printf if I tell it the library file should work fine.
It is when I use ld. Is their away to use gcc just to link all the files together after you got all the object files.
Because I have compiled them all with gcc -c and nasm -f coff
now how do I link them all with gcc. I was just using ld to do it but it was not working?

I see your point with the main yes it probably uses start as it's default instead of main that is an easy fix.
But these unresolved references shouldn't be occuring.

Re: gcc/ld linking unresolved references problem?

Posted: Tue Apr 20, 2010 12:12 am
by Solar
You aren't clear about what exactly you tried.

Have you read the part where Gigasoft tells you you need -l\..\djgpp\lib\libc.a (lowercase -l) instead of -L\..\djgpp\lib\libc.a (uppercase -L)?

Check your assumptions. Use objdump to see what exact spelling for func1 is referenced in prog1.o, and then use objdump again to assert that func1 is actually in prog2.o by the exact same spelling.

If you want to use GCC instead of ld, do this:

Code: Select all

nasm -f coff  prog2.asm
gcc prog1.c prog2.o -o output.exe
Also note that it doesn't help to simply rename your main() to start().

_start is the entry point for the C library runtime. That's a bit of assembler code that does the housekeeping and setup chores (like initializing stdout...) before passing control to the C language main(). Using GCC instead of ld for linking will solve this problem, as GCC links in the C runtime automatically. Otherwise, you'd have to link in crt0.o yourself.

Re: gcc/ld linking unresolved references problem?

Posted: Tue Apr 20, 2010 11:34 am
by Sam111
Well I tried

Code: Select all

nasm -f coff prog2.asm
gcc prog1.c prog2.o
It gives me this

Code: Select all

c:/djgpp/tmp/ccskXUeR.o(.text+0x2d):prog1.c: undefined reference to `_func1'
collect2: ld returned 1 exit status
And my files are
prog1.c

Code: Select all

#include <stdio.h>
extern int func1( int ) ;

int main()
{

int i = 0 ;
i = func1( 3 ) ; 
printf( "Hi this is  %d " , i ) ;
return 0 ;
}

and prog2.asm

Code: Select all

global _func1
_func1:
xor eax , eax
mov eax , 3
ret 
I found out gcc doesn't support stdcalls only fast and cdecl so I am using the default cdecl.

My problem is how the "f" is it not seeing func1. I did an objdump on prog2.o it had an underscore as I put in it. I don't have an prog1.o file because I am trying your example

Code: Select all

nasm -f coff prog2.asm
gcc prog1.c prog2.o
So it didn't work if I use global func1 or global _func1 ?

Re: gcc/ld linking unresolved references problem?

Posted: Tue Apr 20, 2010 11:40 am
by Firestryke31
Try putting prog2.o in front of prog1.c. Perhaps LD isn't seeing the function since it hasn't gotten to the second file yet.

Re: gcc/ld linking unresolved references problem?

Posted: Tue Apr 20, 2010 11:48 am
by Sam111
nop , I tried switching and that didn't work.

I am I using the extern command correctly?
Am I missing something in my asm file?

Should I be using -f coff with nasm ?

I don't think any of the above are issues.

I am wondering what this is

Code: Select all

c:/djgpp/tmp/ccaHgTJw.o(.text+0x2d) .... in my error linking line
Maybe the gcc/ld are caching the files and using those tmp ones instead that sounds pretty stupid though.
I am going to restart my machine and try it again and see if that clears up the issue

do I have to tell gcc to look in prog2.o for the external reference manual? Because if this is the case then I might as well just write my own linker to.

Re: gcc/ld linking unresolved references problem?

Posted: Tue Apr 20, 2010 12:16 pm
by Combuster
Please do use sections if you're assembling to an object file. Linkers don't like it otherwise.

Re: gcc/ld linking unresolved references problem?

Posted: Tue Apr 20, 2010 12:20 pm
by KotuxGuy
Sam111 wrote: I am wondering what this is

Code: Select all

c:/djgpp/tmp/ccaHgTJw.o(.text+0x2d) .... in my error linking line
I believe that means the error is offset 0x2D from the start of your C code.

Try adding -fno-leading-underscore to your GCC options, and removing the leading underscores in your NASM code.
And why are you using DJGPP anyway? It's out of date, and IMHO, Cygwin is far better, and most of the wiki articles about compiling on Windows use Cygwin as their basis.

Re: gcc/ld linking unresolved references problem?

Posted: Tue Apr 20, 2010 11:01 pm
by Solar
Sam111 wrote:I don't have an prog1.o file because I am trying your example.
{Profanity deleted}

Damn it, man! THINK! You had a prog1.o in your previous attempt, no? Don't you think it will be identical to what GCC generates when you leave out the "-c"?