Compiling Nodejs modules with makefile

Source: Internet
Author: User

Use NODE-GYP compiled Nodejs addon plug-in person, must be very curious, node-gyp in the end to help you do what things, and if we do it ourselves, how difficult. This article does not make the makefile file grammar explanation, if you do not understand, does not matter, can understand the basic flow is good. Put aside the NODE-GYP, you will find that some complex things are actually based on very simple principles.

The environment operating system is CENTOS7, has installed the Nodejs, the version is 0.10.36, in fact, almost the new version on the line. The compilation chain is also ready, g++ command.
Writing code source files hello.cc is simple, just like the Hello World example on the official website.
#include <stdio.h> #include <stdlib.h> #include <node/node.h> #include <node/v8.h>using Namespace v8;/** * Output World */handle<value> Hello (const arguments& args) {    handlescope scope;    return scope. Close (String::new ("World"));} void Init (handle<object> exports) {    Exports->set (string::newsymbol ("Hello"), Functiontemplate::new ( Hello)->getfunction ());} Node_module (Hello, init)

When we invoke the Hello method of the module, we want to output the world.
Makefilemakefile some of the parameters, referring to the NODE-GYP generated makefile, but this is much simpler, see the code
CC = g++ #output diroutdir =./build#the moudle namemodule_name = Hello.node#target Pathtarget = $ (OUTDIR)/$ (module_name) #可 To define the variable OBJS = $ (OUTDIR)/hello.ocppflags =-shared-fpiclinkfalgs =-shared-pthread-rdynamic-m64-lpthread-wl,-soname=$ (M Odule_name) All: $ (OUTDIR) $ (OBJS) flock $ (OUTDIR)/linker.lock $ (CC) $ (LINKFALGS)-O $ (TARGET)-WL,--Start-group $ (OBJS)- Wl,--end-group$ (OUTDIR): mkdir [email protected]$ (OBJS): $ (OUTDIR)/%.O:%.cc$ (CC)-C $ (cppflags) $<-o [email Protected] #声明clean是一个伪目标. PHONY:CLEANCLEAN:-RM-RF $ (OUTDIR)-rm-f $ (TARGET)

All is the default target and generates Hello.node under the subdirectory build.
Compile in the current directory to execute make on the line. If everything is in order, the build directory will be generated in the current directory with the Hello.node we want.
Test writing test Code Test.js
var module = require ('./build/hello '); var value = Module.hello (); Console.log (value);

Execute node Test.js,oh and actually output the
World

Originally, no node-gyp, you can also freely write Nodejs module. What is needed is only some makefile knowledge. The information on the Internet is not easy to find, hoping to help everyone.

Compiling Nodejs modules with makefile

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.