简体   繁体   English

如何调试使用 CMake.js 构建的 Node.js 插件?

[英]How do you debug a Node.js addon built with CMake.js?

I want to be able to step though my C++ code for a Node.js addon that I am making.我希望能够遍历我正在制作的 Node.js 插件的 C++ 代码。 I understand that CMake.js has the --debug option, but there is no documentation about it.我知道 CMake.js 有--debug选项,但没有关于它的文档。

I am using the node-addon-api module, in CLion.我在 CLion 中使用 node-addon-api 模块。

After months of blind debugging though the use of Errors to print variables, I have finally figured out how to attach the CLion debugger to a Node.js addon.经过几个月的盲目调试,虽然使用 Errors 打印变量,但我终于想出了如何将 CLion 调试器附加到 Node.js 插件。

  1. Create a new CMake Application configuration.创建一个新的 CMake 应用程序配置。
  2. Fill in these fields:填写这些字段:
    • Target: Your project目标:您的项目
    • Executable: The Node binary (On Unix do which node or where.exe node on Windows)可执行文件:Node 二进制文件(在 Unix 上执行which node或 Windows 上的where.exe node
    • Program arguments: Path to your JS file程序参数:JS 文件的路径
    • Working directory: The directory where the JS file is located工作目录:JS文件所在目录
    • Before launch: Build发布前:构建
  3. Start this configuration in debug mode.在调试模式下启动此配置。

I recently stumbled on the same problem and had success creating a custom toolchain in CLion 2020.3 with CMake.js on a Linux system.我最近偶然发现了同样的问题,并在 Linux 系统上使用 CMake.js 在 CLion 2020.3 中成功创建了自定义工具链。

Reproducible steps:可重现的步骤:

  1. Install cmake-js via npm install -g cmake-js .通过npm install -g cmake-js Make sure to install the package globally, so that your toolchain becomes available across multiple projects.确保全局安装该包,以便您的工具链在多个项目中可用。
  2. Create a npm project, eg mkdir my-project && cd my-project && npm init .创建一个 npm 项目,例如mkdir my-project && cd my-project && npm init
  3. Run npm install bindings && npm install node-addon-api (For the C++ wrapper).运行npm install bindings && npm install node-addon-api (对于 C++ 包装器)。
  4. Create a CMakeLists.txt in the root directory and paste this .在根目录中创建一个CMakeLists.txt并粘贴这个. Make sure to replace file(GLOB SOURCE_FILES hello.cpp) with your addon-specific cpp and header files.确保将file(GLOB SOURCE_FILES hello.cpp)替换为特定于插件的 cpp 和头文件。
  5. Open my-project in CLion.在 CLion 中打开my-project
  6. Go to Settings / Preferences |转到设置/首选项 | Build, Execution, Deployment |构建、执行、部署 | Custom Build Targets and click + to add a new target.自定义构建目标并单击+添加新目标。
  7. Go to Build |转到构建 | Tool Settings |工具设置 | Program and set it to the cmake-js binary that you downloaded in the npm directory where you keep your global packages.编程并将其设置为您在保存全局包的 npm 目录中下载的cmake-js二进制文件。
  8. Set the arguments to compile -D and set the working directory to the root directory of my-project .将参数设置为compile -D并将工作目录设置为my-project的根目录。
  9. Go to Clean |清洁 | Tool Settings |工具设置 | Program and set it to the directory where you keep your cmake-js binary.编程并将其设置为保存cmake-js二进制文件的目录。 Set the arguments to clean and set the working directory to your project's root directory.将参数设置为clean并将工作目录设置为项目的根目录。
  10. Add a new Run Configuration and specify the Toolchain you just created in the Target field.添加新的运行配置并在目标字段中指定您刚刚创建的工具链。 Point the Executable to your node executable and add the .js file where you import your native addons to.Executable指向您的节点可执行文件并添加 .js 文件,您将本地插件导入到该文件中。 Set the working directory to the current directory as well.也将工作目录设置为当前目录。 Now, you can build the target, and also debug the N-API layer of your native code, too!现在,您可以构建目标,也可以调试本机代码的 N-API 层!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM