0

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

foundry
  • 31,615
  • 9
  • 90
  • 125
  • If you're invoking C++ code in that ".m" file, shouldn't it be a ".mm" file extension instead? Or perhaps the comment in the source file is inaccurate? Regardless, see [benjorbin's](http://stackoverflow.com/questions/14104510/objective-c-wrapper-invoking-c-static-member-functions/14104585#14104585) answer below. – WhozCraig Dec 31 '12 at 18:11
  • yes, sorry that was a bad typo, fixed in the question – foundry Dec 31 '12 at 18:15

1 Answers1

2

Inside DummyModel.cpp, replace

static int test ()
{
    ...
}

By

int DummyModel::test ()
{
    ...
}
benjarobin
  • 4,410
  • 27
  • 21
  • Thanks that does fix it, but I don't understand why. Shouldn't the definition and declaration signatures should match? Can you point me to any clear online references? – foundry Dec 31 '12 at 18:18
  • This is fundamental class/namespace method definition logic - your declaration must contain the class specifier, otherwise it's not a method of the class, but a simple function (and in this case a static function) - this is due to C++ being based on it's C roots – Anya Shenanigans Dec 31 '12 at 18:25
  • @Petesh, as you have noticed my c++ knowledge is very rudimentary. `Static` seems to have many uses. To quote Bruce Eckel's __Thinking in C++__ "The static keyword was overloaded in C before people knew what the term “overload” meant, and C++ has added yet another meaning." (he later describes it as 'confusing'). I am doing lots of reading now. I asked this question to help me with [another answer](http://stackoverflow.com/questions/13958321/iosretrieve-rectangle-shaped-image-from-the-background-image) on c++ and obj-c, and it taught me how little I know. I managed to get it working though. – foundry Jan 02 '13 at 15:04