foundry
foundry

Reputation: 31745

objective-c wrapper invoking c++ static member functions

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+)

Upvotes: 0

Views: 2547

Answers (1)

benjarobin
benjarobin

Reputation: 4487

Inside DummyModel.cpp, replace

static int test ()
{
    ...
}

By

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

Upvotes: 2

Related Questions