JonMorehouse
JonMorehouse

Reputation: 1383

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

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

Upvotes: 4

Views: 15797

Answers (3)

Maxim
Maxim

Reputation: 1666

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

Upvotes: 4

Paul R
Paul R

Reputation: 213060

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)

Upvotes: 3

JesperE
JesperE

Reputation: 64434

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

Upvotes: 5

Related Questions