简体   繁体   English

在C ++命令行工具项目中使用Objective-C / C静态库

[英]Using Objective-C/C static library in C++ command line tool project

I've created an Objective-C/C static library which I want to use in separated project which is developed under C++ 我已经创建了一个Objective-C / C静态库,我想在C ++下开发的单独项目中使用

Here is a code of static library: 这是静态库的代码:

Info.h 信息h

const char * GetSomeInfo();

Info.m 信息量

#import "Info.h"
#import <Foundation/Foundation.h>

@interface Info : NSObject

- (NSString *)someInfo;

@end

@implementation Info

- (NSString *)someInfo
{
    return @"getting some info";
}

@end

const char * GetSomeInfo()
{
    Info *info = [[Info alloc] init];
    return [[info someInfo] UTF8String];
}

Then I added Info.h and Info.a into my main C++ Xcode project which looks like command line tool created in Xcode: 然后,将Info.h和Info.a添加到我的主要C ++ Xcode项目中,该项目看起来像在Xcode中创建的命令行工具:

#include <iostream>
#include "Info.h"

int main(int argc, const char * argv[])
{
    const char *someInfo = GetSomeInfo();
    printf("some info: %s", someinfo);
    return 0;
}

I added Info.a into project settings -> link binaries with libraries. 我将Info.a添加到项目设置->与库链接二进制文件中。 Also added -ObjC flag to the project. 还向项目添加了-ObjC标志。 As result I receive linker errors: 结果,我收到链接器错误:

Undefined symbols for architecture x86_64: "GetSomeInfo()", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) 架构x86_64的未定义符号:“ GetSomeInfo()”,引用自:main.o中的_main ld:架构x86_64的符号未找到clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)

How to fix the problem above? 如何解决以上问题?

In Info.h, try wrapping the function prototype GetSomeInfo() in an extern "C" and #ifdef __cplusplus statement (starts with 2 underlines), like this: 在Info.h中,尝试将函数原型GetSomeInfo()包裹在外部的“ C”和#ifdef __cplusplus语句(以2个下划线开头)中,如下所示:

#ifdef __cplusplus
extern "C" {
#endif
    const char * GetSomeInfo();
#ifdef __cplusplus
}
#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM