gcc/ld linking unresolved references problem?

Programming, for all ages and all languages.
Post Reply
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

gcc/ld linking unresolved references problem?

Post 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.
Gigasoft
Member
Member
Posts: 855
Joined: Sat Nov 21, 2009 5:11 pm

Re: gcc/ld linking unresolved references problem?

Post 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.
Hangin10
Member
Member
Posts: 162
Joined: Wed Feb 27, 2008 12:40 am

Re: gcc/ld linking unresolved references problem?

Post 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.
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: gcc/ld linking unresolved references problem?

Post 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.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: gcc/ld linking unresolved references problem?

Post 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.
Every good solution is obvious once you've found it.
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: gcc/ld linking unresolved references problem?

Post 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 ?
Last edited by Sam111 on Tue Apr 20, 2010 11:43 am, edited 1 time in total.
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: gcc/ld linking unresolved references problem?

Post 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.
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
User avatar
Sam111
Member
Member
Posts: 385
Joined: Mon Nov 03, 2008 6:06 pm

Re: gcc/ld linking unresolved references problem?

Post 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.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: gcc/ld linking unresolved references problem?

Post by Combuster »

Please do use sections if you're assembling to an object file. Linkers don't like it otherwise.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
KotuxGuy
Member
Member
Posts: 96
Joined: Wed Nov 25, 2009 1:28 pm
Location: Somewhere within 10ft of my favorite chubby penguin!

Re: gcc/ld linking unresolved references problem?

Post 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.
Give a man Linux, you feed the nearest optician ( Been staring at the PC too long again? ).
Give a man OS X, you feed the nearest NVidia outlet ( I need more GPU power!! )
Give a man Windows, you feed the entire Tylenol company ( Self explanatory :D )
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: gcc/ld linking unresolved references problem?

Post 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"?
Every good solution is obvious once you've found it.
Post Reply