diff --git a/cppLib/.gitignore b/cppLib/.gitignore new file mode 100644 index 0000000..4afcf19 --- /dev/null +++ b/cppLib/.gitignore @@ -0,0 +1,2 @@ +.idea +build \ No newline at end of file diff --git a/cppLib/CMakeLists.txt b/cppLib/CMakeLists.txt new file mode 100644 index 0000000..db127ca --- /dev/null +++ b/cppLib/CMakeLists.txt @@ -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() \ No newline at end of file diff --git a/cppLib/build.bat b/cppLib/build.bat new file mode 100644 index 0000000..6b97382 --- /dev/null +++ b/cppLib/build.bat @@ -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 ../ + diff --git a/cppLib/dist/lib/arm64-v8a/libcalculate.so b/cppLib/dist/lib/arm64-v8a/libcalculate.so new file mode 100644 index 0000000..0ed71cc Binary files /dev/null and b/cppLib/dist/lib/arm64-v8a/libcalculate.so differ diff --git a/cppLib/library.cpp b/cppLib/library.cpp new file mode 100644 index 0000000..7b65604 --- /dev/null +++ b/cppLib/library.cpp @@ -0,0 +1,21 @@ +#include "library.h" + +#include + +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; +} diff --git a/cppLib/library.h b/cppLib/library.h new file mode 100644 index 0000000..0eaa052 --- /dev/null +++ b/cppLib/library.h @@ -0,0 +1,30 @@ +#ifndef CPPLIB_LIBRARY_H +#define CPPLIB_LIBRARY_H + +#include + +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 + + +