napi hello world
2020-12-19 00:33
标签:exports native property pen comm txt ons output api 目录下的文件 napi hello world 标签:exports native property pen comm txt ons output api 原文地址:https://www.cnblogs.com/Searchor/p/13925274.htmlbuild-node-addon-api-with-cmake.node
CMakeLists.txt
hello.cc
hello.js
package.json
build-node-addon-api-with-cmake.node
为 npm run install
后生成的npm i -D cmake-js bindings node-addon-api
cmake-js --version
CMakeLists.txt
cmake_minimum_required(VERSION 3.9)
cmake_policy(SET CMP0042 NEW)
set (CMAKE_CXX_STANDARD 11)
project (build-node-addon-api-with-cmake)
include_directories(${CMAKE_JS_INC})
file(GLOB SOURCE_FILES "hello.cc")
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB})
# Include N-API wrappers
execute_process(COMMAND node -p "require(‘node-addon-api‘).include"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE NODE_ADDON_API_DIR
)
string(REGEX REPLACE "[\r\n\"]" "" NODE_ADDON_API_DIR ${NODE_ADDON_API_DIR})
target_include_directories(${PROJECT_NAME} PRIVATE ${NODE_ADDON_API_DIR})
# define NPI_VERSION
add_definitions(-DNAPI_VERSION=3)
hello.cc
#include
#include
hello.js
var addon = require(‘bindings‘)(‘build-node-addon-api-with-cmake‘);
console.log(addon.hello()); // ‘world‘
package.json
{
"name": "build-node-addon-api-with-cmake",
"version": "0.0.0",
"description": "Build N-API native addon with CMake and node-addon-api C++ wrapper.",
"main": "hello.js",
"private": true,
"dependencies": {},
"scripts": {
"install": "cmake-js compile",
"test": "node hello.js"
},
"devDependencies": {
"bindings": "^1.2.1",
"cmake-js": "^6.1.0",
"node-addon-api": "^1.7.2"
}
}