1

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.

T. Pieper
  • 36
  • 4
  • 1
    We can't help you unless you tell us what the problem is. _I have some trouble_ is not an issue we can address. – MadScientist Mar 10 '20 at 15:55
  • Hey, the lib-include didn't work, so i got an "undefined reference"-error to `initscr' and other curses-functions. It's working now though with LDFLAGS = -lncurses. – T. Pieper Mar 10 '20 at 16:05

1 Answers1

0

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)
T. Pieper
  • 36
  • 4
  • 2
    You can do it however you want. However if you want to follow _convention_, `LDFLAGS` should be used for linker options such as `-L`, while `LDLIBS` is used for libraries like `-lncurses`. This is because in order for linking to work properly, libraries always must come _last_ on the link line while often linker options would come earlier. – MadScientist Mar 10 '20 at 17:44
  • Note well that `make` variables such as `LDLIBS` have no magical properties. They are used only as specified by the rules in which they appear. Some are a little special in that they appear in one or more of `make`'s implicit rules, but that's irrelevant as soon as you have an *explicit* rule applicable to building the desired target. – John Bollinger Mar 11 '20 at 14:02