【问题标题】:adding empty c++ file to xcode project creates syntax errors in obj-c header file将空 c++ 文件添加到 xcode 项目会在 obj-c 头文件中创建语法错误
【发布时间】:2016-01-07 22:46:10
【问题描述】:

我正在向使用 card.io 项目的现有 iphone 应用程序(由其他人编写)添加代码。这是我尝试的第一个更改。该应用程序编译并运行良好,没有任何更改,但是当我尝试将空的 .cpp 和 .h 文件添加到我的 xcode 项目时,我开始在名为 CardIOMacros.h 的不相关 obj-c 头文件中出现语法错误。我正在使用 xcode 7.2 并为 iphone 4s 设备进行编译。

我添加 .cpp 和 .h 文件的过程是

  1. 右键单击项目导航器中的项目节点
  2. 点击新文件
  3. 点击 c++ 图标
  4. 输入文件名并检查创建头文件选项
  5. 选择我的项目目录中的子目录。
  6. 按主菜单中的构建和运行按钮

以下是我遇到错误的文件。错误显示在代码下方。

//
//  CardIOMacros.h
//  See the file "LICENSE.md" for the full license governing this code.
//

// CardIOLog is a replacement for NSLog that logs iff CARDIO_DEBUG is set.

#if CARDIO_DEBUG
#define CardIOLog(format, args...) NSLog(format, ## args)
#else
#define CardIOLog(format, args...)
#endif

@interface CardIOMacros : NSObject

+ (id)localSettingForKey:(NSString *)key defaultValue:(NSString *)defaultValue productionValue:(NSString *)productionValue;

+ (NSUInteger)deviceSystemMajorVersion; 

+ (BOOL)appHasViewControllerBasedStatusBar; 

@end

#define iOS_MAJOR_VERSION  [CardIOMacros deviceSystemMajorVersion]
#define iOS_8_PLUS         (iOS_MAJOR_VERSION >= 8)
#define iOS_7_PLUS         (iOS_MAJOR_VERSION >= 7)
#define iOS_6              (iOS_MAJOR_VERSION == 6)
#define iOS_5              (iOS_MAJOR_VERSION == 5)

编译器错误

/Users/michael/sdev/projectname/ios/card.io-iOS-20151210/Classes/CardIOMacros.h
/Users/michael/sdev/projectname/ios/card.io-iOS-20151210/Classes/CardIOMacros.h:14:1: Expected unqualified-id
/Users/michael/sdev/projectname/ios/card.io-iOS-20151210/Classes/CardIOMacros.h:18:1: Expected external declaration
/Users/michael/sdev/projectname/ios/card.io-iOS-20151210/Classes/CardIOMacros.h:18:4: C++ requires a type specifier for all declarations
/Users/michael/sdev/projectname/ios/card.io-iOS-20151210/Classes/CardIOMacros.h:18:15: Expected ';' after top level declarator
/Users/michael/sdev/projectname/ios/card.io-iOS-20151210/Classes/CardIOMacros.h:20:1: Expected external declaration
/Users/michael/sdev/projectname/ios/card.io-iOS-20151210/Classes/CardIOMacros.h:20:4: C++ requires a type specifier for all declarations
/Users/michael/sdev/projectname/ios/card.io-iOS-20151210/Classes/CardIOMacros.h:20:9: Expected ';' after top level declarator
/Users/michael/sdev/projectname/ios/card.io-iOS-20151210/Classes/CardIOMacros.h:22:1: Expected unqualified-id

.h 和 .cpp 文件是空的,我什至没有在任何其他文件中 #include 新的 c++ 头文件。任何帮助将不胜感激。

【问题讨论】:

    标签: c++ ios objective-c xcode


    【解决方案1】:

    确保您没有在...-Prefix.pch 中包含上述标题。

    【讨论】:

    • 我该如何检查?
    • Xcode 项目通常在“支持文件”组下有一个PROJECT_NAME-Prefix.pch 文件。编译器的行为就像该文件包含在每个源文件中一样。因此,如果您在此前缀标头中有 Objective-C 代码(或包含包含 Objective-C 的文件),即使是空的 cpp 也将无法编译,因为 Xcode 会尝试使用其 C++ 编译器从前缀标头构建 Objective-C 代码.
    • 就是这样。 -Prefix.pch 文件之一中有“#import CardIOMacros.h”。我将它包装在“#ifdef OBJC #endif”宏中,它可以工作!谢谢你,你太棒了!
    猜你喜欢
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多