0

我正在尝试编译用 c++ 编写的机器学习代码并使用 n-api 将其连接到 NodeJS,我在其中一个文件中编写了 NAPI 函数,并在 CMake 中定义了其他依赖项。

安装
'node-addon-api': '^3.0.0'
'cmake-js':'^6.1.0'

错误

> cmake-js compile
 
[
  '/usr/bin/node',
  '/home/nishant/Documents/demo/node_modules/.bin/cmake-js',
  'compile'
]
info TOOL Using Unix Makefiles generator.
info CMD BUILD
info RUN cmake --build "/home/nishant/Documents/demo/build" --config Release
Consolidate compiler generated dependencies of target mobilenetknn
[ 60%] Built target mobilenetknn
Consolidate compiler generated dependencies of target program
[ 80%] Linking CXX executable program
CMakeFiles/program.dir/src/program.cpp.o: In function `Init(Napi::Env, Napi::Object)':
program.cpp:(.text+0x6b): undefined reference to `napi_create_function'
program.cpp:(.text+0xb8): undefined reference to `napi_create_symbol'
program.cpp:(.text+0xd3): undefined reference to `napi_create_external'
program.cpp:(.text+0x10f): undefined reference to `napi_define_properties'
program.cpp:(.text+0x133): undefined reference to `napi_create_string_utf8'
program.cpp:(.text+0x14a): undefined reference to `napi_set_property'
CMakeFiles/program.dir/src/program.cpp.o: In function `classAddTrainModel(Napi::CallbackInfo const&)':
program.cpp:(.text+0x268): undefined reference to `napi_coerce_to_number'
program.cpp:(.text+0x287): undefined reference to `napi_get_value_int32'
program.cpp:(.text+0x33a): undefined reference to `napi_get_element'
program.cpp:(.text+0x35d): undefined reference to `napi_get_element'
program.cpp:(.text+0x380): undefined reference to `napi_get_value_string_utf8'
program.cpp:(.text+0x3f7): undefined reference to `napi_get_value_string_utf8'
program.cpp:(.text+0x415): undefined reference to `napi_get_value_string_utf8'
program.cpp:(.text+0x48c): undefined reference to `napi_get_value_string_utf8'
program.cpp:(.text+0x8b8): undefined reference to `napi_create_double'
program.cpp:(.text+0xb55): undefined reference to `napi_get_undefined'
program.cpp:(.text+0xb7e): undefined reference to `napi_get_undefined'
program.cpp:(.text+0xbae): undefined reference to `napi_get_undefined'
CMakeFiles/program.dir/src/program.cpp.o: In function `Napi::Error::~Error()':
program.cpp:(.text._ZN4Napi5ErrorD2Ev[_ZN4Napi5ErrorD5Ev]+0x46): undefined reference to `napi_delete_reference'
CMakeFiles/program.dir/src/program.cpp.o: In function `Napi::AsyncContext::~AsyncContext()':
program.cpp:(.text._ZN4Napi12AsyncContextD2Ev[_ZN4Napi12AsyncContextD5Ev]+0x15): undefined reference to `napi_async_destroy'
CMakeFiles/program.dir/src/program.cpp.o: In function `Napi::AsyncContext::~AsyncContext()':
program.cpp:(.text._ZN4Napi12AsyncContextD0Ev[_ZN4Napi12AsyncContextD5Ev]+0x19): undefined reference to `napi_async_destroy'
CMakeFiles/program.dir/src/program.cpp.o: In function `Napi::Error::~Error()':
program.cpp:(.text._ZN4Napi5ErrorD0Ev[_ZN4Napi5ErrorD5Ev]+0x39): undefined reference to `napi_delete_reference'
CMakeFiles/program.dir/src/program.cpp.o: In function `Napi::Error::Fatal(char const*, char const*)':
program.cpp:(.text._ZN4Napi5Error5FatalEPKcS2_[_ZN4Napi5Error5FatalEPKcS2_]+0x12): undefined reference to `napi_fatal_error'
CMakeFiles/program.dir/src/program.cpp.o: In function `Napi::CallbackScope::~CallbackScope()':
program.cpp:(.text._ZN4Napi13CallbackScopeD2Ev[_ZN4Napi13CallbackScopeD5Ev]+0x14): undefined reference to `napi_close_callback_scope'
CMakeFiles/program.dir/src/program.cpp.o: In function `Napi::CallbackScope::~CallbackScope()':
program.cpp:(.text._ZN4Napi13CallbackScopeD0Ev[_ZN4Napi13CallbackScopeD5Ev]+0x14): undefined reference to `napi_close_callback_scope'
CMakeFiles/program.dir/src/program.cpp.o: In function `Napi::Error::New(napi_env__*)':
program.cpp:(.text._ZN4Napi5Error3NewEP10napi_env__[_ZN4Napi5Error3NewEP10napi_env__]+0x30): undefined reference to `napi_get_last_error_info'
program.cpp:(.text._ZN4Napi5Error3NewEP10napi_env__[_ZN4Napi5Error3NewEP10napi_env__]+0x45): undefined reference to `napi_is_exception_pending'
program.cpp:(.text._ZN4Napi5Error3NewEP10napi_env__[_ZN4Napi5Error3NewEP10napi_env__]+0x84): undefined reference to `napi_create_string_utf8'
program.cpp:(.text._ZN4Napi5Error3NewEP10napi_env__[_ZN4Napi5Error3NewEP10napi_env__]+0xbd): undefined reference to `napi_create_error'
program.cpp:(.text._ZN4Napi5Error3NewEP10napi_env__[_ZN4Napi5Error3NewEP10napi_env__]+0x10b): undefined reference to `napi_create_reference'
program.cpp:(.text._ZN4Napi5Error3NewEP10napi_env__[_ZN4Napi5Error3NewEP10napi_env__]+0x141): undefined reference to `napi_get_and_clear_last_exception'
program.cpp:(.text._ZN4Napi5Error3NewEP10napi_env__[_ZN4Napi5Error3NewEP10napi_env__]+0x170): undefined reference to `napi_create_type_error'
program.cpp:(.text._ZN4Napi5Error3NewEP10napi_env__[_ZN4Napi5Error3NewEP10napi_env__]+0x1ef): undefined reference to `napi_delete_reference'
CMakeFiles/program.dir/src/program.cpp.o: In function `Napi::HandleScope::~HandleScope()':
program.cpp:(.text._ZN4Napi11HandleScopeD2Ev[_ZN4Napi11HandleScopeD5Ev]+0xc): undefined reference to `napi_close_handle_scope'
CMakeFiles/program.dir/src/program.cpp.o: In function `Napi::Error::ThrowAsJavaScriptException() const':
program.cpp:(.text._ZNK4Napi5Error26ThrowAsJavaScriptExceptionEv[_ZNK4Napi5Error26ThrowAsJavaScriptExceptionEv]+0x28): undefined reference to `napi_open_handle_scope'
program.cpp:(.text._ZNK4Napi5Error26ThrowAsJavaScriptExceptionEv[_ZNK4Napi5Error26ThrowAsJavaScriptExceptionEv]+0x43): undefined reference to `napi_get_reference_value'
program.cpp:(.text._ZNK4Napi5Error26ThrowAsJavaScriptExceptionEv[_ZNK4Napi5Error26ThrowAsJavaScriptExceptionEv]+0x59): undefined reference to `napi_throw'
program.cpp:(.text._ZNK4Napi5Error26ThrowAsJavaScriptExceptionEv[_ZNK4Napi5Error26ThrowAsJavaScriptExceptionEv]+0x6c): undefined reference to `napi_close_handle_scope'
CMakeFiles/program.dir/src/program.cpp.o: In function `Napi::details::CallbackData<Napi::Number (*)(Napi::CallbackInfo const&), Napi::Number>::Wrapper(napi_env__*, napi_callback_info__*)':
program.cpp:(.text._ZN4Napi7details12CallbackDataIPFNS_6NumberERKNS_12CallbackInfoEES2_E7WrapperEP10napi_env__P20napi_callback_info__[_ZN4Napi7details12CallbackDataIPFNS_6NumberERKNS_12CallbackInfoEES2_E7WrapperEP10napi_env__P20napi_callback_info__]+0x65): undefined reference to `napi_get_cb_info'
program.cpp:(.text._ZN4Napi7details12CallbackDataIPFNS_6NumberERKNS_12CallbackInfoEES2_E7WrapperEP10napi_env__P20napi_callback_info__[_ZN4Napi7details12CallbackDataIPFNS_6NumberERKNS_12CallbackInfoEES2_E7WrapperEP10napi_env__P20napi_callback_info__]+0xb7): undefined reference to `napi_get_cb_info'
CMakeFiles/program.dir/src/program.cpp.o: In function `Napi::EscapableHandleScope::~EscapableHandleScope()':
program.cpp:(.text._ZN4Napi20EscapableHandleScopeD2Ev[_ZN4Napi20EscapableHandleScopeD5Ev]+0xc): undefined reference to `napi_close_escapable_handle_scope'
CMakeFiles/program.dir/src/program.cpp.o: In function `Napi::AsyncWorker::OnError(Napi::Error const&)':
program.cpp:(.text._ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE[_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE]+0x46): undefined reference to `napi_get_reference_value'
program.cpp:(.text._ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE[_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE]+0x71): undefined reference to `napi_get_reference_value'
program.cpp:(.text._ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE[_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE]+0x90): undefined reference to `napi_open_escapable_handle_scope'
program.cpp:(.text._ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE[_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE]+0xb3): undefined reference to `napi_get_reference_value'
program.cpp:(.text._ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE[_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE]+0xde): undefined reference to `napi_call_function'
program.cpp:(.text._ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE[_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE]+0xfa): undefined reference to `napi_is_exception_pending'
program.cpp:(.text._ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE[_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE]+0x11c): undefined reference to `napi_get_reference_value'
program.cpp:(.text._ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE[_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE]+0x163): undefined reference to `napi_escape_handle'
program.cpp:(.text._ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE[_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE]+0x17a): undefined reference to `napi_close_escapable_handle_scope'
CMakeFiles/program.dir/src/program.cpp.o: In function `Napi::AsyncWorker::OnOK()':
program.cpp:(.text._ZN4Napi11AsyncWorker4OnOKEv[_ZN4Napi11AsyncWorker4OnOKEv]+0x6f): undefined reference to `napi_get_reference_value'
program.cpp:(.text._ZN4Napi11AsyncWorker4OnOKEv[_ZN4Napi11AsyncWorker4OnOKEv]+0x93): undefined reference to `napi_open_escapable_handle_scope'
program.cpp:(.text._ZN4Napi11AsyncWorker4OnOKEv[_ZN4Napi11AsyncWorker4OnOKEv]+0xb6): undefined reference to `napi_get_reference_value'
program.cpp:(.text._ZN4Napi11AsyncWorker4OnOKEv[_ZN4Napi11AsyncWorker4OnOKEv]+0xe8): undefined reference to `napi_call_function'
program.cpp:(.text._ZN4Napi11AsyncWorker4OnOKEv[_ZN4Napi11AsyncWorker4OnOKEv]+0x104): undefined reference to `napi_is_exception_pending'
program.cpp:(.text._ZN4Napi11AsyncWorker4OnOKEv[_ZN4Napi11AsyncWorker4OnOKEv]+0x126): undefined reference to `napi_get_reference_value'
program.cpp:(.text._ZN4Napi11AsyncWorker4OnOKEv[_ZN4Napi11AsyncWorker4OnOKEv]+0x13d): undefined reference to `napi_close_escapable_handle_scope'
program.cpp:(.text._ZN4Napi11AsyncWorker4OnOKEv[_ZN4Napi11AsyncWorker4OnOKEv]+0x18b): undefined reference to `napi_escape_handle'
CMakeFiles/program.dir/src/program.cpp.o: In function `Napi::Error::what() const':
program.cpp:(.text._ZNK4Napi5Error4whatEv[_ZNK4Napi5Error4whatEv]+0x42): undefined reference to `napi_open_escapable_handle_scope'
program.cpp:(.text._ZNK4Napi5Error4whatEv[_ZNK4Napi5Error4whatEv]+0x5f): undefined reference to `napi_get_reference_value'
program.cpp:(.text._ZNK4Napi5Error4whatEv[_ZNK4Napi5Error4whatEv]+0x81): undefined reference to `napi_get_named_property'
program.cpp:(.text._ZNK4Napi5Error4whatEv[_ZNK4Napi5Error4whatEv]+0xa2): undefined reference to `napi_escape_handle'
program.cpp:(.text._ZNK4Napi5Error4whatEv[_ZNK4Napi5Error4whatEv]+0xc1): undefined reference to `napi_close_escapable_handle_scope'
program.cpp:(.text._ZNK4Napi5Error4whatEv[_ZNK4Napi5Error4whatEv]+0xdd): undefined reference to `napi_get_value_string_utf8'
program.cpp:(.text._ZNK4Napi5Error4whatEv[_ZNK4Napi5Error4whatEv]+0x147): undefined reference to `napi_get_value_string_utf8'
CMakeFiles/program.dir/src/program.cpp.o: In function `Napi::AsyncWorker::OnWorkComplete(Napi::Env, napi_status)':
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x3b): undefined reference to `napi_open_handle_scope'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x6c): undefined reference to `napi_create_string_utf8'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x88): undefined reference to `napi_create_error'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0xff): undefined reference to `napi_create_reference'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x142): undefined reference to `napi_get_reference_value'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x172): undefined reference to `napi_get_reference_value'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x19c): undefined reference to `napi_open_escapable_handle_scope'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x1bf): undefined reference to `napi_get_reference_value'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x1ed): undefined reference to `napi_call_function'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x20c): undefined reference to `napi_is_exception_pending'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x231): undefined reference to `napi_escape_handle'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x24e): undefined reference to `napi_close_escapable_handle_scope'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x2b3): undefined reference to `napi_close_handle_scope'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x377): undefined reference to `napi_get_reference_value'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x3a1): undefined reference to `napi_open_escapable_handle_scope'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x3c4): undefined reference to `napi_get_reference_value'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x3fc): undefined reference to `napi_call_function'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x41b): undefined reference to `napi_is_exception_pending'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x440): undefined reference to `napi_escape_handle'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x45d): undefined reference to `napi_close_escapable_handle_scope'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x491): undefined reference to `napi_delete_reference'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x4c2): undefined reference to `napi_get_reference_value'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x532): undefined reference to `napi_get_reference_value'
program.cpp:(.text._ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status[_ZN4Napi11AsyncWorker14OnWorkCompleteENS_3EnvE11napi_status]+0x778): undefined reference to `napi_delete_reference'
CMakeFiles/program.dir/src/program.cpp.o: In function `_register_mobilenetknn':
program.cpp:(.text.startup+0x6): undefined reference to `napi_module_register'
collect2: error: ld returned 1 exit status
CMakeFiles/program.dir/build.make:161: recipe for target 'program' failed
make[2]: *** [program] Error 1
CMakeFiles/Makefile2:110: recipe for target 'CMakeFiles/program.dir/all' failed
make[1]: *** [CMakeFiles/program.dir/all] Error 2
Makefile:90: recipe for target 'all' failed
make: *** [all] Error 2
ERR! OMG Process terminated: 2
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! demo@1.0.0 install: `cmake-js compile`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the demo@1.0.0 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
 
npm ERR! A complete log of this run can be found in:
npm ERR!     /home/nishant/.npm/_logs/2021-05-25T19_38_32_738Z-debug.log

CMakeLists.txt

cmake_minimum_required(VERSION 3.15)
project(mobilenetknn)
 
set(CMAKE_CXX_STANDARD 11)
 
SET(Caffe_DIR /home/nishant/caffe/distribute)
include_directories(${CMAKE_JS_INC})
include_directories(${Caffe_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/node_modules/node-addon-api)
 
file(GLOB_RECURSE SOURCE_O ${CMAKE_CURRENT_SOURCE_DIR}/lib/*.o GLOB SOURCE_FILES "src/*.cpp" "src/*.hpp")
# file(GLOB SOURCE_FILES "src/*.cpp" "src/*.hpp")
 
 
add_library(${PROJECT_NAME}
       STATIC ${SRC}
           ${SOURCE_O} ${CMAKE_JS_SRC})
 
# add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
# add_library(SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
 
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
 
add_definitions(-DCPU_ONLY)
target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_JS_LIB} ${Caffe_DIR}/lib/libcaffe.a)
 
SET(EXAMPLES
        program
)
# License source files
find_package(OpenMP)
if(OPENMP_FOUND)
   set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif()
find_package( OpenCV REQUIRED)
set(CMAKE_BUILD_TYPE release)
set(CMAKE_CXX_STANDARD 11)
 
add_definitions(-DCPU_ONLY)
 
find_package(HDF5 COMPONENTS HL REQUIRED)
include_directories(SYSTEM ${HDF5_INCLUDE_DIRS} ${HDF5_HL_INCLUDE_DIR})
list(APPEND Caffe_LINKER_LIBS ${HDF5_LIBRARIES})
list(APPEND Caffe_LINKER_LIBS ${HDF5_HL_LIBRARIES})
 
execute_process(COMMAND node -p "require('node-addon-api').include"
       WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
       OUTPUT_VARIABLE NODE_ADDON_API_DIR
       )
string(REPLACE "\n" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
string(REPLACE "\"" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
target_include_directories(${PROJECT_NAME} PRIVATE ${NODE_ADDON_API_DIR} ${CAMKE_JS_INC})
 
# target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_SOURCE_DIR}/node_modules/node-addon-api")
# target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_JS_LIB})
add_definitions(-DNAPI_VERSION=3)
 
include_directories(${OpenCV_DIR}/include)
foreach (example ${EXAMPLES})
   add_executable(${example} src/${example}.cpp)
   TARGET_LINK_LIBRARIES(${example} ${OpenCV_LIBS} ${CMAKE_CURRENT_LIST_DIR}/build/libmobilenetknn.a ${HDF5_LIBRARIES} ${HDF5_HL_LIBRARIES} -lopenblas -lprotobuf -lboost_system -lglog -lgflags -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_ml -lopencv_imgcodecs -lopencv_videoio  -lboost_system -lboost_regex -lboost_filesystem -lboost_thread -lboost_log)
endforeach()

主文件(program.cpp) 我注释了一些函数,同样的问题也出现在其他函数上。

#include <opencv2/core/core.hpp>    
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/ml.hpp>
 
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
// #include "napi.h"
#include <napi.h>
// #include <node_modules/node-addon-api/napi.h>
 
using namespace std;
using namespace cv;
using namespace cv::ml;
// using namespace Napi;
 
struct classInfo{
  int classid;
  std::string classname;
  std::string imageset_location;
};
 
class Classify{ // declaration of class classify
public:
   Classify(); //constructor
   ~Classify(); //destructor
 
       std::vector<classInfo>cls;
   int counter=0;
   Ptr<KNearest> trainedModel;
 
   std::string infer(std::string data_path);
   void loadModel(string knn_path="train.knn");
   std::string exportTrainedModel();
   void trainModel(vector<classInfo>cls);
   void listClasses(vector<classInfo>n);
   vector<classInfo> addClass(std::string classname, std::string imageset_location);
 
 
};
 
Classify ic;
 
Napi::Number classAddTrainModel(const Napi::CallbackInfo& info){
   Napi::Env env = info.Env();
 
   // number of classes
   int n = info[0].ToNumber();
   Napi::Array classn = info[1].As<Napi::Array>();
   Napi::Array iloc = info[2].As<Napi::Array>();
   vector<classInfo> classinfo;
 
   for(int i  = 0; i < n; i++){
       Napi::Value x = classn[i];
       Napi::Value y = iloc[i];
       string className = (string)x.As<Napi::String>();
       string loc = (string)y.As<Napi::String>();
       // cout << className << endl << loc << endl;
       classinfo = ic.addClass(className, loc);       
   }
 
   // Train Model
   ic.trainModel(classinfo);
 
   return Napi::Number::New(env, 1);
}
 
// Napi::String modelExport(const Napi::CallbackInfo& info){
//     Napi::Env env = info.Env();
  
//     std::string path = ic.exportTrainedModel();
//     return Napi::String::New(env, ic.exportTrainedModel());
// }
 
// Napi::String modelLoad(const Napi::CallbackInfo& info){
//     Napi::Env env = info.Env();
 
//     std::string modelPath = info[0].ToString();
//     std::string imgPath = info[1].ToString();
 
//     ic.loadModel(modelPath);
//     std::string label = ic.infer(imgPath);
  
//     return Napi::String::New(env, label);
// }
 
Napi::Object Init(Napi::Env env, Napi::Object exports){
   // set a key on exports object
   exports.Set(
       Napi::String::New(env, "classAddTrainModel"),
       Napi::Function::New(env, classAddTrainModel)
   );
   // exports.Set(
   //     Napi::String::New(env, "modelExport"),
   //     Napi::Function::New(env, modelExport)
   // );
   // exports.Set(
   //     Napi::String::New(env, "modelLoad"),
   //     Napi::Function::New(env, modelLoad)
   // );
  
   // return 'exports' object (always)
   return exports;
}
 
// register 'greet' module which calls Init method
NODE_API_MODULE(mobilenetknn, Init);
 
int main(){
   return 0;
}
 
4

0 回答 0