从objective-c访问静态c++字段
Access to a static c++ field from objective-c
我在C++中有一个库,并进行了修改,所以我想添加一个新的静态变量。
但我总是犯同样的错误。
Ld /Users/ricardo/Library/Developer/Xcode/DerivedData/parlamobile-gjgrppzlpeaavrbixticgpbwnurz/Build/Products/Debug-iphoneos/myapp.app/myapp normal arm64
cd /Users/ricardo/xcode/mobile-ios
export IPHONEOS_DEPLOYMENT_TARGET=8.0
export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.3.sdk -L/Users/ricardo/Library/Developer/Xcode/DerivedData/parlamobile-gjgrppzlpeaavrbixticgpbwnurz/Build/Products/Debug-iphoneos -L/Users/ricardo/xcode/mobile-ios/parlamobile/Vendor/OpenSSL/lib -F/Users/ricardo/Library/Developer/Xcode/DerivedData/parlamobile-gjgrppzlpeaavrbixticgpbwnurz/Build/Products/Debug-iphoneos -F/Users/ricardo/xcode/mobile-ios/Pods/Crashlytics/iOS -F/Users/ricardo/xcode/mobile-ios/Pods/Fabric/iOS -F/Users/ricardo/xcode/mobile-ios -F/Users/ricardo/xcode/mobile-ios/parlamobile/Frameworks -filelist /Users/ricardo/Library/Developer/Xcode/DerivedData/parlamobile-gjgrppzlpeaavrbixticgpbwnurz/Build/Intermediates/parlamobile.build/Debug-iphoneos/myApp.build/Objects-normal/arm64/myApp.LinkFileList -Xlinker -rpath -Xlinker @executable_path/Frameworks -miphoneos-version-min=8.0 -dead_strip -Xlinker -no_deduplicate -ObjC -lc++ -lz -framework Crashlytics -framework Fabric -framework Security -framework SystemConfiguration -framework UIKit -fobjc-arc -fobjc-link-runtime -lsqlite3 -framework SystemConfiguration -framework CoreData -lz.1.2.5 -framework UIKit -framework Foundation -lssl -lcrypto -lPods-myApp -Xlinker -dependency_info -Xlinker /Users/ricardo/Library/Developer/Xcode/DerivedData/parlamobile-gjgrppzlpeaavrbixticgpbwnurz/Build/Intermediates/parlamobile.build/Debug-iphoneos/myApp.build/Objects-normal/arm64/myApp_dependency_info.dat -o /Users/ricardo/Library/Developer/Xcode/DerivedData/parlamobile-gjgrppzlpeaavrbixticgpbwnurz/Build/Products/Debug-iphoneos/myApp.app/myApp
ld: warning: directory not found for option '-F/Users/ricardo/xcode/mobile-ios/parlamobile/Frameworks'
Undefined symbols for architecture arm64:
"DNS::ipType", referenced from:
-[GlooxBridge getIPType] in GlooxBridge.o
DNS::connect(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int, gloox::LogSink const&) in dns.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
这是我的dns.h
#ifndef DNS_H__
#define DNS_H__
#include "macros.h"
#include "logsink.h"
#ifdef __MINGW32__
# include <windows.h>
# include <windns.h>
#endif
#ifdef HAVE_ARPA_NAMESER_H
# include <arpa/nameser.h>
#endif
#ifdef __APPLE__
# include <arpa/nameser_compat.h>
#endif
#ifndef NS_MAXDNAME
# define NS_MAXDNAME 1025
#endif
#ifndef NS_PACKETSZ
# define NS_PACKETSZ 512
#endif
#ifdef HAVE_GETADDRINFO
# include <sys/types.h>
# include <sys/socket.h>
# include <netdb.h>
#endif
#include <string>
#include <map>
namespace gloox
{
/**
* @brief This class holds a number of static functions used for DNS related stuff.
*
* You should not need to use these functions directly.
*
* @author Jakob Schröter <js@camaya.net>
* @since 0.3
*/
class GLOOX_API DNS
{
public:
//IP type (4 or 6)
static int ipType;//nothing(0),ipv4(4),ipv6(6)
...
这就是我访问dns.cpp 中变量的方式
if(sockfd!=-1){
DNS::ipType = 6;
}
现在从Objective-c类MyBridge.h
#import <Foundation/Foundation.h>
#import "RemoteDto.h"
@class MyUserDto;
@class MyMessageDto;
@class MyRoomDto;
@interface GlooxBridge : NSObject<RemoteDtoDelegate>
@property (nonatomic, readwrite) BOOL loggedIn;
@property (nonatomic, retain) NSMutableDictionary *contacts;
....
+ (GlooxBridge *)sharedInstance;
- (IBAction)initMainLoop;
- (IBAction)appearOnline;
- (IBAction)appearOffline;
- (IBAction)logout;
...
- (int)getIPType;
@end
MyBridge.mm
#import "GlooxBridge.h"
#include "GlooxHelper.h"
#include "gloox.h"
#include "dns.h"
using namespace gloox;
static GlooxBridge *_instance;
static GlooxHelper *_helper;
@implementation GlooxBridge {
int _firstMessage;
DataForm *_roomConfigForm;
UIBackgroundTaskIdentifier _backgroundTaskId;
}
@synthesize loggedIn = _loggedIn;
@synthesize contacts = _contacts;
@synthesize rooms = _rooms;
@synthesize lastMessages = _lastMessages;
@synthesize roomParticipants = _roomParticipants;
+ (GlooxBridge *)sharedInstance {
@synchronized(self) {
if(!_instance) {
_instance = [[GlooxBridge alloc] init];
}
}
return _instance;
}
- (id)init {
if (self = [super init]) {
_loggedIn = NO;
}
return self;
}
...
- (int)getIPType {
//return GLOOX_API::DNS::ipType;
return DNS::ipType;
}
看起来您忘记了变量的定义
(注意,C++中DNS::connect
的访问也是未定义的,这表明这不是Objective-C++的问题(。
添加
int DNS::ipType;
到文件作用域的"dns.cpp">
(如果您不希望它为零,请使用合适的初始值。(
相关文章:
- 将结构字段的类型展开为可变模板参数
- 将位字段导出到数组
- 为了方便起见,我应该避免公开私有字段变量吗
- 当字段可以为null时,如何使用C++接口在Avro中写入数据
- 在java中读取c++字节的位字段
- 链接器找不到在虚拟类 c++ 中访问的静态字段的符号
- 私有字段对象与私有继承?
- 声明没有默认构造函数的字段
- C++内存模型和位字段的最大序列
- 声明为无效的变量或字段'...' Ardunio 编译器上的错误
- 如何在QByteArray中放置和检索位字段而不会感到痛苦?
- C++ win32 如何使密码字段可选并启用复制和粘贴?
- 如何通过UDP接收QByteArray并将其解析为位字段结构?
- 仅匹配集合中的某些字段
- 结构字段名称与 GDB 中的 STL 数组冲突
- 如何使用位字段将数据从二进制文件复制到结构中?
- 结构体和类的不同大小(),彼此具有相同的字段类型
- 如何避免在数据结构中包含存储为字段的类?
- 聚合初始化和删除的复制构造函数,也称为不可复制的 obejcts 作为字段
- C++:用户定义的类,以成员字段作为地址