代码之家  ›  专栏  ›  技术社区  ›  Frames Catherine White

链接器错误:“链接器输入文件未使用,因为链接未完成”,未定义对该文件中某个函数的引用

  •  37
  • Frames Catherine White  · 技术社区  · 14 年前

    我的文件链接有问题。

    基本上,我的程序包括:

    • 主程序, gen1 .
    • GEN1 -接收发送到的输入 str2value 对于 处理,输出结果 STR2值 ,将输入分解为令牌 使用“tokenizer”确定要对每个 令牌,并将它们传递给 str2num str2cmd . 然后返回一个 结果的数组。
    • 斯特朗 -做些处理
    • STR2CMD -同上
    • author.py -生成 str2cmd.c str2cmd.h 从页眉 cmdTable.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 拥有我需要的所有对象文件,以定义缺少的函数。

    1 回复  |  直到 8 年前
        1
  •  46
  •   Mike    11 年前

    我想你对编译器是如何把东西组合在一起感到困惑。当你使用 -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)
    

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