Undefined reference to error handling functions from "The Linux Programming Interface"

undefined reference to c++ function
linker error undefined reference to function in c
c++ undefined reference to function in header file
c undefined reference to function in same file
undefined reference to gcc
c undefined reference to function in header file
undefined reference to variable in c++
undefined reference to main

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)

For the undefined references, someone asked a very similar answer here How to compile examples in the book "The Linux Programming Interface" by Michael Kerrisk

Then, if that does not work, I would suggest to try again by downloading the source file as specified at the TLPI web site and you can look into the makefile for your script, which is in the fileio/ subfolder and the Makefile.inc file.

Error: undefined reference to 'main' in C, at compile time indicates that the named identifier was used in the named source file, but had no definition in the source file. This is usually caused by a misspelled identifier name, or missing declaration of the identifier used. Now this code will compile. An alternative situation arises where the source for foo() is in a separate source file foo.c (and there's a header foo.h to declare foo() that is included in both foo.c and undefined_reference.c). Then the fix is to link both the object file from foo.c and undefined_reference.c, or to compile both the source files:

I was having the exact same issues with undefined references to his error-handling functions, but if you're doing everything in your own directories my advice may not help. If you downloaded the compressed tarball from his website everything should work. As stated in the BUILDING.txt file it includes, all you have to do is run make from the tlpi-dist or tlpi-book directory, depending on which one you downloaded, and all programs will be compiled and ready to run (subject to some OS specific issues obviously). If you want to modify a program, just add the name you save it as to that directory's Makefile and run make again, and you should be good to go.

C++ Error: undefined reference to `main', For the undefined references, someone asked a very similar answer here How to compile examples in the book "The Linux Programming  Undefined reference/unresolved external symbol errors in C++/C/Objective-c and ways to solve/avoid them. And as the name of function/variable has changed in c++ file, The definition of

You would require to ../lib/error_functions.o file along with your compilation.

gcc -I booklib -o copy copy.c ../lib/error_functions.o

The error functions are defined and declared in lib/error_functions.c lib/error_function.h Make sure you run make from tlpi-dist by which you would have error_functions.o

Understanding "Undefined Symbol" Error Messages., c documentation: Undefined reference errors when linking. gcc undefined_reference.c /tmp/ccoXhwF0.o: In function `main': undefined_reference.​c:(.text+0x15): Translate user input to numbers, extra error checking * should be done here. You've called functions but not provided their definition. There's no way we can tell where those functions are defined. It might be another C file that you're supposed to compile to an object file and link with the main program; it might be supposed to be in a library you're not linking with; it might be some other issue.

I was having similar troubles on Ubuntu 16.04. and MacOSX. Running 'make' always gave errors containing:

No rule to make target `../libtlpi.a'

This has a simple solution for Ubuntu:

sudo apt-get install libcap-dev

then run 'make' in the root directory.

What does "collect2: error: ld returned 1 exit status" mean?, So At the linking time, no such symbol found hence the error. So the declaration of C functions in C++ becomes a bit tricky. it is possible to have same name file in different folder, And Microsoft Visual Studio(Some versions) can't handle it. ex3-1.cc:(.text+0x139): undefined reference to `swap(int, int)'. collect2: ld returned 1 exit status. The declaration of the "swap" function is void swap(int x, int y); Remove the reference from it or add it to the declaration also, they must be the same.

Undefined reference to error handling functions from "The Linux , An “Undefined Reference” error occurs when we have a reference to object name (class, function, variable, etc.) in our program and the linker  Calling functions from an array of pointers in a library causes 'undefined reference to' error 0 Protecting variables and functions within a library that has no class

C Language, I get: …/…/…/build/target/user/platform-6/libuser.a(library.o): In function XXXXXXX(unsigned char*)': XXX/XXXX.cpp:342: undefined reference  Dismiss Join GitHub today. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

Undefined reference/unresolved external symbol errors in C++/C , The error: undefined reference to 'main' in C program is a very stupid mistake by the programmer, it occurs when the main() function does not  Undefined reference means the compiler can't find the definition of the function. Make sure that you are compiling and linking both main.cpp and Dog.cpp (if you use an IDE you should have all files in the same project). Aug 7, 2015 at 3:13pm UTC AbstractionAnon (6421)

Comments
  • It looks like you've misdefined main() — probably int main(int argc, char *argv) (or perhaps int main(int argc, char argv[])) instead of int main(int argc, char **argv). The undefined functions are irresolvable from the information given. You've called functions but not provided their definition. There's no way we can tell where those functions are defined. It might be another C file that you're supposed to compile to an object file and link with the main program; it might be supposed to be in a library you're not linking with; it might be some other issue.
  • Could we see some of your code. It is really impossible to debug anything without looking at the source code. More specifically I would like to look at the signature of main. I suspect you have written int main(int argc, char argv[]) instead of int main(int argc, char* argv[]);
  • @AjayBrahmakshatriya actually I give the link of source code in the question. I didn't enter the code to my question because it would be long. I edited the question.
  • The code from TLPI doesn't compile particularly well on a Mac, it seems. At least, it's giving me more grief than I've time to resolve right now. Basically, you need to get the code from the TLPI lib directory compiled into a library (maybe libtlpi.a or libtlpi.so), and you include -I /path/to/tlpi/lib and -L /path/to/tlpi/lib and -ltlpi on the compiler command lines.
  • Are the missing functions defined earlier in the book? You might need to compile (and link) the appropriate sources for them. In particular, you'll want to make lib/error_functions.c from Chapter 3, at least.