Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

about stderror

44 views
Skip to first unread message

rainman

unread,
Jul 12, 2008, 7:10:48 PM7/12/08
to
I am using ubuntu 8.04.

I wrote a small program to try stderror.

===
#include <stdio.h>
#include <errno.h>
#include <string.h>

int
main()
{
printf("%d\n", errno);
printf("%s\n", stderror(errno));
return 0;
}
===

But it always says

gcc test.c
/tmp/ccojpACa.o: In function `main':
test.c:(.text+0x33): undefined reference to `stderror'
collect2: ld returned 1 exit status

and,

$ gcc --version
gcc (GCC) 4.2.3 (Ubuntu 4.2.3-2ubuntu7)


I googled, and most articles just say to include "string.h" is enough.
what's the problem?

Thanks.
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Gordon Burditt

unread,
Jul 13, 2008, 11:55:35 AM7/13/08
to
>I wrote a small program to try stderror.

Uh, what? If you mean the file stream called "Standard Error",
it's spelled stderr. If you mean the function to turn errno
error codes into readable text strings, it's spelled strerror().

Keith Thompson

unread,
Jul 13, 2008, 11:55:47 AM7/13/08
to
rainman <tang...@gmail.com> writes:
> I am using ubuntu 8.04.
>
> I wrote a small program to try stderror.
>
> ===
> #include <stdio.h>
> #include <errno.h>
> #include <string.h>
>
> int
> main()
> {
> printf("%d\n", errno);
> printf("%s\n", stderror(errno));
> return 0;
> }
> ===
>
> But it always says
>
> gcc test.c
> /tmp/ccojpACa.o: In function `main':
> test.c:(.text+0x33): undefined reference to `stderror'
> collect2: ld returned 1 exit status
>
> and,
>
> $ gcc --version
> gcc (GCC) 4.2.3 (Ubuntu 4.2.3-2ubuntu7)
>
>
> I googled, and most articles just say to include "string.h" is enough.
> what's the problem?

The problem is that you misspelled "strerror".

(And I suggest "int main(void)" rather than "int main()".)

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Jack Klein

unread,
Jul 13, 2008, 11:55:44 AM7/13/08
to
On Sat, 12 Jul 2008 18:10:48 -0500 (CDT), rainman <tang...@gmail.com>
wrote in comp.lang.c.moderated:

> I am using ubuntu 8.04.
>
> I wrote a small program to try stderror.
>
> ===
> #include <stdio.h>
> #include <errno.h>
> #include <string.h>
>
> int
> main()
> {
> printf("%d\n", errno);
> printf("%s\n", stderror(errno));
> return 0;
> }
> ===
>
> But it always says
>
> gcc test.c
> /tmp/ccojpACa.o: In function `main':
> test.c:(.text+0x33): undefined reference to `stderror'
> collect2: ld returned 1 exit status
>
> and,
>
> $ gcc --version
> gcc (GCC) 4.2.3 (Ubuntu 4.2.3-2ubuntu7)
>
>
> I googled, and most articles just say to include "string.h" is enough.
> what's the problem?

The problem is one of the following:

1. You are trying to use a function named "stderror" which is not
part of the standard C library but is instead either a gnu or Linux
extension, in which case we can't help you here.

2. I think this one is more likely. You are confusing the name of a
pre-opened output stream, "stderr", with the name of a standard
library function that generates strings from integers, namely
"strerror", NOT "stderror".

The standard library function strerror() is indeed prototyped in
<string.h>.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

Barry Schwarz

unread,
Jul 13, 2008, 11:57:08 AM7/13/08
to
On Sat, 12 Jul 2008 18:10:48 -0500 (CDT), rainman <tang...@gmail.com>
wrote:

>I am using ubuntu 8.04.
>
>I wrote a small program to try stderror.
>
>===
>#include <stdio.h>
>#include <errno.h>
>#include <string.h>
>
>int
>main()
>{
> printf("%d\n", errno);
> printf("%s\n", stderror(errno));

The function you want is called strerror, not stderror.

BTW, are you aware that errno is set to zero at startup?

> return 0;
>}
>===
>
>But it always says
>
>gcc test.c
>/tmp/ccojpACa.o: In function `main':
>test.c:(.text+0x33): undefined reference to `stderror'

That's what happens when you misspell a function name.

>collect2: ld returned 1 exit status
>
>and,
>
>$ gcc --version
>gcc (GCC) 4.2.3 (Ubuntu 4.2.3-2ubuntu7)
>
>
>I googled, and most articles just say to include "string.h" is enough.
>what's the problem?

Your first hint should have been that most of the google hits are for
languages other than C.


Remove del for email

Kalle Olavi Niemitalo

unread,
Jul 13, 2008, 11:57:17 AM7/13/08
to
rainman <tang...@gmail.com> writes:

> printf("%d\n", errno);
> printf("%s\n", stderror(errno));

The function is called strerror, not stderror.
Besides, the first printf call may already change errno,
so if you want the printed values to match each other,
you should save the initial errno and use that in both calls:

int saved_errno = errno;
printf("%d\n", saved_errno);
printf("%s\n", strerror(saved_errno));

Note that printf("%d\n%s\n", errno, strerror(errno)) is not
reliable either, because the arguments of printf might be
evaluated right to left, and strerror might change errno.

Dag-Erling Smørgrav

unread,
Jul 13, 2008, 11:57:14 AM7/13/08
to
rainman <tang...@gmail.com> writes:
> I wrote a small program to try stderror.

There is no such thing as stderror. Perhaps you mean strerror?

> gcc test.c

You should always compile with warnings enabled:

% gcc -Wall -Wextra -std=c99 test.c

Read the gcc documentation for an explanation of these options.

DES
--
Dag-Erling Smørgrav - d...@des.no

Jens Schweikhardt

unread,
Jul 13, 2008, 11:57:19 AM7/13/08
to
rainman <tang...@gmail.com> wrote
in <clcm-2008...@plethora.net>:
# I am using ubuntu 8.04.
#
# I wrote a small program to try stderror.

Use the key that's northeast of the 'd' on your keyboard :-)

Regards,

Jens
--
Jens Schweikhardt http://www.schweikhardt.net/
SIGSIG -- signature too long (core dumped)

Wolfgang Draxinger

unread,
Jul 13, 2008, 11:57:21 AM7/13/08
to
rainman wrote:

> I googled, and most articles just say to include "string.h" is
> enough. what's the problem?

You need some glasses. The function is named strerror (there's
a 'r' at the 3rd place, not a 'd'), not stderror.

Wolfgang Draxinger
--
E-Mail address works, Jabber: hexa...@jabber.org, ICQ: 134682867

Hans-Bernhard Bröker

unread,
Jul 13, 2008, 11:57:30 AM7/13/08
to
rainman wrote:
> I wrote a small program to try stderror.

You mistyped or misread strerror.

Jasen Betts

unread,
Jul 20, 2008, 5:09:17 PM7/20/08
to
On 2008-07-13, Kalle Olavi Niemitalo <k...@iki.fi> wrote:
> rainman <tang...@gmail.com> writes:
>
>> printf("%d\n", errno);
>> printf("%s\n", stderror(errno));
>
> The function is called strerror, not stderror.
> Besides, the first printf call may already change errno,

In this case, if it does, the it's unlikely the effect will be visible :)

If the first printf modifies errno it's most unlikely that the second
will produce any output.

Bye.
Jasen

Keith Thompson

unread,
Jul 21, 2008, 7:27:27 PM7/21/08
to
Jasen Betts <ja...@xnet.co.nz> writes:
> On 2008-07-13, Kalle Olavi Niemitalo <k...@iki.fi> wrote:
>> rainman <tang...@gmail.com> writes:
>>
>>> printf("%d\n", errno);
>>> printf("%s\n", stderror(errno));
>>
>> The function is called strerror, not stderror.
>> Besides, the first printf call may already change errno,
>
> In this case, if it does, the it's unlikely the effect will be visible :)
>
> If the first printf modifies errno it's most unlikely that the second
> will produce any output.

That's not necessarily correct.

Note that the standard doesn't actually require printf to set errno on
failure. The input and output functions set errno to EILSEQ if an
encoding error occurs; otherwise, only fgetpos, fsetpos, and ftell are
required to do so. (It's odd that fseek isn't in this list; I wonder
if that was an unintentional oversight.)

Even if it does so for a given implementation, there's nothing
preventing it from modifying the value of errno on success. printf
might call some lower-level routine that has the side effect of
modifying errno.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Kalle Olavi Niemitalo

unread,
Jul 21, 2008, 7:29:11 PM7/21/08
to
Jasen Betts <ja...@xnet.co.nz> writes:

> If the first printf modifies errno it's most unlikely that the second
> will produce any output.

Unlikely but possible. The first printf can modify errno
(to a nonzero value) and succeed. C99 7.5p3.

Dag-Erling Smørgrav

unread,
Jul 21, 2008, 7:29:27 PM7/21/08
to
Jasen Betts <ja...@xnet.co.nz> writes:

> Kalle Olavi Niemitalo <k...@iki.fi> writes:
>> The function is called strerror, not stderror.
>> Besides, the first printf call may already change errno,
>
> In this case, if it does, the it's unlikely the effect will be visible :)
>
> If the first printf modifies errno it's most unlikely that the second
> will produce any output.

You are wrong. ISO/IEC 9899:1999 section 7.5:

[#3] The value of errno is zero at program startup, but is
never set to zero by any library function.159) The value of
errno may be set to nonzero by a library function call
whether or not there is an error, provided the use of errno
is not documented in the description of the function in this
International Standard.

The use of errno is not documented in the description of printf().

Try the following program on FreeBSD:

| #include <stdio.h>
| #include <errno.h>
| #include <string.h>
|
| int
| main()
| {

| printf("%d\n", errno);

| printf("%s\n", strerror(errno));
| return 0;
| }

The first printf() prints "0\n", since errno is zero (as specified by
the standard) at the start of the program. However, printf() calls
malloc() internally, and FreeBSD's malloc() looks for /etc/malloc.conf
on its first invocation. On most systems, there is no malloc.conf, so
when the first printf() retuns, errno is set to ENOENT. The second
printf() therefore prints "No such file or directory\n".

On a glibc system (such as most Linux distributions), the same program
will print "0\nSuccess\n".

DES
--
Dag-Erling Smørgrav - d...@des.no

0 new messages