fatal error: napi.h: No such file or directory

使用Cmake-js构建基于node-addon-api的C++扩展

基于node-addon官方的eample改造测试:https://github.com/nodejs/node-addon-examples

在这里插入图片描述
Cmake-js的github给了一个例子,但是是基于NAN的,而不是node-addon-api:https://github.com/cmake-js/cmake-js/wiki/TUTORIAL-01-Creating-a-native-module-by-using-CMake.js-and-NAN
它里面给了一个CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

# Name of the project (will be the name of the plugin)
project(addon)

# Build a shared library named after the project from the files in `src/`
file(GLOB SOURCE_FILES "src/*.cc" "src/*.h")
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})

# Gives our library file a .node extension without any "lib" prefix
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")

# Essential include files to build a node addon,
# You should add this line in every CMake.js based project
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_JS_INC})

# Essential library files to link to a node addon
# You should add this line in every CMake.js based project
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB})

直接复制过来,然后编译:

npm install -g cmake-js
npm install node-addon-api
cmake-js build

报错:
在这里插入图片描述
原因是没找到hello.cc里引用的头文件napi.h
在这里插入图片描述
解决办法一: 直接指定这个头文件的路径
参考:https://github.com/cmake-js/cmake-js/issues/309

target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_SOURCE_DIR}/node_modules/node-addon-api")

这个方法不太好,因为得认为去指定路径。

解决办法二: 自动找
参考:http://nodejs.github.io/node-addon-examples/build-tools/cmake-js/

Modules based on node-addon-api include additional header files that are not part of Node itself. These lines instruct CMake.js where to find these files:

# Include Node-API wrappers
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})

这就类似于binding,gyp里的:
在这里插入图片描述

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误通常是由于编译器无法找到所需的头文件而引起的。根据给出的引用内容,有几种可能的解决办法。首先,你可以检查头文件的路径是否正确。如果出现fatal error: error.h: No such file or directory错误,可以尝试以下方法: 1. 确保头文件存在于正确的位置。根据所提供的引用,你可以参考fatal error: Python.h: No such file or directory错误。这意味着编译器无法找到Python.h头文件。你可以通过检查路径是否正确来解决这个问题。 2. 引入正确的库。根据引用,你可以在CMakeLists.txt文件中使用include_directories命令来引入所需的库。确保路径和库名称正确,并重新编译你的代码。 3. 更改头文件引用。根据引用,你可以尝试更改头文件的引用路径。使用#include语句,并指定正确的路径来包含所需的头文件。 记住,解决这个错误可能因个别情况而异。根据你的具体环境和代码要求,你可能需要进行一些调整和尝试不同的解决方案来解决这个问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [解决fatal error: Python.h: No such file or directory报错](https://blog.csdn.net/weixin_35757704/article/details/124852304)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [pcre2.h fatal error: pcre2.h: No such file or directory 27 | #include "pcre2.h](https://download.csdn.net/download/u012414183/19987888)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [fatal error: ***.h: No such file or directory](https://blog.csdn.net/zhibing_ding/article/details/128577138)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值