0

I'm trying to make a c++ library work under Swift. I'm a noob in both, so I could be doing something completely stupid but do appreciate all the feedback. I have the following setup:

AnswerToEverything.cpp:

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

class Everything{

    int answerToEverything()
    {
        return 42;
    }
};

AnswerToEverything.h:

#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

int answerToEverything();

#ifdef __cplusplus
}
#endif

Answer-Bridging-Header.h:

 #include "AnswerToEverything.h"

GameScene.swift:

...
override func didMoveToView(view: SKView) {
        /* Setup your scene here */
        println("C++ call: \(answerToEverything())")
}
...

The Result is:

enter image description here

So what am I missing? All help Appreciated!

Chris
  • 7,270
  • 19
  • 66
  • 110
user3673836
  • 591
  • 1
  • 9
  • 23
  • In the header you declare a global 'extern C' function answerToEverything, in the source you have define a class with a member function answerToEverything. Those are two different and completely unrelated functions. Which of them do you actually need/want? – stijn Dec 27 '14 at 13:31

1 Answers1

0

You will need to write a "C" function which creates your C++ object and calls its method as is plotted out in this question. All the solutions look somewhat ugly, but you will have to expect that if you mix the two. Once you have a statically linkable C-function it will be found by the linker. Currently it is just telling you that you do not have one. As @stijn pointed out a C++ member method is nothing of that kind.

Community
  • 1
  • 1
Patru
  • 4,481
  • 2
  • 32
  • 42
  • Thanks! I took out the class and started to write a new c++ function into "AnswerToEverything.cpp". On the first include I run into trouble:

    #include results into: 'vector' file not found. Is there something that I have to do for the compiler settings etc?
    – user3673836 Dec 27 '14 at 21:00
  • @user3673836: You won't be able to be very clear in a comment as you can only inline your code. You should edit your question (by adding an **Update:** tag and your new code/problem) or create a new, more specific question. – Patru Dec 28 '14 at 03:17