简体   繁体   English

成功编译节点模块和“模块没有自注册”。

[英]Successful compiling the Node module and "Module did not self-register."

I decided to write a native module for Node using VS2015 compiler and node-gyp under Windows 8.1 32bit.我决定在 Windows 8.1 32 位下使用 VS2015 编译器和 node-gyp 为 Node 编写一个本地模块。 I worked with the instructions from this page .我按照这个页面的说明工作。 I searched the internet (including StackOverflow) in search of a solution to my problem.我在互联网上(包括 StackOverflow)搜索了我的问题的解决方案。

I use following versions:我使用以下版本:

  • Node: 4.6.0节点:4.6.0
  • Node-gyp: 3.4.0节点-gyp:3.4.0

The source code of the module:该模块的源代码:

// main.c++
#include <node.h>
#include <v8.h>

void Method(const v8::FunctionCallbackInfo<v8::Value>& args) {
  v8::Isolate* isolate = args.GetIsolate();
  v8::HandleScope scope(isolate);
  args.GetReturnValue().Set(v8::String::NewFromUtf8(isolate, "world"));
}

void init(v8::Local<v8::Object> target) {
  NODE_SET_METHOD(target, "hello", Method);
}

NODE_MODULE(sapphire, init);
// binding.gyp
{
    "targets": [
        {
            "target_name": "sapphire",
            "sources": [ "main.c++" ]
        }
    ]
}

Every time compilation ( node-gyp rebuild called inside addon's source code folder) succeeds.每次编译(在插件的源代码文件夹中调用node-gyp rebuild )都会成功。 Before compilation Node-Gyp prints that compiles for node@4.6.0 | win32 | ia32编译前Node-Gyp打印编译为node@4.6.0 | win32 | ia32 node@4.6.0 | win32 | ia32 node@4.6.0 | win32 | ia32 . node@4.6.0 | win32 | ia32 Everything looked good until that point.在那之前,一切看起来都很好。

For the test, I wrote the simplest possible script.为了测试,我编写了最简单的脚本。

// test.js
try {
    var sapphire = require('/build/Release/sapphire');
    console.log(sapphire.hello());
} catch(err) {
    console.log(err);
}

The result was printed error Error: Module did not self-register.结果是打印错误Error: Module did not self-register. . . I tried to replace Node and V8 with NAN (following this tutorial ).我尝试用 NAN 替换 Node 和 V8(遵循本教程)。 But the result was the same.但结果是一样的。

During the search for a solution to my problem I came across two possible reasons:在为我的问题寻找解决方案的过程中,我遇到了两个可能的原因:

  • Incorrect versions (libraries compared to the interpreter).不正确的版本(与解释器相比的库)。 Unfortunately eliminated this possibility, Node-Gyp clearly uses the library versions of the same as the interpreter, the one I use.不幸的是消除了这种可能性,Node-Gyp 显然使用了与解释器相同的库版本,我使用的那个。
  • Re-download modules from node_modules.从 node_modules 重新下载模块。 I honestly do not understand why.老实说,我不明白为什么。 All the necessary modules have been updated during the installation of Node-Gyp, the rest does not matter.在安装 Node-Gyp 期间,所有必要的模块都已更新,其余的无关紧要。

What can cause this error?什么会导致此错误? The source code of the module was downloaded from the tutorial from the documentation for the Node (v4.6.0).该模块的源代码是从 Node (v4.6.0) 文档的教程中下载的。 I also tried to make small changes and use all sorts of unofficial guides including of NAN.我还尝试进行一些小的更改并使用各种非官方指南,包括 NAN。 Each time the problem is the same.每次都是一样的问题。

node-gyp (the tool used to build node addons) tends to make some assumptions, at least when it comes to compiling your source files. node-gyp(用于构建节点插件的工具)倾向于做出一些假设,至少在编译源文件时是这样。 For example, it will automatically choose the "right" compiler based on the source file's extension.例如,它会根据源文件的扩展名自动选择“正确”的编译器。 So if you have a '.c' file it uses the C compiler and if you have a '.cc' file (or perhaps '.cpp' file) it uses the C++ compiler.因此,如果您有一个“.c”文件,它会使用 C 编译器,如果您有一个“.cc”文件(或者可能是“.cpp”文件),它会使用 C++ 编译器。

'.c++' isn't a common file extension for C++ source files, so node-gyp may be interpreting the extension in an unexpected way (probably as a C source file). '.c++' 不是 C++ 源文件的常见文件扩展名,因此 node-gyp 可能会以意想不到的方式解释扩展名(可能作为 C 源文件)。 Changing the extension to something more common may help things.将扩展名更改为更常见的内容可能会有所帮助。

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

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