xcode ios项目,如何从其他目录导入cpp代码文件? [英] xcode ios project , how to import cpp code files from other directory?
问题描述
我尝试将CPP代码放在一个文件夹中统一管理,
并提供给Android和iOS平台编译使用.
我在网上搜索了几个例子,
但他们都没有提供详细的步骤.
我实验的目录结构如下:
TestCppCrossPlatorm|---AndroidDemo//为AndroidStudio创建一个AndroidDemo||---CommonCPP//cpp文件|---Core.h|---Core.cpp|---我的数学|---NumAdd.h|---NumAdd.cpp||---iOS_Demo//用于Xcode创建iOS项目
AndroidDemo|---应用|---src|---主要|---cpp|---CMakeLists.txt|---native-lib.cpp|---java|---res
AndroidStudio 创建的演示项目,我在 CMakeFiles.txt
中配置添加 CommonCPP
代码文件如下:
编辑 native-lib.cpp
:
#include "Core.h";//测试 cpp 代码:Core.h#include "myMath/NumAdd.h";//测试 cpp 代码 : NumAdd.h外部C"JNIEXPORT jstring JNICALLJava_sodino_demo_MainActivity_stringFromJNI(JNIEnv* 环境,作业/* 这个 */) {//要尝试的代码:没关系!std::string addResult = std::to_string(add(2, 3));const char* result = concatenateMyStringWithCppString(addResult.c_str());//std::string hello = "Hello from C++";返回 env->NewStringUTF(result);}
代码运行正常,演示也运行正常.
iOS项目的配置存在一些问题
Xcode 在文件夹 iOS_Demo 中创建了一个 iOS 演示项目,
首先,我通过路径Build Settings -> 添加一个名为CommonCPP for iOS_Demo的
,static library
目标 ->添加
其次,点击CommonCPP
目标,选择Build Phases ->编译源 ->添加
,添加CommonCPP
文件夹,如下:
编辑 ViewController.mm
以添加 include Core.h 和 myMath/NumAdd.h
,
失败了...未找到'myMath/NumAdd.h'文件
问题1:
为什么所有 cpp 文件都显示在 Demo_iOS
下方的 Xcode 中,而不是 CommonCPP
目标?
问题2:
为什么 Core.h
可以包含,但 myMath/NumAdd.h
失败?以及如何修复它?
没有详细步骤的例子:
注意这是目录结构的样子:
您可以根据您的 Xcode 项目目录相应地调整您的路径.但如果它看起来像我的,你可以使用 ../CommonCPP
.
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
.
这篇关于xcode ios项目,如何从其他目录导入cpp代码文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!