【问题标题】:error: ‘NSString’ undeclared (first use in this function)错误:“NSString”未声明(在此函数中首次使用)
【发布时间】:2012-04-30 10:42:19
【问题描述】:

当我尝试在 cygwin Windows 7 中编译 Object-c 程序时出现此错误,但该程序在 Xcode 中执行。

main.m:5:3: error: ‘NSString’ undeclared (第一次在这个函数中使用)

#include <stdio.h>    
int main (int argc, const char * argv[]) 

  {
        NSString *str1 = @"1st string";
        NSString *str2 = @"2nd string";         
        NSLog(@"Hello, World!");    
    return 0;
}

在cygwin中使用如下CMD执行,

gcc -c -Wno-import main.m

你能帮我解决这个编译错误吗?

【问题讨论】:

    标签: objective-c ios cocoa-touch cocoa


    【解决方案1】:

    Windows 不附带 Foundation 库,因此默认情况下 NSString 不可用。你应该试试GNUstep,或者你可以在Mac上使用Cocotron从Xcode交叉编译。无论您选择哪种方式,请查看其文档以了解如何使用它(至少您需要#import &lt;Foundation/Foundation.h&gt; 并链接 Foundation 库)。

    【讨论】:

      【解决方案2】:

      确保您的 .h 或 .m 文件顶部有以下代码:

      #import <UIKit/UIKit.h>
      #import <Foundation/Foundation.h>
      

      您也可以将代码放在 Prefix.pch 文件中。

      【讨论】:

        猜你喜欢
        • 2016-05-09
        • 2018-10-03
        • 1970-01-01
        • 2015-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多