I tried to put the CPP codes in a folder for unified management,
and provide them for Android and iOS platforms to compile and use together.
I've searched the Internet for several examples ,
but none of them provide detailed steps .
The directory structure for my experiment is as follows :
TestCppCrossPlatorm
|---AndroidDemo // for AndroidStudio to create a AndroidDemo
|
|---CommonCPP // cpp files
|---Core.h
|---Core.cpp
|---myMath
|---NumAdd.h
|---NumAdd.cpp
|
|---iOS_Demo // for Xcode to create a iOS project
AndroidDemo
|---app
|---src
|---main
|---cpp
|---CMakeLists.txt
|---native-lib.cpp
|---java
|---res
A demo project created by AndroidStudio, I configure in CMakeFiles.txt
to add CommonCPP
code files as follow :
include_directories(../../../../../CommonCPP)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp
../../../../../CommonCPP/Core.h
../../../../../CommonCPP/Core.cpp
../../../../../CommonCPP/myMath/NumAdd.h
../../../../../CommonCPP/myMath/NumAdd.cpp
)
Edit native-lib.cpp
:
#include "Core.h" // test cpp code : Core.h
#include "myMath/NumAdd.h" // test cpp code : NumAdd.h
extern "C" JNIEXPORT jstring JNICALL
Java_sodino_demo_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
// code to try : it's ok!
std::string addResult = std::to_string(add(2, 3));
const char* result = concatenateMyStringWithCppString(addResult.c_str());
// std::string hello = "Hello from C++";
return env->NewStringUTF(result);
}
The code is working fine, and the demo is also running OK.
There are some problems with the configuration of the iOS project
A iOS demo project is created in the folder iOS_Demo by Xcode,
First, I add a static library
named CommonCPP for iOS_Demo by the path Build Settings -> Targets -> Add
,
Second, click CommonCPP
target, select Build Phases -> Compile Sources -> Add
, add CommonCPP
folder, as follow :
Edit ViewController.mm
to add include Core.h and myMath/NumAdd.h
,
It failed ... 'myMath/NumAdd.h' file not found
Question 1 :
Why is all cpp files displayed in the Xcode below Demo_iOS
instead of the CommonCPP
target?
Question 2 :
Why Core.h
can be include ok, but myMath/NumAdd.h
failed? and how to fixed it?
examples without detailed steps : How to use C++ for Cross-Platform Development
CMAKE is a good choice But now, I'm more concerned about how to do it step by step, and right .
Core.h
#ifndef __HelloCpp__Core__
#define __HelloCpp__Core__
#include <iostream>
const char *concatenateMyStringWithCppString(const char *myString);
#endif /* defined(__HelloCpp__Core__) */
Core.cpp
#include <string.h>
#include "Core.h"
#include "myMath/NumAdd.h"
const char *CPP_BASE_STRING = "cpp says hello to %s";
const char *concatenateMyStringWithCppString(const char *myString) {
char *concatenatedString = new char[strlen(CPP_BASE_STRING) + strlen(myString)];
sprintf(concatenatedString, CPP_BASE_STRING, myString);
return concatenatedString;
}
myMath/NumAdd.h
//
// Created by sodino on 2021/7/18.
//
#ifndef ANDROIDDEMO_NUMADD_H
#define ANDROIDDEMO_NUMADD_H
int add(int a, int b);
#endif //ANDROIDDEMO_NUMADD_H
myMath/NumAdd.cpp
//
// Created by sodino on 2021/7/18.
//
#include "NumAdd.h"
int add(int a, int b) {
return a + b;
}
Your problem is that you need to define the proper header path location for your libraries which will probably be something like ../CommonCPP
.
You'll want to add it to Header Search Paths
. You can use non-recursive
based on the way you are including your files. Note I did add an Objective C++
file after I did the screenshot to verify it would build.
Note this is what the directory structure looks like:
You can adjust your path accordingly based on your Xcode project directory. But if it looks like mine, you can use the ../CommonCPP
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.