添加C++ native工程代码
This commit is contained in:
parent
c49f7d821f
commit
09b129fe5a
2
cppLib/.gitignore
vendored
Normal file
2
cppLib/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
.idea
|
||||||
|
build
|
32
cppLib/CMakeLists.txt
Normal file
32
cppLib/CMakeLists.txt
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
# CMake 最低版本号要求
|
||||||
|
cmake_minimum_required(VERSION 3.5)
|
||||||
|
|
||||||
|
# 项目名称和版本
|
||||||
|
project(calculate)
|
||||||
|
|
||||||
|
# 设置编译标准
|
||||||
|
set(CMAKE_CXX_STANDARD 11)
|
||||||
|
|
||||||
|
# 设置生成静态库还是动态库
|
||||||
|
set(STATIC_OR_SHARED SHARED)
|
||||||
|
|
||||||
|
# 设置输出目录
|
||||||
|
IF(OHOS_ARCH STREQUAL "arm64-v8a")
|
||||||
|
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../dist/lib/arm64-v8a)
|
||||||
|
ELSEIF(OHOS_ARCH STREQUAL "x86_64")
|
||||||
|
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../dist/lib/x86_64)
|
||||||
|
ELSE()
|
||||||
|
MESSAGE(FATAL_ERROR "Unsupported architecture: ${OHOS_ARCH}")
|
||||||
|
ENDIF()
|
||||||
|
|
||||||
|
# 添加共享库
|
||||||
|
add_library(${PROJECT_NAME} ${STATIC_OR_SHARED} library.cpp)
|
||||||
|
|
||||||
|
# 解析配置
|
||||||
|
if(EX_PLATFORM EQUAL 32)
|
||||||
|
set(EX_PLATFORM_FLAG -m32)
|
||||||
|
elseif(EX_PLATFORM EQUAL 64)
|
||||||
|
set(EX_PLATFORM_FLAG -m64)
|
||||||
|
else()
|
||||||
|
message(WARNING "User defined ex_platform not 32 or 64 !")
|
||||||
|
endif()
|
29
cppLib/build.bat
Normal file
29
cppLib/build.bat
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
@echo off
|
||||||
|
chcp 65001 > nul
|
||||||
|
set native_path=D:\AppData\Huawei\Sdk\HarmonyOS-NEXT-DP2\base\native\
|
||||||
|
set toolchain=%native_path%build\cmake\ohos.toolchain.cmake
|
||||||
|
set cmake_root=%native_path%build-tools\cmake\
|
||||||
|
set cmake_path=%cmake_root%bin\cmake.exe
|
||||||
|
set ninja_path=%cmake_root%bin\ninja.exe
|
||||||
|
set make_path=C:\MinGW\msys\1.0\bin\make.exe
|
||||||
|
|
||||||
|
if exist build (
|
||||||
|
del /q /s build
|
||||||
|
) else (
|
||||||
|
mkdir build
|
||||||
|
)
|
||||||
|
|
||||||
|
if exist dist (
|
||||||
|
del /q /s dist
|
||||||
|
)
|
||||||
|
|
||||||
|
cd build
|
||||||
|
:: 使用 make 构建
|
||||||
|
:: %cmake_path% -G"Unix Makefiles" -DCMAKE_MAKE_PROGRAM="%make_path%" -DOHOS_STL=c++_static -DOHOS_ARCH=arm64-v8a -DOHOS_PLATFORM=OHOS -DCMAKE_TOOLCHAIN_FILE=%toolchain% ..
|
||||||
|
:: %make_path%
|
||||||
|
|
||||||
|
:: 使用 ninja 构建
|
||||||
|
%cmake_path% -GNinja -DCMAKE_MAKE_PROGRAM="%ninja_path%" -DOHOS_STL=c++_static -DOHOS_ARCH=arm64-v8a -DOHOS_PLATFORM=OHOS -DCMAKE_TOOLCHAIN_FILE=%toolchain% ..
|
||||||
|
%ninja_path%
|
||||||
|
cd ../
|
||||||
|
|
BIN
cppLib/dist/lib/arm64-v8a/libcalculate.so
vendored
Normal file
BIN
cppLib/dist/lib/arm64-v8a/libcalculate.so
vendored
Normal file
Binary file not shown.
21
cppLib/library.cpp
Normal file
21
cppLib/library.cpp
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#include "library.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
Calculate& Calculate::getInstance() {
|
||||||
|
// 使用静态局部变量确保线程安全地初始化单例对象
|
||||||
|
static Calculate instance;
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Calculate::add(int a, int b) {
|
||||||
|
return a + b;
|
||||||
|
}
|
||||||
|
|
||||||
|
CalculateInfo Calculate::getInfo() {
|
||||||
|
CalculateInfo info;
|
||||||
|
info.name = "Calculate";
|
||||||
|
info.versionName = "Calculate_v1.0.0";
|
||||||
|
info.versionCode = 1;
|
||||||
|
return info;
|
||||||
|
}
|
30
cppLib/library.h
Normal file
30
cppLib/library.h
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#ifndef CPPLIB_LIBRARY_H
|
||||||
|
#define CPPLIB_LIBRARY_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
struct CalculateInfo {
|
||||||
|
std::string name;
|
||||||
|
std::string versionName;
|
||||||
|
int versionCode;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Calculate {
|
||||||
|
public:
|
||||||
|
static Calculate& getInstance();
|
||||||
|
int add(int a, int b);
|
||||||
|
CalculateInfo getInfo();
|
||||||
|
|
||||||
|
// 删除拷贝构造函数和赋值运算符
|
||||||
|
Calculate(const Calculate&) = delete;
|
||||||
|
Calculate& operator=(const Calculate&) = delete;
|
||||||
|
|
||||||
|
private:
|
||||||
|
// 构造函数和析构函数私有化
|
||||||
|
Calculate() {}
|
||||||
|
~Calculate() {}
|
||||||
|
};
|
||||||
|
#endif //CPPLIB_LIBRARY_H
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user