objective-c包装器调用c ++静态成员函数 [英] objective-c wrapper invoking c++ static member functions

查看:125
本文介绍了objective-c包装器调用c ++静态成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在纯c ++类(.cpp)和纯目标c对象(.m)之间创建一个目标c ++包装器(.mm)。一个很好的工作示例可以是在github 上找到。我可以构建和运行这个没有问题。



但是我需要访问c ++类中的静态成员函数。我修改了github示例,通过删除所有现有的函数并引入一个静态成员函数:

  // ==== ============ 
// DummyModel.h
// ================

class DummyModel
{
public:
static int test();

};

// ==================
// DummyModel.cpp
// ======= ===========

#includeDummyModel.h


static int test()
{
int x = 1;
return x;
}


// ==================
// DummyModelWrapper.h
// ================

#import< Foundation / Foundation.h>

@interface DummyModelWrapper:NSObject

- (int)test;

@end

// ================
// DummyModelWrapper.mm
// ==================

#importDummyModelWrapper.h
#importDummyModel.h

@implementation DummyModelWrapper

- (int)test
{
int result;
result = DummyModel :: test();
return result;
}

@end

这会产生以下错误:

 架构i386的未定义符号:
DummyModel :: test(),引用来源:
- [DummyModelWrapper test] DummyModelWrapper.o
ld:没有为架构i386找到符号
clang:error:linker命令失败,退出代码为1(使用-v查看调用)

正是此引用要在DummyModelWrapper.mm中测试,它正在调用错误:

  result = DummyModel :: test(); 

测试项目是从github项目改编而来的,它以未编辑形式编译和运行实例化 DummyModel 并调用实例上的成员函数。一旦我尝试添加一个静态成员并从包装器对象中访问它,错误就会出现。



我已经读取了我在stackoverflow和其他地方可以找到的一切,但可以只找到涉及非静态成员函数的示例。



参考

http://www.philjordan.eu/article/mixing-objective-c-c++-and-objective-c++

http://robnapier.net/blog/wrapping-cppfinal-edition-759

http: //www.boralapps.com/an-objective-c-project-that-uses-c/294/



环境

xcode 4.5.2 / osx8.2(指定ios5 +)

解决方案

DummyModel.cpp,替换

  static int test()
{
...
}

  int DummyModel :: test()
{
...
}


I am trying to make an objective-c++ wrapper (.mm) between a pure c++ class (.cpp) and pure objective-c object (.m). A good working example can be found on github. I can build and run this without a problem.

However I need to access static member functions in the c++ class. I have modified the github example by removing all existing functions and introducing a static member function:

//  ==================
//  DummyModel.h
//  ==================

class DummyModel
{
    public:
        static int test ();

};

//  ==================
//  DummyModel.cpp
//  ==================

#include "DummyModel.h"


static int test ()
{
    int x = 1;
    return x;
}


//  ==================
//  DummyModelWrapper.h
//  ==================

#import <Foundation/Foundation.h>

@interface DummyModelWrapper : NSObject

- (int) test;

@end

//  ==================
//  DummyModelWrapper.mm
//  ==================

#import "DummyModelWrapper.h"
#import "DummyModel.h"

@implementation DummyModelWrapper

- (int) test
{
    int result;
    result = DummyModel::test();
    return result;
}

@end

This yields the following error:

Undefined symbols for architecture i386:
  "DummyModel::test()", referenced from:
      -[DummyModelWrapper test] in DummyModelWrapper.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

It is this reference to test in DummyModelWrapper.mm that is invoking the error:

   result = DummyModel::test();

The test project is adapted from the github project, which compiles and runs fine in it's unedited form (it instantiates DummyModel and invokes member functions on the instance). The error occurs as soon as I attempt to add a static member and access it from the wrapper object.

I have read everything i can find on stackoverflow and elsewhere, but can only find examples involving non-static member functions.

references
http://www.philjordan.eu/article/mixing-objective-c-c++-and-objective-c++
http://robnapier.net/blog/wrapping-cppfinal-edition-759
http://www.boralapps.com/an-objective-c-project-that-uses-c/294/

environment
xcode 4.5.2/osx8.2 (targeting ios5+)

解决方案

Inside DummyModel.cpp, replace

static int test ()
{
    ...
}

By

int DummyModel::test ()
{
    ...
}

这篇关于objective-c包装器调用c ++静态成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆