c - 如何禁用 "curses.h" header (Xcode 6.3 OSX Yosemite 中“stdio.h 的一部分”)以避免函数声明冲突

标签 c macos curses compiler-flags xcode6.3

我正在尝试在 Xcode 中构建一个项目,但出现以下错误函数“clear”的隐式声明在 C99 中无效“clear”的类型冲突.

代码如下:

//main.c
#include <stdio.h>
#include "tree.h"

int main(){
  clear(); // Implicit declaration of function 'clear' is invalid in C99
  return 0;
}

//tree.c
#include <stdio.h>
#include "tree.h"
void clear(){ ///Conflicting types for 'clear'
  printf("Command clear.\n");
}

//tree.h
#include <stdio.h>

void clear(); ///Conflicting types for 'clear'

为什么我会收到这些错误和警告?我尝试在 StackOverflow 上搜索解决方案,但所有相关答案都涉及没有某种 #include 的情况。

“clear”不是 C 中的关键字,所以情况并非如此,是吗?
(来源:http://aboutc.weebly.com/keywords.html)

相关主题(尽管它们实际上相关,但不要回答我的问题):

感谢您的帮助。


更新!

事实证明,将 clear 函数的名称更改为 cleark 函数解决了该问题。尽管如此,它对我来说还没有任何意义。


更新2!

我的项目基于 Mac OS 10.10 上 Xcode 6.3 的命令行工具模板。因此,Xcode 已自动向项目的编译器添加一些库和标志。这里最重要的是 curses.h header 已被添加,并且该 header 已经包含 clear() 函数

>

这是“clear”的冲突类型错误日志: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/curses.h:541:28:先前的声明位于此处

我尝试手动从编译器标志中删除 -lcurses,但找不到此类设置。还有其他方法来构建项目吗? (总而言之,我的目标是在项目扩展时能够使用 Xcode 的调试器)


更新3! 根据 Paul Griffiths 在下面的评论中发现并发表的内容,问题如下:

I can indeed replicate this problem with Xcode 6.3.1 with only the code presented. For some reason, stdio.h seems to be including curses.h (i.e. if you don't include stdio.h, this issue goes away), and I haven't been quickly able to find a way to stop it doing that. This seems to be problematic, since standard headers should not import random symbols into the global namespace without an easy and obvious way to turn it off.

最佳答案

通常我运行 C 预处理器来查看编译器实际解析的内容。但是,遵循 Xcode Preprocessor Output 使用 Xcode 检查预处理器输出并不能实现这一点 - 它将 #include 转换为 @import。这是预处理器 View 向我显示的内容:

// Preprocessed output for tree.c
// Generated at 9:24:57 PM on Friday, May 1, 2015
// Using Debug configuration, x86_64 architecture for curses-vs-stdio target of curses-vs-stdio project

# 1 "/Users/thomas/Desktop/curses-vs-stdio/curses-vs-stdio/tree.c"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 322 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "/Users/thomas/Desktop/curses-vs-stdio/curses-vs-stdio/tree.c" 2



# 1 "/Users/thomas/Desktop/curses-vs-stdio/curses-vs-stdio/tree.h" 1


void clear(void);

@import Darwin.C.stdio; /* clang -E: implicit import for "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/stdio.h" */
# 5 "/Users/thomas/Desktop/curses-vs-stdio/curses-vs-stdio/tree.c" 2

void clear(void) {
    printf("Command clear.\n");
}

显然,问题在于 Xcode 使用模块而不是,而不是 stdio.h,包括 curses.h。 “ Darwin ”模块就是问题所在。

事实上,如果我禁用模块(使用 Enable Clang Modules, Disable Auto Linking 中的提示),构建问题就会消失。这是

  • 在build设置中
  • Apple LLVM 6.1 - 语言 - 模块
  • 设置启用模块(C 和 Objective-C)

作为对该问题的进一步提示,稍微重新安排了示例(将原型(prototype)放在包含之前),我看到一条消息提示重载 - 但这不是 C。

也许苹果会在下一个版本中修复这个问题。

关于c - 如何禁用 "curses.h" header (Xcode 6.3 OSX Yosemite 中“stdio.h 的一部分”)以避免函数声明冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29809959/

相关文章:

c - 套接字在 linux 上工作但在 osx 上不工作

c++ - ncurses非阻塞读取将光标推到窗口底部

Python 诅咒 : module function vs instance function

c - 我应该向 isdigit() 传递什么才能使其正常工作?

C 变量的地址范围

c - 为什么 x - y 在这个函数中不会溢出 TMin?为什么在这种情况下功能错误?

objective-c - 如何散列目录的(可能是递归的)内容

java - 将整个项目导入 Maven (Eclipse Juno)

python - 如何从标准输入获取输入,使用curses 显示某些内容并输出到标准输出?

c - 哪个格式说明符