G++似乎忽略了 #ifdef #include

G++ seems to be ignoring #ifdef for an #include

本文关键字:#ifdef #include G++      更新时间:2023-10-16

环境乌班图16.04G++ 5.3.1

我有一个头文件,其中包含以下内容,旨在根据平台包含不同的.h文件:

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

这在 Windows 中工作正常,但在 Ubuntu 中我收到有关 curses.h 文件的错误:

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

这在编译时:

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

为什么headers/curses.h,这是PDCurses的Windows特定文件在这里涉及?

/usr/include/unctrl.h包含以下行:

#include <curses.h>

由于您已经告诉编译器在 headers/ 文件夹中查找带有 -Iheaders 标志的头文件,编译器会选取该文件夹中的curses.h

所以你需要删除-Iheaders标志(例如使用 #include"headers/header_name.h"),或者你需要重命名你的headers/curses.h以免与/usr/include/curses.h发生冲突

在您的 g++ 版本中,-I 选项不是将特定于应用程序的头文件(#include那些不应该在系统头中找到的头文件)添加到搜索路径的正确方法(此更改也让我感到惊讶)。

相反,您应该使用 -iquote headers .

看到这个答案:如何告诉 g++ 编译器在哪里搜索包含文件? 和这个官方文档