1

I'm fairly new to C++ and I'm trying to set up ncurses, but I can't get it to work. Here's the code:

#include <iostream>
#include <string>
#include <ncurses.h>
      int main(){
        initscr();
        printw("Hello World !!!");
        refresh();
        getch();
        endwin();
        return 0;
    }

With this file I get 'undefined reference' errors

And here is the makefile:

main.o: main.cpp ncurses.h
    g++ main.cpp -o crawler -lncurses

The error I get with the makefile is :

make: *** No rule to make target `ncurses.h', needed by `main.o'.  Stop.

Thanks for your help!

Note: I am using Ubuntu 12.04 with Geany and g++

lijrobert
  • 125
  • 2
  • 9
  • 1
    remove `ncurses.h` from `main.o: main.cpp ncurses.h`, you don't need that, also this is `C` code not `C++`. – user2485710 Feb 12 '14 at 16:37
  • 1
    @user2485710 Can you not use ncurses and C++ together? I thought you could. I've seen it work before. – lijrobert Feb 12 '14 at 16:42
  • @user68537 I believe you can still use ncurses with c/c++, you just don't have to define ncurses.h in the makefile. – John Odom Feb 12 '14 at 16:43
  • yes you can, but the post shouldn't be tagged `c++` since this entire snippet is `C`, the functions that you are using are all from the `C` world, and if you want to use `C` code inside a `C++` application you should use `extern "C"`, Google for more on the keyword `extern` . – user2485710 Feb 12 '14 at 16:44
  • The dependencies in the makefile have to be specified with either absolute paths or paths relative to the current working directory. Normally you don't put library headers as dependencies on the basis that library headers don't get modified. – rici Feb 12 '14 at 16:45
  • Thank you everyone! Also, I'm sorry for putting it under the wrong tag. – lijrobert Feb 12 '14 at 16:55
  • [Related Q about how to handle header dependencies in Makefiles.](https://stackoverflow.com/questions/52034997) Although you rarely should have that need for system headers (which are not in a habit of changing that often). – DevSolar Jul 26 '20 at 07:44

1 Answers1

1

You should remove ncurses.h dependency from Makefile. You Makefile should look like this:

main.o: main.cpp
    g++ main.cpp -o crawler -lncurses

make tries to find ncurses.h in current working directory, but it is not available there. So make indicates error.

Also, there is no need of iostream and string headers in your code, because string header is included by iostream and you are not using any functions from both of headers.

Akib Azmain Turja
  • 1,142
  • 7
  • 27