【发布时间】:2017-10-26 21:34:05
【问题描述】:
首先我很抱歉标题。我真的不知道如何才能更好地描述我的问题。
使用 XCode 时,我遇到的问题是“typedefs”和“#defines”似乎只对写入它们的文件可见。
假设我有三个文件。 main.c、Foo.h、Foo.c
main.c:
#include <stdio.h>
#include <stdlib.h>
typedef int simpleInteger;
#include "Foo.h"
int main(int argc, const char * argv[]) {
simpleInteger I = 22;
printf("%d\n", Foo(I));
return 0;
}
Foo.h:
#ifndef Foo_h
#define Foo_h
simpleInteger Foo(simpleInteger number);
#endif /* Foo_h */
Foo.c:
#include "Foo.h"
int Foo(simpleInteger number)
{
return number*2;
}
当我尝试编译它时,XCode 在 Foo.h 和 Foo.c 中抛出错误“Unknown type name 'simpleInteger'” >.
要使其正常工作,我必须在 Foo.h 中包含“typedef int simpleInteger”行,这对我来说似乎并不干净。但是,如果我在不使用 XCode 的情况下编译这些文件,它就可以完美运行。
我怎样才能告诉 XCode 不要抱怨这个并让它像任何其他编译器一样工作?
【问题讨论】:
-
simpleInteger仅在main.c的范围内定义。你需要了解作用域,以及#include(和其他预处理器的东西)是如何工作的。它并不复杂,但很关键。 -
好的,这解决了我的问题,但如果在
main.c中定义它对我来说似乎更自然,因为main.c也使用这个typedef。此外,我觉得很奇怪,只有 XCode 抱怨这一点,但即使没有警告也可以用 clang 编译。