Linker error: “linker input file unused because linking not done”, undefined reference to a function in that file

后端 未结 1 528
遇见更好的自我
遇见更好的自我 2020-12-24 05:05

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

Basically, my program consists of:

  • The main program, gen1.
  • gen1 -
相关标签:
1条回答
  • 2020-12-24 05:59

    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.

    0 讨论(0)
提交回复
热议问题