Reputation: 109
I now have to porting C++ code to iOS, trying to build static library by original C++ code and load the library on iOS. Because the original code is heavy, I start a small test to verify my steps could work or not.
First I need to build library (.a), which prints some string. I compile the following code and generate a library(.a) file
//talk.h
...
#include <iostream>
class Talk {
Talk();
void printHello();
void printWord(char*);
};
//talk.cpp
#include "talk.h"
using namespace std;
void Talk::printHello() {
cout << "Hello World";
}
void Talk::printWord(char* word) {
cout << "Hello" << word;
}
The second step I try to do is open a new project for iOS app and then set link to the library file, also include corresponding "talk.h" header file. However, some errors happen on the header file even though I build library successfully.
The errors indicate that
I have try to rename controller.m to controller.mm, but it not fixes the problem
How to import the header file written in C++ for using library on iOS? Thanks
Upvotes: 7
Views: 9710
Reputation: 9159
A rough outline:
In Xcode (starting with an either one of the template iOS Application projects (or an existing one):
Xcode will have taken care of setting everything else up for you, including compiler flags and header search paths.
Upvotes: 4
Reputation: 5290
You need to add the C++ Standard Library.
To do this, find Other Linker Flags in your project and add -lstdc++
.
Upvotes: 0
Reputation: 2600
For the 1st part C++ compiles well with Xcode I have written a simple tutorial http://tutorialsios.blogspot.com/2013/09/c-beginers-code-for-objective-c.html These code were written using Xcode and compiled in terminal
#include "functionOverLoading.h"
int main(int argc, const char * argv[]) {
..
..
return 0;
}
$g++ functionOverLoading.cpp –o functionOverLoading
$ ./functionOverLoading
Upvotes: 0