I tried to run an example program from a book called 'The Linux Programming Interface'. I copied all user-defined header files and functions from official website of book to my booklib location.
When I compiled the program, I took these errors. I need help about'Undefined reference to [functions_name]**.
code:
#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);
}
You can see user-defined header files 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 output:
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)