Linker input file unused c++ g++ make file

巧了我就是萌 提交于 2019-12-07 11:17:55

问题


I am unable to figure out what is causing this error that I keep getting upon making my project:

i686-apple-darwin11-llvm-g++-4.2: -lncurses: linker input file unused because linking not done

And my make file looks like this:

CC = g++

LIB_FLAGS = -l ncurses

FLAGS = $(LIB_FLAGS)

DEPENDENCIES = window.o element.o

# FINAL OUTPUTS
main: main.cpp $(DEPENDENCIES)
    $(CC) $(FLAGS) -o main.out main.cpp $(DEPENDENCIES)

# MODULES
window.o: main.h classes/window.cpp
    $(CC) $(FLAGS) -c classes/window.cpp

element.o: main.h classes/element.cpp
    $(CC) $(FLAGS) -c classes/element.cpp

# CLEAN
clean:
    rm -rf *.o
    rm main.out

Everything compiles okay, but I'm just curious what is causing this error message and what it means..


回答1:


Do not give link flags when you compile (-c flag) your source files. Take a look for this example makefile (very similar as in makefile docs)

CPP = g++
CPPFLAGS =-Wall -g
OBJECTS = main.o net.o
PREFIX = /usr/local

.SUFFIXES: .cpp .o

.cpp.o:
        $(CPP) $(CPPFLAGS) -c $<

.o:
        $(CPP) $(CPPFLAGS) $^ -o $@

main: $(OBJECTS)
main.o: main.cpp
net.o: net.cpp net.h


.PHONY:
install: main
        mkdir -p $(PREFIX)/bin
        rm -f $(PREFIX)/bin/main
        cp main $(PREFIX)/bin/main


clean:
        rm -f *.o main



回答2:


You are passing linker options to a compiler invocation together with -c, which means that linking is not performed and thereby -l options are unused. In your case, your LIB_FLAGS should not be in FLAGS, but instead specified in the the main: ... rule:

main: main.cpp
        $(CC) $(FLAGS) $(LIB_FLAGS) ...



回答3:


As has been mentioned already you're passing linker-related flags at the compile stage. Usually you want different flags for compiling and linking, e.g.

CC = g++

CPPFLAGS = -Wall -g -c -o $@

LDFLAGS = -l ncurses -o $@

DEPENDENCIES = main.o window.o element.o

# FINAL OUTPUTS
main: $(DEPENDENCIES)
    $(CC) $(LDFLAGS) $(DEPENDENCIES)

# MODULES
main.o: main.h main.cpp
    $(CC) $(CPPFLAGS) main.cpp

window.o: main.h classes/window.cpp
    $(CC) $(CPPFLAGS) classes/window.cpp

element.o: main.h classes/element.cpp
    $(CC) $(CPPFLAGS) classes/element.cpp

# CLEAN
clean:
    -rm main $(DEPENDENCIES)


来源:https://stackoverflow.com/questions/12525579/linker-input-file-unused-c-g-make-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!