makefileg++ldflags

C++: Including an external library in Makefile


So, I have some trouble including the ncurses-library in a C++ program. It seems my Makefile isn't set correctly and the library-functions can't be found. I installed the library with "sudo apt-get install libncurses5-dev libncursesw5-dev" and I'm able to compile my code manually via "g++ -o output src/main.cpp -lncurses".

The compiler settings in my Makefile looked like this:

CC = g++
CXXFLAGS = -std=c++11 -Wall`
LDFLAGS =
LDLIBS = -lncurses 

I'm using the "C/C++ Makefile Project" Plugin within Visual Studios Code on ubuntu.


Solution

  • Edit: As MadScientist explained, the second option follows the convention.

    So, I found two solutions and I'm not sure which one or if any of them is the desired way of doing it:

    1. Set LDFLAGS = -lncurses

    2. Add $(LDLIBS) to a line in the Makefile:

    # Builds the app
    $(APPNAME): $(OBJ)
       $(CC) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) $(LDLIBS)