G++似乎忽略了 #ifdef #include
G++ seems to be ignoring #ifdef for an #include
环境乌班图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++ 编译器在哪里搜索包含文件? 和这个官方文档
相关文章:
- 既然存在危险,为什么项目要使用-I include开关
- 有充分的理由在h文件中使用include保护而不是cpp文件吗
- 如何将更多文件夹添加到c++include路径
- 什么是"#include <boost/functional/hash.hpp> "?
- #ifdef和未声明的标识符
- C++ Macros #ifdef
- 对于MacOS上的G++,如何添加默认的include目录/usr/local/include和默认的库搜索路径/usr
- C++包含来自 #include "DevEngine/Core.h" 的错误
- <filesystem> 在 clang 6 和 10 上 #include 错误
- 在 void 函数中使用 #include 变量C++
- N-API include an .so or dll
- 允许哪些令牌作为 #include 的参数?
- 有没有办法在不使用 #ifdef 的情况下不编译发布版本中的单元测试函数体?
- GCC 包含标头(使用"-include")CMake 未检测到的更改
- WebAssembly include OpenCV
- 如何在 c++ 中正确指定 #include 路径以使程序可移植
- 修改生成文件以简化框架 #include 路径
- 更新 #ifdef 中的现有变量值?
- VS2015/VC++ 在新类模板中禁用默认 #include "stdafx.h"
- G++似乎忽略了 #ifdef #include