file tree is
├── CMakeLists.txt
├── main.cpp
├── mindspore
│ ├── CMakeLists.txt
│ ├── kernel_registry.cpp
│ └── kernel_registry.h
└── nnie
├── CMakeLists.txt
└── nnie.cpp
main.cpp
#include <iostream>
#include "mindspore/kernel_registry.h"
int main()
{
auto handle = dynamicOpen();
auto res = KernelRegistry::GetInstance()->getReg();
std::cout << res[0] << std::endl;
std::cout << res[1] << std::endl;
std::cout<<"Hello, World!"<<std::endl;
return 0;
}
CMakeLists.txt in main
cmake_minimum_required(VERSION 3.19)
project(untitled10)
set(CMAKE_CXX_STANDARD 14)
add_subdirectory(nnie)
add_subdirectory(mindspore)
add_executable(untitled10 main.cpp)
target_link_libraries(untitled10 mindspore)
#target_link_libraries(untitled10 /home/huawei/CLionProjects/untitled10/cmake-build-debug/mindspore/libmindspore.so)
kernel_registry.h
#include <vector>
#include <string>
using namespace std;
void *dynamicOpen();
class KernelRegistry {
public:
static KernelRegistry *GetInstance();
void RegKernel(const string &name);
vector<string> getReg();
private:
vector<string> names_;
};
class KernelRegistrar {
public:
KernelRegistrar(const string &name) {
KernelRegistry::GetInstance()->RegKernel(name);
}
};
kernel_registry.cpp
#include "kernel_registry.h"
#include <dlfcn.h>
static KernelRegistrar Jianghui("jianghui");
KernelRegistry *KernelRegistry::GetInstance() {
static KernelRegistry instance;
return &instance;
}
void KernelRegistry::RegKernel(const string &name) {
names_.push_back(name);
}
vector<string> KernelRegistry::getReg() {
return names_;
}
void *dynamicOpen() {
auto handle = dlopen("/home/huawei/CLionProjects/untitled10/cmake-build-debug/nnie/libnnie.so", RTLD_LAZY);
dlclose(handle);
return handle;
}
CMakeLists.txt in mindspore
aux_source_directory(. DIR_MINDSPORE_SRCS)
add_library(mindspore SHARED ${DIR_MINDSPORE_SRCS})
target_link_libraries(mindspore dl)
nnie.cpp
#include "../mindspore/kernel_registry.h"
static KernelRegistrar Zhaodezan("zhaodezan");
CMakeLists.txt in nnie
aux_source_directory(. DIR_NNIE_SRCS)
add_library(nnie SHARED ${DIR_NNIE_SRCS})
target_link_libraries(nnie mindspore)
#target_link_libraries(nnie /home/huawei/CLionProjects/untitled10/cmake-build-debug/mindspore/libmindspore.so)
1. in clion you can generator so file, select the project name then compile
2. commit using #
3. if in the project the link so no need say the path