I want to use the below code from C (compile with arm-gcc)
我想使用下面来自C的代码(使用arm-gcc编译)
NSString *newText;
CLLocationManager * locationManager = [[CLLocationManager alloc] init];
[locationManager startUpdatingLocation];
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
//[locationManager setDelegate:self];
CLLocation* location = [locationManager location];
newText = [[NSString alloc] initWithFormat: @"Your Position : %f %f", [location horizontalAccuracy], [location verticalAccuracy]];
Is there any way to use objective-c library in c (like using c++ library in c)?
是否有办法在c中使用objective-c库(比如在c中使用c++库)?
2 个解决方案
#1
5
It is possible basically in the same manner you would use C++ library in C.
在C语言中使用c++库的方式基本上是一样的。
You have to provide the wrapping C API. If you define plain C functions, they should be easily accessible from another plain C executable.
您必须提供包装C API。如果定义纯C函数,则可以从另一个纯C可执行文件轻松访问它们。
You will need some header file:
你需要一些头文件:
#ifndef __OBJC__
typedef void* id;
#endif
id api_getlocation();
const char* api_location_to_text(id location);
void api_free_location(id location);
And the code (1):
和代码(1):
id api_getlocation()
{
CLLocationManager * locationManager = [[CLLocationManager alloc] init];
[locationManager startUpdatingLocation];
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
//[locationManager setDelegate:self];
CLLocation* location = [locationManager location];
return [location retain];
}
const char* api_location_to_text(id location)
{
NSString* newText = [NSString stringWithFormat: @"Your Position : %f %f", [location horizontalAccuracy], [location verticalAccuracy]];
return strdup([newText UTF8String]);
}
void api_free_location(id location)
{
[location release];
}
Then you could use it from C code, including your header file and calling these C function.
然后可以从C代码中使用它,包括头文件和调用这些C函数。
NB: if you link with objective-c runtime library, you should be also able to directly send messages to the objects by calling objc_sendMsg
, but this will prove to be a pain in ....
注:如果你用objective - c运行时库链接,你也应该能够直接通过调用objc_sendMsg向对象发送消息,但这将被证明是一个....疼痛
(1) I have not checked if the objective-c code actually makes sense.
(1)我没有检查objective-c代码是否有意义。
#2
0
You could create a wrapper interface for what you need -- just like they do in C++ -> C.
您可以为您需要的东西创建一个包装器接口——就像在c++ -> C中所做的那样。
#1
5
It is possible basically in the same manner you would use C++ library in C.
在C语言中使用c++库的方式基本上是一样的。
You have to provide the wrapping C API. If you define plain C functions, they should be easily accessible from another plain C executable.
您必须提供包装C API。如果定义纯C函数,则可以从另一个纯C可执行文件轻松访问它们。
You will need some header file:
你需要一些头文件:
#ifndef __OBJC__
typedef void* id;
#endif
id api_getlocation();
const char* api_location_to_text(id location);
void api_free_location(id location);
And the code (1):
和代码(1):
id api_getlocation()
{
CLLocationManager * locationManager = [[CLLocationManager alloc] init];
[locationManager startUpdatingLocation];
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
//[locationManager setDelegate:self];
CLLocation* location = [locationManager location];
return [location retain];
}
const char* api_location_to_text(id location)
{
NSString* newText = [NSString stringWithFormat: @"Your Position : %f %f", [location horizontalAccuracy], [location verticalAccuracy]];
return strdup([newText UTF8String]);
}
void api_free_location(id location)
{
[location release];
}
Then you could use it from C code, including your header file and calling these C function.
然后可以从C代码中使用它,包括头文件和调用这些C函数。
NB: if you link with objective-c runtime library, you should be also able to directly send messages to the objects by calling objc_sendMsg
, but this will prove to be a pain in ....
注:如果你用objective - c运行时库链接,你也应该能够直接通过调用objc_sendMsg向对象发送消息,但这将被证明是一个....疼痛
(1) I have not checked if the objective-c code actually makes sense.
(1)我没有检查objective-c代码是否有意义。
#2
0
You could create a wrapper interface for what you need -- just like they do in C++ -> C.
您可以为您需要的东西创建一个包装器接口——就像在c++ -> C中所做的那样。