ubuntu 8.10で「unix環境高度プログラミング」の最初の例を解決
本人の使用するオペレーティングシステムはubuntu 8である.10.
「unix環境高度プログラミング」の学習を開始し、コンパイルの最初の例に問題が発生しました.
myls.c:1:19: apue.h: No such file or directory myls.c: In function `main': myls.c:13: error: `NULL' undeclared (first use in this function) myls.c:13: error: (Each undeclared identifier is reported only once myls.c:13: error: for each function it appears in.)
コンパイル後のエラーメッセージを見ると、ファイルapueが見つかりませんでした.h.
ネット上の人がどのように解決したのかを見て、apueを発見しました.h開発環境に手動で追加します.そこでwww.apuebook.comはソースコードをダウンロードします.
ソースコードを解凍するとディレクトリapueが表示されます.2e.apue.2 eの下のincludeディレクトリのapue.hファイルを/usr/includeにコピーします.
cd apue.2 e所在ディレクトリ
cd include
cp apue.h/usr/include
再コンパイルするか、それとも問題が発生しますか.
/tmp/ccBBopm0.o(.text+0x2b): In function `main': : undefined reference to `err_quit' /tmp/ccBBopm0.o(.text+0x5f): In function `main': : undefined reference to `err_sys' collect2: ld returned 1 exit status
errが見つからなかったのかquit関数とerr_Sys関数の定義.
実はこの2つの関数は『unix環境高度プログラミング』の付録Bで見つけることができます.
ここで解決策を示します.
ディレクトリ/usr/includeに移動し、ファイルmyerrを作成します.h.次のコードをこのファイルに追加します.
次に、同じディレクトリの下のapueを編集します.hファイル(このファイルは前のステップで追加されました).最後の行#endif/*_APUE_H*/の前に宣言を追加します.
#include"myerr.h"/*自分で追加したヘッダファイルは、unix環境の高度なプログラミングに便利です*/
次に、ソースコードが存在するディレクトリに移動し、コンパイルします.
gcc -o myls myls.c
今回成功しました.
./myls /usr
次の結果が表示されます.
src .. . lib share sbin include local X11R6 bin games
すべてのフォルダをリストできました.success!
私と同じ初心者に一定の助けがあることを望んでいます.
「unix環境高度プログラミング」の学習を開始し、コンパイルの最初の例に問題が発生しました.
myls.c:1:19: apue.h: No such file or directory myls.c: In function `main': myls.c:13: error: `NULL' undeclared (first use in this function) myls.c:13: error: (Each undeclared identifier is reported only once myls.c:13: error: for each function it appears in.)
コンパイル後のエラーメッセージを見ると、ファイルapueが見つかりませんでした.h.
ネット上の人がどのように解決したのかを見て、apueを発見しました.h開発環境に手動で追加します.そこでwww.apuebook.comはソースコードをダウンロードします.
ソースコードを解凍するとディレクトリapueが表示されます.2e.apue.2 eの下のincludeディレクトリのapue.hファイルを/usr/includeにコピーします.
cd apue.2 e所在ディレクトリ
cd include
cp apue.h/usr/include
再コンパイルするか、それとも問題が発生しますか.
/tmp/ccBBopm0.o(.text+0x2b): In function `main': : undefined reference to `err_quit' /tmp/ccBBopm0.o(.text+0x5f): In function `main': : undefined reference to `err_sys' collect2: ld returned 1 exit status
errが見つからなかったのかquit関数とerr_Sys関数の定義.
実はこの2つの関数は『unix環境高度プログラミング』の付録Bで見つけることができます.
ここで解決策を示します.
ディレクトリ/usr/includeに移動し、ファイルmyerrを作成します.h.次のコードをこのファイルに追加します.
#include "apue.h"
#include <errno.h>/* for definition of errno */
#include <stdarg.h> /* ISO C variable aruments */
static void err_doit(int, int, const char *, va_list);
/*
* Nonfatal error related to a system call.
* Print a message and return.
*/
void
err_ret(const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
}
/*
* Fatal error related to a system call.
* Print a message and terminate.
*/
void
err_sys(const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
exit(1);
}
/*
* Fatal error unrelated to a system call.
* Error code passed as explict parameter.
* Print a message and terminate.
*/
void
err_exit(int error, const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(1, error, fmt, ap);
va_end(ap);
exit(1);
}
/*
* Fatal error related to a system call.
* Print a message, dump core, and terminate.
*/
void
err_dump(const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
abort(); /* dump core and terminate */
exit(1); /* shouldn't get here */
}
/*
* Nonfatal error unrelated to a system call.
* Print a message and return.
*/
void
err_msg(const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(0, 0, fmt, ap);
va_end(ap);
}
/*
* Fatal error unrelated to a system call.
* Print a message and terminate.
*/
void
err_quit(const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(0, 0, fmt, ap);
va_end(ap);
exit(1);
}
/*
* Print a message and return to caller.
* Caller specifies "errnoflag".
*/
static void
err_doit(int errnoflag, int error, const char *fmt, va_list ap)
{
char buf[MAXLINE];
vsnprintf(buf, MAXLINE, fmt, ap);
if (errnoflag)
snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s",
strerror(error));
strcat(buf, " ");
fflush(stdout); /* in case stdout and stderr are the same */
fputs(buf, stderr);
fflush(NULL); /* flushes all stdio output streams */
}#include "apue.h"
#include <errno.h>/* for definition of errno */
#include <stdarg.h> /* ISO C variable aruments */
static void err_doit(int, int, const char *, va_list);
/*
* Nonfatal error related to a system call.
* Print a message and return.
*/
void
err_ret(const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
}
/*
* Fatal error related to a system call.
* Print a message and terminate.
*/
void
err_sys(const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
exit(1);
}
/*
* Fatal error unrelated to a system call.
* Error code passed as explict parameter.
* Print a message and terminate.
*/
void
err_exit(int error, const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(1, error, fmt, ap);
va_end(ap);
exit(1);
}
/*
* Fatal error related to a system call.
* Print a message, dump core, and terminate.
*/
void
err_dump(const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
abort(); /* dump core and terminate */
exit(1); /* shouldn't get here */
}
/*
* Nonfatal error unrelated to a system call.
* Print a message and return.
*/
void
err_msg(const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(0, 0, fmt, ap);
va_end(ap);
}
/*
* Fatal error unrelated to a system call.
* Print a message and terminate.
*/
void
err_quit(const char *fmt, ...)
{
va_list ap; va_start(ap, fmt);
err_doit(0, 0, fmt, ap);
va_end(ap);
exit(1);
}
/*
* Print a message and return to caller.
* Caller specifies "errnoflag".
*/
static void
err_doit(int errnoflag, int error, const char *fmt, va_list ap)
{
char buf[MAXLINE];
vsnprintf(buf, MAXLINE, fmt, ap);
if (errnoflag)
snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s",
strerror(error));
strcat(buf, " ");
fflush(stdout); /* in case stdout and stderr are the same */
fputs(buf, stderr);
fflush(NULL); /* flushes all stdio output streams */
}
次に、同じディレクトリの下のapueを編集します.hファイル(このファイルは前のステップで追加されました).最後の行#endif/*_APUE_H*/の前に宣言を追加します.
#include"myerr.h"/*自分で追加したヘッダファイルは、unix環境の高度なプログラミングに便利です*/
次に、ソースコードが存在するディレクトリに移動し、コンパイルします.
gcc -o myls myls.c
今回成功しました.
./myls /usr
次の結果が表示されます.
src .. . lib share sbin include local X11R6 bin games
すべてのフォルダをリストできました.success!
私と同じ初心者に一定の助けがあることを望んでいます.