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

15,660

Solution 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

Solution 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) ...

Solution 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)
Share:
15,660

Related videos on Youtube

JonMorehouse
Author by

JonMorehouse

Engineer. Innovator. Entrepreneur.

Updated on October 15, 2022

Comments

  • JonMorehouse
    JonMorehouse over 1 year

    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..

    • Mat
      Mat over 11 years
      Don't passe linker arguments if you're just compiling (-o) and not linking. (BTW, those linker args (-lfoo) should usually go at the end of the command line.)
    • Some programmer dude
      Some programmer dude over 11 years
      @Mat I think you mean -c not -o?
  • Admin
    Admin over 11 years
    By (GNU) convention, you should really change LFLAGS to LDFLAGS.