Friday, December 9, 2011

Building a simple native nodejs module on windows from command line

Setting up a visual studio project to just build a simple native hello world node module is an overkill and there is a simpler way to do that by using the visual studio command prompt directly. Here you may find the required commands you have to issue, plus a vcbuild.bat file that does the whole thing at once.

The basic idea is that if you fire up a visual studio command line (or execute the command call "%VS100COMNTOOLS%..\..\vc\vcvarsall.bat" on a regular command line) you get the cl compiler and the link linker tools and you may do the whole module compiling by issuing two commands. Note that VS100COMTOOLS environment variable is set automatically when you install visual studio 2010 (any edition, even the VC++ express one).
You will need the node source tree (see my previous post on how to do the building and what are the expected folders and files) in order to get access to the node.lib file where we shall link our obj file. To make it a bit generic, I have created an environment variable named “NODE_ROOT” to specify the path of node’s source dir.
Based on this NODE_ROOT and whether you built node on DEBUG or RELEASE, you may set a temporary environment variable named “nodelibpath” to either %NODE_ROOT%\Release or %NODE_ROOT%\Debug that will pinpoint the location of node.lib.
Having these variables, you may compile your module issuing:
@rem filename "don't strip comments" "no banner" "disable intrinsic functions" "no optimization" "calling conversion __cdecl" "no analysis" "the /I adds some folders in the include path" "no clr. This is deprecated and should be changed but I am working on a gyp file instead of this bat" cl.exe helloworld.cpp /c /nologo /Oi- /Od /Gd /analyze- /I%NODE_ROOT%\src\ /I%NODE_ROOT%\deps\v8\include\ /I%NODE_ROOT%\deps\uv\include\ /clr:noAssembly
and then link it by issuing:
link helloworld.obj node.lib /OUT:"helloworld.dll" /NOLOGO /DLL /NOENTRY /MANIFEST:NO /TLBID:1 /DYNAMICBASE /NXCOMPAT /MACHINE:X86 /LIBPATH:%nodelibpath%
I have created a bat file that assumes that the NODE_ROOT is set (it actually checks to see if it is) and then compiles and links the HelloWorld.cpp module that I used in my earlier post.

You may download the cpp and the bat files from the following link:

No comments: