Mac M1 Xcode创建动态链接库dylib(c++)——JNA-JNI(三)
系列文章:
Java通过JNI调用C++动态链接库dll,并打在jar包内 ——JNA-JNI(一)
Java使用JNA调用C++动态链接库——JNA-JNI(二)
Mac M1 Xcode创建动态链接库dylib(c++)——JNA-JNI(三)
JNA调用dll(c++)附带解析xml——JNA-JNI(四)
JNA参数类型转换(含接收、发送结构体)——JNA-JNI(五)
创建项目
创建文件
-
项目右键,选择New File…
![](https://gitee.com/guohaonan1/imagesStore/raw/main/cpp/mac-dylib/cpp1.jfif)
-
C++文件,勾选Also create a header file生成相应的头文件(.hpp)
![](https://gitee.com/guohaonan1/imagesStore/raw/main/cpp/mac-dylib/cpp2.jfif)
-
勾选相应的Targets,不然生成的.dylib文件不会包含相应的代码,默认是选中的
![](https://gitee.com/guohaonan1/imagesStore/raw/main/cpp/mac-dylib/cpp3.jfif)
-
创建好之后,可以通过ARGETS–>Build Phases界面检测上一步的Target有没有勾选成功,如果未如图显示,也可以在这个界面手动设置。
![](https://gitee.com/guohaonan1/imagesStore/raw/main/cpp/mac-dylib/mac3.jfif)
编辑代码
代码头文件(.hpp)
#ifndef MacSDK_hpp
#define MacSDK_hpp
#define _DllExport _declspec(dllexport) //使用宏定义缩写下
#pragma once
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
extern "C"
{
float GetDistance(float x1, float y1, float x2, float y2);
}
#endif /* MacSDK_hpp */
代码(.cpp)
#include "MacSDK.hpp"
float GetDistance(float x1, float y1, float x2, float y2)
{
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
生成.dylib
-
将需要外放的代码头文件拖拽到Public栏下
![](https://gitee.com/guohaonan1/imagesStore/raw/main/cpp/mac-dylib/mac4.jfif)
-
点击Build生成
![](https://gitee.com/guohaonan1/imagesStore/raw/main/cpp/mac-dylib/mac5.jfif)
输出位置
.dylib文件的输出位置:Users/xxx/资源库/Developer–>Xcode–>DeriveData–>对应项目的文件–>Build–>Products–>Debug
![](https://gitee.com/guohaonan1/imagesStore/raw/main/cpp/mac-dylib/path.jfif)
与.dylib文件同级的文件夹中usr–>local–>include,就是上面设置的Public栏下的头文件(.hpp)
![](https://gitee.com/guohaonan1/imagesStore/raw/main/cpp/mac-dylib/pathh.jfif)