gpt4 book ai didi

c - "The Linux Programming Interface"中对错误处理函数的 undefined reference

转载 作者:行者123 更新时间:2023-11-30 18:36:13 26 4
gpt4 key购买 nike

我尝试运行一本名为“Linux 编程接口(interface)”的书中的示例程序。我将所有用户定义的头文件和函数从本书的官方网站复制到我的 booklib 位置。当我编译程序时,我发现了这些错误。我需要有关“对 [functions_name] 的 undefined reference **”的帮助。
代码:

    #include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <stdio.h>

#include "tlpi_hdr.h"

#ifndef BUF_SIZE
#define BUF_SIZE 1024
#endif

int main(int argc, char *argv[])
{

int inputFd, outputFd, openFlags;
mode_t filePerms;
ssize_t numRead;
char buf[BUF_SIZE];

if(argc != 3 || strcmp(argv[1], "--help") == 0)
{
usageErr("%s old-file new-file\n", argv[0]);
}

//open input old-file
inputFd = open(argv[1], O_RDONLY);

//error check
if(inputFd == -1)
{
errExit("opening file %s", argv[1]);
}

openFlags = O_CREAT | O_WRONLY | O_TRUNC;
filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;

//open output the new-file
outputFd = open(argv[2], openFlags, filePerms);

if(outputFd == -1)
{
errExit("opening file %s", argv[2]);
}

//transfer data until we encounter end of input or an error
while((numRead = read(inputFd, buf, BUF_SIZE)) > 0)
{
if(write(outputFd, buf, numRead) != numRead)
fatal("couldn't write whole buffer");
if(numRead == -1)
errExit("read");

if(close(inputFd) == -1)
errExit("close input");

if(close(outputFd) == -1)
errExit("close output");

}

exit(EXIT_SUCCESS);
}


可以看到用户定义的头文件from here .

$gcc -I booklib -o copy copy.c 

c
/tmp/ccqC9Tg9.o: In function `main':
copy.c:(.text+0x62): undefined reference to `usageErr'
copy.c:(.text+0xb3): undefined reference to `errExit'
copy.c:(.text+0x11c): undefined reference to `errExit'
copy.c:(.text+0x14b): undefined reference to `fatal'
copy.c:(.text+0x163): undefined reference to `errExit'
copy.c:(.text+0x183): undefined reference to `errExit'
copy.c:(.text+0x1a3): undefined reference to `errExit'
collect2: error: ld returned 1 exit status

gcc -v 输出:

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 6.3.0-6' --with-bugurl=file:///usr/share/doc/gcc-6/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-6 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-6-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-6-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-6-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 6.3.0 20170205 (Debian 6.3.0-6)

最佳答案

对于 undefined reference ,有人在这里提出了非常相似的答案How to compile examples in the book "The Linux Programming Interface" by Michael Kerrisk

然后,如果这不起作用,我建议通过下载 TLPI web site 中指定的源文件来重试。您可以查看脚本的 makefile,它位于 fileio/子文件夹和 Makefile.inc 文件中。

关于c - "The Linux Programming Interface"中对错误处理函数的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42516673/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com