Skip to content

Commit

Permalink
Add C++ example. TODO: test
Browse files Browse the repository at this point in the history
  • Loading branch information
Pospelove committed Jan 17, 2025
1 parent 92999d6 commit d9c6245
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/8-tooling/build_with_cmake_and_vcpkg/napi/hello.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
var addon = require(process.cwd() + "/build/Release/scam_native.node");
var addon = require(process.cwd() + "/build/Release/build-napi-with-cmake-and-vcpkg.node");

console.log(addon.hello()); // 'world'
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
set(CMAKE_TOOLCHAIN_FILE "${CMAKE_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake")

# File from vcpkg submodule. This indicates inability to find this file or checkout submodules.
if(NOT EXISTS "${CMAKE_TOOLCHAIN_FILE}")
set(msg "${CMAKE_TOOLCHAIN_FILE} doesn't exist. It seems that vcpkg submodule is not initialized.")
set(msg "${msg}\nUse commands below to initialize:")
set(msg "${msg}\n git submodule init")
set(msg "${msg}\n git submodule update")
message(FATAL_ERROR "${msg}")
endif()

cmake_minimum_required(VERSION 3.19)
project(build-node-addon-api-with-cmake-and-vcpkg)

find_package(unofficial-node-addon-api REQUIRED)

set(sources hello.c)
add_library(${PROJECT_NAME} SHARED ${sources})

set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
target_link_libraries(${PROJECT_NAME} PRIVATE unofficial::node-addon-api::node-addon-api)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/src)
23 changes: 23 additions & 0 deletions src/8-tooling/build_with_cmake_and_vcpkg/node-addon-api/hello.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <napi.h>

static Napi::String Method(const Napi::CallbackInfo& info) {
// Napi::Env is the opaque data structure containing the environment in which
// the request is being run. We will need this env when we want to create any
// new objects inside of the node.js environment
Napi::Env env = info.Env();

// Create a C++ level variable
std::string helloWorld = "Hello, world!";

// Return a new javascript string that we copy-construct inside of the node.js
// environment
return Napi::String::New(env, helloWorld);
}

static Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "hello"),
Napi::Function::New(env, Method));
return exports;
}

NODE_API_MODULE(hello, Init)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var addon = require(process.cwd() + "/build/Release/build-node-addon-api-with-cmake-and-vcpkg.node");

console.log(addon.hello()); // 'world'
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "build-node-addon-api-with-cmake-and-vcpkg",
"version": "0.0.0",
"description": "Build Node-API native addon with CMake abd Vcpkg.",
"main": "hello.js",
"private": true,
"dependencies": {},
"scripts": {
"install": "git submodule update --init && mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release && cmake --build . --config Release",
"test": "node hello.js"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json",
"name": "build-node-addon-api-with-cmake-and-vcpk",
"homepage": "https://github.com/nodejs/node-addon-examples",
"description": "Build Node-API native addon with CMake abd Vcpkg.",
"dependencies": [
"node-addon-api"
]
}

0 comments on commit d9c6245

Please sign in to comment.