手动在apue中编译代码但出现错误
compile code in apue manually but get error
apue的appendixB的代码是我自己敲出来的。但是当我第一次测试时,我得到了一个错误。
`(master)⚡ [1] % clang -o myls myls.c apue.c` `~/Code/c/apue`
/tmp/apue-cf5ea0.o: In function `log_open':
apue.c:(.text+0xb95): undefined reference to `log_to_stderr'
/tmp/apue-cf5ea0.o: In function `log_doit':
apue.c:(.text+0xe28): undefined reference to `log_to_stderr'
x86_64-pc-linux-gnu-clang-3.5.0: error: linker command failed with exit code 1 (use -v to see invocation)
这是我的一部分 apue.c :
#include <errno.h>
#include <stdarg.h>
#include <syslog.h>
#include "apue.h"
static void log_doit(int, int, int, const char *, va_list ap);
extern int log_to_stderr;
void log_open(const char *ident, int option, int facility) {
if (log_to_stderr == 0)
openlog(ident, option, facility);
}
static void log_doit(int errnoflag, int error, int priority, const char *fmt,
va_list ap) {
char buf[MAXLINE];
vsnprintf(buf, MAXLINE-1, fmt, ap);
if (errnoflag)
snprintf(buf+strlen(buf), MAXLINE-strlen(buf)-1, ": %s",
strerror(error));
strcat(buf, "\n");
if (log_to_stderr) {
fflush(stdout);
fputs(buf, stderr);
fflush(stderr);
} else {
syslog(priority, "%s", buf);
}
}
我输入了 extern int log_to_stderr
,但为什么我也得到错误?
我正在使用 linux-gentoo。
如果你使用extern
声明一个元素而不定义它(在变量声明一个变量而不初始化它的情况下),声明的元素的内存没有分配,这会导致编译错误。
确保您已在 myls.c
中声明 log_to_stderr
:
int log_to_stderr;
apue的appendixB的代码是我自己敲出来的。但是当我第一次测试时,我得到了一个错误。
`(master)⚡ [1] % clang -o myls myls.c apue.c` `~/Code/c/apue`
/tmp/apue-cf5ea0.o: In function `log_open':
apue.c:(.text+0xb95): undefined reference to `log_to_stderr'
/tmp/apue-cf5ea0.o: In function `log_doit':
apue.c:(.text+0xe28): undefined reference to `log_to_stderr'
x86_64-pc-linux-gnu-clang-3.5.0: error: linker command failed with exit code 1 (use -v to see invocation)
这是我的一部分 apue.c :
#include <errno.h>
#include <stdarg.h>
#include <syslog.h>
#include "apue.h"
static void log_doit(int, int, int, const char *, va_list ap);
extern int log_to_stderr;
void log_open(const char *ident, int option, int facility) {
if (log_to_stderr == 0)
openlog(ident, option, facility);
}
static void log_doit(int errnoflag, int error, int priority, const char *fmt,
va_list ap) {
char buf[MAXLINE];
vsnprintf(buf, MAXLINE-1, fmt, ap);
if (errnoflag)
snprintf(buf+strlen(buf), MAXLINE-strlen(buf)-1, ": %s",
strerror(error));
strcat(buf, "\n");
if (log_to_stderr) {
fflush(stdout);
fputs(buf, stderr);
fflush(stderr);
} else {
syslog(priority, "%s", buf);
}
}
我输入了 extern int log_to_stderr
,但为什么我也得到错误?
我正在使用 linux-gentoo。
如果你使用extern
声明一个元素而不定义它(在变量声明一个变量而不初始化它的情况下),声明的元素的内存没有分配,这会导致编译错误。
确保您已在 myls.c
中声明 log_to_stderr
:
int log_to_stderr;