1

Environment Ubuntu 16.04 G++ 5.3.1

I have a header file with the following intended to include a different .h file depending on platform:

#ifdef _WIN32 
#include "curses.h"
#else
#include <ncurses.h>
#endif

This works fine in windows but in Ubuntu I get errors about the curses.h file:

In file included from /usr/include/unctrl.h:54:0,
                 from /usr/include/curses.h:1694,
                 from headers/command_window.h:8,
                 from command_window.cpp:1:
headers/curses.h:900:19: error: macro "clear" passed 1 arguments, but takes just 0
int     clear(void);

This when compiling with:

g++  -g -lncurses -std=c++11  -Iheaders  -c -o command_window.o command_window.cpp

Why is headers/curses.h, which is the windows specific file for PDCurses being involved here at all?

jackmott
  • 1,112
  • 8
  • 16
  • Use [this old answer](http://stackoverflow.com/a/2224357/440558) to learn how to dump all predefined macros. You can also stop after preprocessing to see the source as the compiler sees it. – Some programmer dude May 23 '16 at 13:55
  • _WIN32 does not appear to be in the list of defines using that old answer – jackmott May 23 '16 at 13:59

2 Answers2

4

/usr/include/unctrl.h contains this line:

#include <curses.h>

And since you've told the compiler to look in your headers/ folder for header files with the -Iheaders flag , the compiler picks up curses.h in that folder.

So you need to drop the -Iheaders flag (and e.g. use #include "headers/header_name.h") or you need to rename your headers/curses.h to not collide with /usr/include/curses.h

nos
  • 223,662
  • 58
  • 417
  • 506
  • This seems a bit off, your supposition is that it's using the system include syntax, but the `-I` option doesn't affect the search path for system headers. In particular, *Any directories you specify with `-I` options before the `-I-` option are searched only for the case of `#include "file"`; they are not searched for `#include `.* – Ben Voigt May 23 '16 at 14:07
  • Hmm, found the right version of the g++ docs and it looks like this was intentionally broken: *Please use `-iquote` instead for `-I` directories before the `-I-` and remove the `-I-` option. Any directories you specify with `-I` options before the `-I-` option are searched only for the case of `#include "file"`; they are not searched for `#include `. If additional directories are specified with `-I` options after the `-I-` option, these directories are searched for all `#include` directives. (Ordinarily all `-I` directories are used this way.) * – Ben Voigt May 23 '16 at 14:12
  • @BenVoigt The docs here, https://gcc.gnu.org/onlinedocs/cpp/Search-Path.html, states that directories added with `-I` are searched before any default directories (such as /usr/include) for #include files encased in `` - and that matches my experience. The main difference with gcc between `"header"` and `
    ` is that "header" first relative to the file, and that `-I.` and `-I-` has special meanings.
    – nos May 23 '16 at 14:26
  • There are two factors: the order, and whether `""` or `<>` is used in the `#include` to name the file. – Ben Voigt May 23 '16 at 14:27
  • 1
    @BenVoigt Directories added with `-I` makes gcc search those before any system directories (such as /usr/include/) in both the `""` and the `<>` case. – nos May 23 '16 at 14:32
  • Thank you both. Both answers solved my problem but can accept only one! – jackmott May 23 '16 at 15:49
2

In your version of g++, the -I option is not the correct way to add application-specific header files (those that shouldn't be found by #include in system headers) to the search path (this change surprised me as well).

Instead, you should use -iquote headers.

See this answer: How to tell g++ compiler where to search for include files? and this official documentation

Community
  • 1
  • 1
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720