【问题标题】:Linker error: "linker input file unused because linking not done", undefined reference to a function in that file链接器错误:“链接器输入文件未使用,因为链接未完成”,未定义对该文件中函数的引用
【发布时间】:2011-01-24 14:35:14
【问题描述】:

我的文件链接有问题。

基本上,我的程序包括:

  • 主程序gen1
  • gen1 - 接收发送到str2value 的输入 处理,输出结果str2value,将输入分解为令牌 使用“tokenizer”确定对每个人进行什么样的处理 令牌,并将它们传递给str2numstr2cmd。然后它返回一个 结果数组。
  • str2num - 做一些处理
  • str2cmd - 同上
  • author.py - 一个 Python 脚本,从标头 cmdTable.h 生成 str2cmd.cstr2cmd.h

我很确定我的包含正确,我检查了几次。我还检查了标题中没有条件#ifndef 错误。

这是我的 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

这是我从 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

关于这是什么的任何建议? STR2VALUESFILES 拥有我需要的所有目标文件,用于定义缺失的函数。

【问题讨论】:

    标签: c++ c gcc makefile linker-errors


    【解决方案1】:

    我认为您对编译器如何将事物组合在一起感到困惑。当您使用-c 标志时,即不进行链接,输入是 C++ 代码,输出是目标代码。因此,.o 文件不会与-c 混合使用,编译器会对此发出警告。目标文件中的符号不会像那样移动到其他目标文件中。

    所有目标文件都应该在最后的链接器调用中,这不是这里的情况,所以链接器(通过g++前端调用)抱怨缺少符号。

    这是一个小例子(为清楚起见,显式调用g++):

    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)
    

    X11 还附带了makedepend 实用程序 - 对源代码依赖性有很大帮助。您可能还想查看用于构建 make 规则的 -M gcc 选项。

    【讨论】:

    • 我怎样才能从其他目标文件中构建一个目标文件呢? gen1 与 str2num str2cmd 或 tokenizer 没有直接依赖关系
    • 您单独编译每个.cpp 文件,然后将所有生成的.o 文件链接在一起。我添加了一个简单的make 示例。
    • 忘了提 - 通过头文件而不是目标文件管理依赖关系。
    • 我无法按照您的示例进行操作。我从未见过 $@、@
    • .cpp.o 规则告诉make 如何从带有.cpp 后缀的相应文件中构建带有.o 后缀的文件(例如,如何从main.cpp 中生成main.o$&lt; 是规则的“输入”,$@ 是“输出”(同样,$&lt; 将是 main.cpp$@ 将是 main.o)这些是内置变量 make.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多