xcode 14.2从C++调用Objc方法时出错[重复]

dw1jzc5e  于 2023-02-25  发布在  其他
关注(0)|答案(1)|浏览(167)
    • 此问题在此处已有答案**:

How to call functions in c++ and objective-c to each other(1个答案)
昨天关门了。
社区正在审查是否从昨天开始重新讨论这个问题。
我正在使用xcode 14.2版本。使用Objective-Cc++创建应用程序时出错。
objective-c调用c++函数是可以的,但是当我从c++调用objective-c函数时,出现了错误。
在看到一篇文章说为了同时使用"objective-c"和"c++",". m"扩展名应该修改为". mm"之后,我将扩展名改为". mm"。

    • 对象控制器**
///objcSum.h

#ifndef objcSum_h
#define objcSum_h

#import <Foundation/Foundation.h>
#import "mycpp.hpp"

@interface TDWObject : NSObject

-(NSInteger)addNum:(NSInteger)lhs second:(NSInteger)rhs;
-(NSInteger)getNum;

@end

int TDWObjcSum(int num1, int num2);

#endif /* objcSum_h */
///objcSum.mm

#import "objcSum.h"

@implementation TDWObject

-(NSInteger)addNum:(NSInteger)lhs second:(NSInteger)rhs
{
    return lhs + rhs;
}

-(NSInteger)getNum
{
    MySum mysum;
    NSInteger num = mysum.ObjcCall(10, 100);
    return num;
}

@end

int TDWObjcSum(int num1, int num2)
{
    TDWObject* tdwObject = [[TDWObject alloc]init];
    int res = [tdwObject addNum:num1 second:num2];
    [tdwObject release];
    return res;
}
    • C++ **

一个二个一个一个

    • 调用实际函数的代码**
//AppDelegate.mm

TDWObject* tdwObjct = [[TDWObject alloc]init];
          int res = [tdwObjct getNum];
          [tdwObjct release];

但是当我构建它时,我得到了一个错误。

    • 错误信息**
Showing Recent Messages
CompileC /Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Intermediates.noindex/Runner.build/Debug-iphoneos/Runner.build/Objects-normal/arm64/mycpp.o /Users/sinjeong-won/Desktop/elfscoreprint_mobile_20230220/ios/Runner/mycpp.cpp normal arm64 c++ com.apple.compilers.llvm.clang.1_0.compiler (in target 'Runner' from project 'Runner')
    cd /Users/sinjeong-won/Desktop/elfscoreprint_mobile_20230220/ios
    export LANG\=en_US.US-ASCII
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x c++ -target arm64-apple-ios15.3 -fmessage-length\=0 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit\=0 -std\=gnu++11 -stdlib\=libc++ -fmodules -gmodules -fmodules-cache-path\=/Users/sinjeong-won/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -fmodules-prune-interval\=86400 -fmodules-prune-after\=345600 -fbuild-session-file\=/Users/sinjeong-won/Library/Developer/Xcode/DerivedData/ModuleCache.noindex/Session.modulevalidation -fmodules-validate-once-per-build-session -Wnon-modular-include-in-framework-module -Werror\=non-modular-include-in-framework-module -Wno-trigraphs -fpascal-strings -O0 -fno-common -Wno-missing-field-initializers -Wno-missing-prototypes -Werror\=return-type -Wunreachable-code -Werror\=deprecated-objc-isa-usage -Werror\=objc-root-class -Wno-non-virtual-dtor -Wno-overloaded-virtual -Wno-exit-time-destructors -Wno-missing-braces -Wparentheses -Wswitch -Wunused-function -Wno-unused-label -Wno-unused-parameter -Wunused-variable -Wunused-value -Wempty-body -Wuninitialized -Wconditional-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wconstant-conversion -Wint-conversion -Wbool-conversion -Wenum-conversion -Wno-float-conversion -Wnon-literal-null-conversion -Wobjc-literal-conversion -Wshorten-64-to-32 -Wno-newline-eof -Wno-c++11-extensions -DDEBUG\=1 -DCOCOAPODS\=1 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk -fstrict-aliasing -Wdeprecated-declarations -Winvalid-offsetof -g -fvisibility-inlines-hidden -Wno-sign-conversion -Winfinite-recursion -Wmove -Wcomma -Wblock-capture-autoreleasing -Wstrict-prototypes -Wrange-loop-analysis -Wno-semicolon-before-method-body -index-store-path /Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Index/DataStore -iquote /Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Intermediates.noindex/Runner.build/Debug-iphoneos/Runner.build/Runner-generated-files.hmap -I/Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Intermediates.noindex/Runner.build/Debug-iphoneos/Runner.build/Runner-own-target-headers.hmap -I/Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Intermediates.noindex/Runner.build/Debug-iphoneos/Runner.build/Runner-all-non-framework-target-headers.hmap -ivfsoverlay /Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Intermediates.noindex/Runner.build/Debug-iphoneos/Runner.build/all-product-headers.yaml -iquote /Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Intermediates.noindex/Runner.build/Debug-iphoneos/Runner.build/Runner-project-headers.hmap -I/Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Products/Debug-iphoneos/include -I/Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Products/Debug-iphoneos/device_info_plus/device_info_plus.framework/Headers -I/Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Products/Debug-iphoneos/path_provider_ios/path_provider_ios.framework/Headers -I/Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Products/Debug-iphoneos/pdfx/pdfx.framework/Headers -I/Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Products/Debug-iphoneos/printing/printing.framework/Headers -I/Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Products/Debug-iphoneos/uni_links/uni_links.framework/Headers -I/Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Intermediates.noindex/Runner.build/Debug-iphoneos/Runner.build/DerivedSources-normal/arm64 -I/Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Intermediates.noindex/Runner.build/Debug-iphoneos/Runner.build/DerivedSources/arm64 -I/Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Intermediates.noindex/Runner.build/Debug-iphoneos/Runner.build/DerivedSources -F/Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Products/Debug-iphoneos -F/Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Products/Debug-iphoneos/device_info_plus -F/Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Products/Debug-iphoneos/path_provider_ios -F/Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Products/Debug-iphoneos/pdfx -F/Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Products/Debug-iphoneos/printing -F/Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Products/Debug-iphoneos/uni_links -MMD -MT dependencies -MF /Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Intermediates.noindex/Runner.build/Debug-iphoneos/Runner.build/Objects-normal/arm64/mycpp.d --serialize-diagnostics /Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Intermediates.noindex/Runner.build/Debug-iphoneos/Runner.build/Objects-normal/arm64/mycpp.dia -c /Users/sinjeong-won/Desktop/elfscoreprint_mobile_20230220/ios/Runner/mycpp.cpp -o /Users/sinjeong-won/Library/Developer/Xcode/DerivedData/Runner-fiwvkgeptrgkurarntkwsxtdzbqb/Build/Intermediates.noindex/Runner.build/Debug-iphoneos/Runner.build/Objects-normal/arm64/mycpp.o -index-unit-output-path /Runner.build/Debug-iphoneos/Runner.build/Objects-normal/arm64/mycpp.o

In file included from /Users/sinjeong-won/Desktop/elfscoreprint_mobile_20230220/ios/Runner/mycpp.cpp:9:
In file included from /Users/sinjeong-won/Desktop/elfscoreprint_mobile_20230220/ios/Runner/objcSum.h:11:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:8:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:523:1: error: expected unqualified-id
@class NSString, Protocol;
^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:525:9: error: unknown type name 'NSString'
typedef NSString * NSExceptionName NS_TYPED_EXTENSIBLE_ENUM;
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:526:9: error: unknown type name 'NSString'
typedef NSString * NSRunLoopMode NS_TYPED_EXTENSIBLE_ENUM;
        ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:528:19: error: unknown type name 'NSString'
FOUNDATION_EXPORT NSString *NSStringFromSelector(SEL aSelector);
                  ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:529:44: error: unknown type name 'NSString'
FOUNDATION_EXPORT SEL NSSelectorFromString(NSString *aSelectorName);
                                           ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:531:19: error: unknown type name 'NSString'
FOUNDATION_EXPORT NSString *NSStringFromClass(Class aClass);
                  ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:532:53: error: unknown type name 'NSString'
FOUNDATION_EXPORT Class _Nullable NSClassFromString(NSString *aClassName);
                                                    ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:534:19: error: unknown type name 'NSString'
FOUNDATION_EXPORT NSString *NSStringFromProtocol(Protocol *proto) API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
                  ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:534:50: error: unknown type name 'Protocol'
FOUNDATION_EXPORT NSString *NSStringFromProtocol(Protocol *proto) API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
                                                 ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:535:19: error: unknown type name 'Protocol'
FOUNDATION_EXPORT Protocol * _Nullable NSProtocolFromString(NSString *namestr) API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
                  ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:535:61: error: unknown type name 'NSString'
FOUNDATION_EXPORT Protocol * _Nullable NSProtocolFromString(NSString *namestr) API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
                                                            ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:539:30: error: unknown type name 'NSString'
FOUNDATION_EXPORT void NSLog(NSString *format, ...) NS_FORMAT_FUNCTION(1,2) NS_NO_TAIL_CALL;
                             ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:539:53: error: format argument not an NSString
FOUNDATION_EXPORT void NSLog(NSString *format, ...) NS_FORMAT_FUNCTION(1,2) NS_NO_TAIL_CALL;
                             ~~~~~~~~~~~~~~~~       ^                  ~
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:94:49: note: expanded from macro 'NS_FORMAT_FUNCTION'
        #define NS_FORMAT_FUNCTION(F,A) __attribute__((format(__NSString__, F, A)))
                                                       ^                    ~
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:540:31: error: unknown type name 'NSString'
FOUNDATION_EXPORT void NSLogv(NSString *format, va_list args) NS_FORMAT_FUNCTION(1,0) NS_NO_TAIL_CALL;
                              ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:540:63: error: format argument not an NSString
FOUNDATION_EXPORT void NSLogv(NSString *format, va_list args) NS_FORMAT_FUNCTION(1,0) NS_NO_TAIL_CALL;
                              ~~~~~~~~~~~~~~~~                ^                  ~
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:94:49: note: expanded from macro 'NS_FORMAT_FUNCTION'
        #define NS_FORMAT_FUNCTION(F,A) __attribute__((format(__NSString__, F, A)))
                                                       ^                    ~
In file included from /Users/sinjeong-won/Desktop/elfscoreprint_mobile_20230220/ios/Runner/mycpp.cpp:9:
In file included from /Users/sinjeong-won/Desktop/elfscoreprint_mobile_20230220/ios/Runner/objcSum.h:11:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:10:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSArray.h:5:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:8:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSZone.h:9:1: error: expected unqualified-id
@class NSString;
^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSZone.h:19:63: error: unknown type name 'NSString'
FOUNDATION_EXPORT void NSSetZoneName(NSZone * _Nullable zone, NSString *name)NS_SWIFT_UNAVAILABLE("Zone-based memory management is unavailable");
                                                              ^
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSZone.h:20:19: error: unknown type name 'NSString'
FOUNDATION_EXPORT NSString *NSZoneName(NSZone * _Nullable zone) NS_SWIFT_UNAVAILABLE("Zone-based memory management is unavailable");
                  ^
In file included from /Users/sinjeong-won/Desktop/elfscoreprint_mobile_20230220/ios/Runner/mycpp.cpp:9:
In file included from /Users/sinjeong-won/Desktop/elfscoreprint_mobile_20230220/ios/Runner/objcSum.h:11:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:10:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSArray.h:5:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS15.4.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:10:1: error: expected unqualified-id
@class NSInvocation, NSMethodSignature, NSCoder, NSString, NSEnumerator;
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
hsvhsicv

hsvhsicv1#

如果你使用C代码作为Objective-C和C的公共接口,这意味着你必须避免使用除C语言部分之外的任何语言,以保持接口与C和Objective-C兼容。
也就是说,在将标题导入Objective-C代码时,标题不应包含以下任何内容:

  • C++类
  • C++模板
  • C++重载运算符
  • C++特定关键字
  • C++模块
  • C++支持但C不支持的任何其他内容
  • 通过#include#import指令间接导入的上述任何内容

同样,**C++**代码不应该通过包含的头文件看到任何Objective-C语句:

  • @interface节(包括类别)
  • 任意@interface@implementation部分
  • 任何其他Objective-C特定关键字(例如@end@property@synthesize)。
  • 任何Objective-C特定宏(如NS_ASSUME_NONNULL_BEGINNS_SWIFT_UNAVAILABLE
  • Objective-C支持但C不支持的任何东西
  • 通过#include#import指令间接导入的上述任何内容

这里一个明显的方法是将任何语言特有的东西移到相应的实现文件中。例如,一个C++实现的函数可能看起来像这样:

//
//  MyClass.h
//

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

int TDWCppSum(int lhs, int rhs);

#ifdef __cplusplus
}
#endif

而实现文件包含其他所有内容:

//
//  MyClass.cpp
//

#include "MyClass.h"
#include "TDWClass.h"

#pragma mark - C++ implementation

class MySum {
public:
    int cppSum(int lhs, int rhs);
    int objcSum(int lhs, int rhs);

};

int MySum::cppSum(int lhs, int rhs){
    return lhs + rhs;
}

int MySum::objcSum(int num1, int num2) {
    return TDWObjcSum(num1, num2);
}

#pragma mark - Public API implementation

int TDWCppSum(int lhs, int rhs) {
    return MySum{}.cppSum(lhs, rhs);
}

TDWClass.h在本例中是包含引用Objective-C实现的函数声明的头部:

//
//  TDWClass.h
//

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

int TDWObjcSum(int lhs, int rhs);

#ifdef __cplusplus
}
#endif

这是这个头文件的实现文件:

//
//  TDWClass.m
//

#import "TDWClass.h"
#import "MyClass.h"

#pragma mark - Objective-C implementation

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface TDWClass : NSObject

- (NSInteger)objcSum:(NSInteger)lhs :(NSInteger)rhs;
- (NSInteger)cppSum:(NSInteger)lhs :(NSInteger)rhs;

@end

NS_ASSUME_NONNULL_END

@implementation TDWClass

- (NSInteger)objcSum:(NSInteger)lhs :(NSInteger)rhs {
    return lhs + rhs;
}

- (NSInteger)cppSum:(NSInteger)lhs :(NSInteger)rhs; {
    return TDWCppSum((int)lhs, (int)rhs);
}

#pragma mark - Public API implementation

int TDWObjcSum(int lhs, int rhs) {
    return (int)[[TDWClass new] objcSum:lhs :rhs];
}

@end

正如你所看到的,两个头文件都没有公开哪种语言实现了它,它们彼此是100%兼容的(你可以用C、C或Objective-C语言中的任何一种来实现)
还应注意,从C
编译单元C/Objective-C编译单元引用的 * 任何头文件 * 都必须用extern "C"保护进行 Package ,以避免链接错误。
下面是AppDelegate.m如何调用相应的函数:

...
#import "MyClass.h"
#import "TDWClass.h"
...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSLog(@"Cpp sum: %d", TDWCppSum(4, 8));
    NSLog(@"ObjC sum: %d", TDWObjcSum(16, 32));
    return YES;
}

相关问题