[c++] Linker error: "linker input file unused because linking not done", undefined reference to a function in that file

I'm having trouble with the linking of my files.

Basically, my program consists of:

  • The main program, gen1.
  • gen1 - receives input sends to str2value for processing, outputs results str2value, breaks input into tokens using "tokenizer" determines what sort of processing to do to each token, and passes them off to str2num, or str2cmd. It then returns an array of the results.
  • str2num - does some processing
  • str2cmd - ditto
  • author.py - a python script that generates str2cmd.c and str2cmd.h from a header cmdTable.h.

I'm pretty sure I have my includes right, I've checked a couple of times. I've also checked that there are no conditions #ifndef wrong in the headers.

Here is my Makefile:

#CPP = g++ -lserial
CPP = g++ -DTESTMODE
C= gcc
DEFINES = LURC
CFLAGS = -Wall -fshort-enums -D$(DEFINES)
PROJECTFILES = gen1.cpp str2value.o

STR2VALUEFILES = str2value.cpp str2cmd.o str2num.o tokenizer.o str2value.h

gen1 : $(PROJECTFILES)
        $(CPP) $(CFLAGS) -o gen1 $(PROJECTFILES)



str2value.o : $(STR2VALUEFILES)
#       echo "str2value"
        $(CPP) $(CFLAGS) -c $(STR2VALUEFILES)

str2num.o: str2num.cpp  str2value.h str2num.hpp
         $(C) $(CFLAGS) -c $^


tokenizer.o: tokenizer.cpp tokenizer.hpp
        $(CPP) $(CFLAGS) -c $^

str2cmd.o : authorCMDs.py cmdTable.h
        python authorCMDs.py cmdTable.h str2cmd #this uses the gcc -E cmdTable.h -DLURC
        $(C) $(CFLAGS) -c str2cmd.c str2cmd.h

#TODO: add a thing that checks str2cmd.h/.c has not been modified by hand



.PHONEY: clean
clean:
        rm *.o

.PHONEY: all
all:
        clear
        make clean
        make

Here is the output I recieve from make all:

make clean
make[1]: Entering directory `/home/frames/LURC/gen1/gen1Source'
rm *.o
make[1]: Leaving directory `/home/frames/LURC/gen1/gen1Source'
make
make[1]: Entering directory `/home/frames/LURC/gen1/gen1Source'
python authorCMDs.py cmdTable.h str2cmd #this uses the gcc -E cmdTable.h -DLURC
str2cmd.c and str2cmd.h, generated from cmdTable.h

gcc  -Wall -fshort-enums -DLURC -c str2cmd.c str2cmd.h
gcc  -Wall -fshort-enums -DLURC -c str2num.cpp str2value.h str2num.hpp
g++ -DTESTMODE -Wall -fshort-enums -DLURC -c tokenizer.cpp tokenizer.hpp
g++ -DTESTMODE -Wall -fshort-enums -DLURC -c str2value.cpp str2cmd.o str2num.o tokenizer.o str2value.h
g++: str2cmd.o: linker input file unused because linking not done
g++: str2num.o: linker input file unused because linking not done
g++: tokenizer.o: linker input file unused because linking not done
g++ -DTESTMODE -Wall -fshort-enums -DLURC -o gen1 gen1.cpp str2value.o
str2value.o: In function `getValue(char*)':
str2value.cpp:(.text+0xbd): undefined reference to `str2cmd(char*)'
str2value.cpp:(.text+0x102): undefined reference to `str2num(char*)'
str2value.o: In function `getAllValues(char*)':
str2value.cpp:(.text+0x164): undefined reference to `tokenizer::tokenizer(char*)'
str2value.cpp:(.text+0x177): undefined reference to `tokenizer::getNumTokens(char const*)'
str2value.cpp:(.text+0x1a9): undefined reference to `tokenizer::getNextToken(char const*)'
str2value.cpp:(.text+0x1e9): undefined reference to `tokenizer::getNumTokens(char const*)'
str2value.cpp:(.text+0x201): undefined reference to `tokenizer::~tokenizer()'
str2value.cpp:(.text+0x25b): undefined reference to `tokenizer::~tokenizer()'
collect2: ld returned 1 exit status
make[1]: *** [gen1] Error 1
make[1]: Leaving directory `/home/frames/LURC/gen1/gen1Source'
make: *** [all] Error 2

Any suggestions about what this is about? STR2VALUESFILES has all the object files I need, to define the missing functions.

This question is related to c++ c gcc makefile linker-errors

The answer is


I think you are confused about how the compiler puts things together. When you use -c flag, i.e. no linking is done, the input is C++ code, and the output is object code. The .o files thus don't mix with -c, and compiler warns you about that. Symbols from object file are not moved to other object files like that.

All object files should be on the final linker invocation, which is not the case here, so linker (called via g++ front-end) complains about missing symbols.

Here's a small example (calling g++ explicitly for clarity):

PROG ?= myprog
OBJS = worker.o main.o

all: $(PROG)

.cpp.o:
        g++ -Wall -pedantic -ggdb -O2 -c -o $@ $<

$(PROG): $(OBJS)
        g++ -Wall -pedantic -ggdb -O2 -o $@ $(OBJS)

There's also makedepend utility that comes with X11 - helps a lot with source code dependencies. You might also want to look at the -M gcc option for building make rules.


Examples related to c++

Method Call Chaining; returning a pointer vs a reference? How can I tell if an algorithm is efficient? Difference between opening a file in binary vs text How can compare-and-swap be used for a wait-free mutual exclusion for any shared data structure? Install Qt on Ubuntu #include errors detected in vscode Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio 2017 errors on standard headers How do I check if a Key is pressed on C++

Examples related to c

conflicting types for 'outchar' Can't compile C program on a Mac after upgrade to Mojave Program to find largest and second largest number in array Prime numbers between 1 to 100 in C Programming Language In c, in bool, true == 1 and false == 0? How I can print to stderr in C? Visual Studio Code includePath "error: assignment to expression with array type error" when I assign a struct field (C) Compiling an application for use in highly radioactive environments How can you print multiple variables inside a string using printf?

Examples related to gcc

Can't compile C program on a Mac after upgrade to Mojave Compiling an application for use in highly radioactive environments Make Error 127 when running trying to compile code How to Install gcc 5.3 with yum on CentOS 7.2? How does one set up the Visual Studio Code compiler/debugger to GCC? How do I set up CLion to compile and run? CMake error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found How to printf a 64-bit integer as hex? Differences between arm64 and aarch64 Fatal error: iostream: No such file or directory in compiling C program using GCC

Examples related to makefile

MINGW64 "make build" error: "bash: make: command not found" "No rule to make target 'install'"... But Makefile exists How to overcome "'aclocal-1.15' is missing on your system" warning? How to install and use "make" in Windows? How to get a shell environment variable in a makefile? Using local makefile for CLion instead of CMake Difference between using Makefile and CMake to compile the code How to set child process' environment variable in Makefile Windows 7 - 'make' is not recognized as an internal or external command, operable program or batch file Debugging the error "gcc: error: x86_64-linux-gnu-gcc: No such file or directory"

Examples related to linker-errors

Duplicate Symbols for Architecture arm64 Multiple definition of ... linker error What is an undefined reference/unresolved external symbol error and how do I fix it? Why am I getting "undefined reference to sqrt" error even though I include math.h header? What causes the error "undefined reference to (some function)"? Undefined reference to 'vtable for xxx' C error: undefined reference to function, but it IS defined Undefined reference to sqrt (or other mathematical functions) Undefined reference to `sin` Linker error: "linker input file unused because linking not done", undefined reference to a function in that file