- 此问题在此处已有答案**:
How to call functions in c++ and objective-c to each other(1个答案)
昨天关门了。
社区正在审查是否从昨天开始重新讨论这个问题。
我正在使用xcode 14.2
版本。使用Objective-C
和c++
创建应用程序时出错。
从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.
1条答案
按热度按时间hsvhsicv1#
如果你使用C代码作为Objective-C和C的公共接口,这意味着你必须避免使用除C语言部分之外的任何语言,以保持接口与C和Objective-C兼容。
也就是说,在将标题导入Objective-C代码时,标题不应包含以下任何内容:
#include
或#import
指令间接导入的上述任何内容同样,**C++**代码不应该通过包含的头文件看到任何Objective-C语句:
@interface
节(包括类别)@interface
的@implementation
部分@end
、@property
或@synthesize
)。NS_ASSUME_NONNULL_BEGIN
或NS_SWIFT_UNAVAILABLE
)#include
或#import
指令间接导入的上述任何内容这里一个明显的方法是将任何语言特有的东西移到相应的实现文件中。例如,一个C++实现的函数可能看起来像这样:
而实现文件包含其他所有内容:
TDWClass.h
在本例中是包含引用Objective-C实现的函数声明的头部:这是这个头文件的实现文件:
正如你所看到的,两个头文件都没有公开哪种语言实现了它,它们彼此是100%兼容的(你可以用C、C或Objective-C语言中的任何一种来实现)
还应注意,从C编译单元和C/Objective-C编译单元引用的 * 任何头文件 * 都必须用
extern "C"
保护进行 Package ,以避免链接错误。下面是
AppDelegate.m
如何调用相应的函数: