Re: Linking libraries?
- From: "Julienne Walker" <happyfrosty@xxxxxxxxxxx>
- Date: 30 May 2006 05:26:57 -0700
Solar wrote:
Hi,
I'm a bit new to assembly. I'm programming on a linux machine (using
nasm) and as part of a programming challenge set by a friend, I am
trying to write a program that gets and displays the PATH environment
variable.
I made a stub C program to get the environment variable and found that
getenv() leaves a pointer to the string in eax, etc (please bear with
me, a lot of this stuff is new to me and I can't find any good
tutorials on the web).
However, I'm having a problem linking my program.
At the top of the file I have:
extern _getenv
global _start
section .data
; yada yada
section .text
; yada yada
As I was made aware that this is the way to tell ld that the getenv
function is in another object(? - I could be wrong, feel free to
correct me :))
I'm getting the following errors:
matt@matrix ~ $ ld -o path path.o
path.o: In function `_start':
path.asm:(.text+0x6): undefined reference to `_getenv'
Its a bit mystical to me, as far as I can see its all correct. I
thought that maybe part of the problem would be how I'm using ld, but I
couldn't find anything in the man pages.
If anyone could point me in the right direction as to how I'd solve
this, it would be very helpful :) Also, if anyone has any suggestions
for any online tutorials that are specific to assembly programming
under linux, that would be helpful too. Being the generic student, I'm
too poor/lazy to buy one (delete as appropriate)!
You're only linking the object file you created into an executable. ld
doesn't automagically link with the C library as well, you need to
specify it explicitly. If you're willing to follow a few rules that C
requires, you can use ld implicitly through gcc and the C library comes
along for the ride:
section .data
; Yadda yadda
section .text
global _main
extern _getenv
_main:
; Yadda yadda
xor eax,eax
ret
$ gcc path.o -o path
That saves you a few headaches, but removes a little bit of flexibility
because you have to satisfy the C runtime startup code.
.
- Follow-Ups:
- Re: Linking libraries?
- From: Frank Kotler
- Re: Linking libraries?
- From: Solar
- Re: Linking libraries?
- References:
- Linking libraries?
- From: Solar
- Linking libraries?
- Prev by Date: Re: Linking libraries?
- Next by Date: Re: Linking libraries?
- Previous by thread: Re: Linking libraries?
- Next by thread: Re: Linking libraries?
- Index(es):
Relevant Pages
|