Wierd problem with command line args

V

vjonweb

Hello!
I am facing an extremely frustrating problem with command line
arguments...I have no clue why it is happening. Any help will be
greatly appreciated.

My program: "a.c"
--------------------------------------------------------
#inlclude<stdio.h>

int main(int argc, char * argv[])
{
fprintf(stderr,"%d\n",argc);
fprintf(stderr,"%s",argv[0]);
return 0;
}
--------------------------------------------------------
If I compile this program using
cc a.c
and then run it using
../a.out arg1 arg2
the program displays
--------
3
a.out
--------

which is fine!!

However, if I compile & link this program separately using
cc -c a.c
ld -lc -ldl a.c

and run it using
../a.out arg1 arg2
the program displays
--------
0
Segmentation Fault, Core dumped
--------


I cannot understand this at all. Please help....

vj
 
B

Ben Pfaff

vjonweb said:
I am facing an extremely frustrating problem with command line
arguments...I have no clue why it is happening. Any help will be
greatly appreciated.

The problem is how you're invoking the linker. That's an issue
to discuss in a newsgroup about your implementation, e.g. a Linux
newsgroup if you're using Linux.
 
V

vjonweb

Ben said:
The problem is how you're invoking the linker. That's an issue
to discuss in a newsgroup about your implementation, e.g. a Linux
newsgroup if you're using Linux.

Can you please tell me what is the problem with the invocation...?I was
not sure if this was platform specific which is why I posted it here
originally...

VJ
 
B

Ben Pfaff

vjonweb said:
Can you please tell me what is the problem with the invocation...?I was
not sure if this was platform specific which is why I posted it here
originally...

And now you've been told that it *is* platform specific, so you
need to inquire about it someplace related to your platform.
 
K

Keith Thompson

vjonweb said:
I am facing an extremely frustrating problem with command line
arguments...I have no clue why it is happening. Any help will be
greatly appreciated.

My program: "a.c"
--------------------------------------------------------
#inlclude<stdio.h>

int main(int argc, char * argv[])
{
fprintf(stderr,"%d\n",argc);
fprintf(stderr,"%s",argv[0]);
return 0;
}

It's already been pointed out that your problem is platform-specific,
but I'll point out a couple of things that aren't.

You misspelled "include". Apparently you've re-typed your program.
Don't do that. Copy-and-paste the *exact* program as you fed it to
the compiler; otherwise we can't guess which errors are in the
original program that you didn't show us, and which were introduced
when you re-typed it.

[snip]
However, if I compile & link this program separately using
cc -c a.c
ld -lc -ldl a.c

and run it using
./a.out arg1 arg2
the program displays

Now this part is platform-specific.

<OT>
"ld" is the linker. The linker works on object files, not on source
files; the correct command line would be "ld -lc -ldl a.o".
Copying-and-pasting command lines is just as important as
copying-and-pasting source code.
</OT>
 
D

Dik T. Winter

> Can you please tell me what is the problem with the invocation...?I was
> not sure if this was platform specific which is why I posted it here
> originally...

It depends on your platform. The C language states nothing about linking.
If I perform as you wrote the response is:
Linux.i686/nissiros.cst/ksh: cc -c a.c
Linux.i686/nissiros.cst/ksh: ld -lc -ldl a.c
a.c: file not recognized: File format not recognized
not entirely what you wrote. If I change the second line to:
Linux.i686/nissiros.cst/ksh: ld -lc -ldl a.o
ld: warning: cannot find entry symbol _start; defaulting to 08048214
Linux.i686/nissiros.cst/ksh:
again not entirely what you wrote. Please, this appears to be Linux
specific (and also depending on the version of Linux you are using).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top