build a node addon on windows for linux

1,186 views
Skip to first unread message

NodeNinja

unread,
Oct 28, 2012, 9:31:43 AM10/28/12
to nod...@googlegroups.com
I successfully built a node add-on on windows using Visual Studio.
Now I want to build the same add-on on windows but targeting Linux.

1. Is it possible to compile a node c++ add-on on windows for Linux?
2. What compiler should I install on windows?
3. Would it be possible using node-gyp?


mscdex

unread,
Oct 28, 2012, 9:57:10 AM10/28/12
to nodejs
On Oct 28, 9:31 am, NodeNinja <aeon6f...@gmail.com> wrote:
> I successfully built a node add-on on windows using Visual Studio.
> Now I want to build the same add-on on windows but targeting Linux.

It's probably going to be easier to just create a linux VM for
compiling/testing purposes.

NodeNinja

unread,
Oct 28, 2012, 1:15:25 PM10/28/12
to nod...@googlegroups.com

It's probably going to be easier to just create a linux VM for
compiling/testing purposes.

I had seen a few binding.gyp scripts with lines like these 

'conditions': [
 ['OS=="linux"',

['OS=="mac"',

 ['OS=="win"',

]

and was wondering if there was anyway to target multiple platforms?




mscdex

unread,
Oct 28, 2012, 1:37:37 PM10/28/12
to nodejs
On Oct 28, 1:15 pm, NodeNinja <aeon6f...@gmail.com> wrote:
> and was wondering if there was anyway to target multiple platforms?

Sure, you can target multiple platforms in your binding.gyp easily
using conditionals like that. However I thought you were originally
asking how to compile a Linux executable on Windows.

NodeNinja

unread,
Oct 28, 2012, 11:30:03 PM10/28/12
to nod...@googlegroups.com
My intention in to build the addon on one system preferably having Windows OS and then use that addon on Windows and Linux. 
If I do something like this in binding.gyp 
'conditions': [
 ['OS=="linux"',

['OS=="mac"',

 ['OS=="win"',

]
and then building the addon on windows?

1. Will three versions of the addon be generated?
2. Can I then run the addon on Windows and Linux?

Ben Noordhuis

unread,
Oct 29, 2012, 8:07:47 AM10/29/12
to nod...@googlegroups.com
No. You need to run the node-gyp configure/build cycle once for each
platform that you target. Here is how you approximately would script
that on a UNIX system:

for OS in linux mac win; do
GYP_DEFINES="-DOS=$OS" CC=gcc-$OS CXX=g++-$OS node-gyp rebuild --arch=ia32
cp build/Release/module.node build/Release/module-$OS.node
done

CC and CXX need to point to the cross-compilers for the platform that
you're compiling for. If your module is C++ only, you can skip the CC
variable.

> 2. Can I then run the addon on Windows and Linux?

Provided you get everything to compile, yes. :-)

NodeNinja

unread,
Oct 29, 2012, 11:47:12 PM10/29/12
to nod...@googlegroups.com

No.  You need to run the node-gyp configure/build cycle once for each
platform that you target.  Here is how you approximately would script
that on a UNIX system:

That was great news Ben, 
 
Since I am doing this on a Windows system What cross compiler would you recommend to target Unix/Linux,  gcc or MinGW?


mscdex

unread,
Oct 30, 2012, 12:39:40 AM10/30/12
to nodejs
On Oct 29, 11:47 pm, NodeNinja <aeon6f...@gmail.com> wrote:
> Since I am doing this on a Windows system What cross compiler would you
> recommend to target Unix/Linux,  gcc or MinGW?

I recommend setting up and using a local Linux virtual machine if you
do not have access to a Linux system elsewhere.

You might be able to get away with using a cross-compiler under
cygwin, but just mentioning that makes me cringe.

NodeNinja

unread,
Nov 1, 2012, 12:01:43 AM11/1/12
to nod...@googlegroups.com

I recommend setting up and using a local Linux virtual machine if you
do not have access to a Linux system elsewhere.

You might be able to get away with using a cross-compiler under
cygwin, but just mentioning that makes me cringe.

Your advice is sound mscdex... 
I couldn't find much info on the net for cross compiling on windows possibly install linux on a vm is a good way to start! 
 

Jonathan Kunkee

unread,
Nov 2, 2012, 2:00:00 AM11/2/12
to nod...@googlegroups.com
You might be able to get away with using a cross-compiler under
cygwin, but just mentioning that makes me cringe.

Your advice is sound mscdex... 
I couldn't find much info on the net for cross compiling on windows possibly install linux on a vm is a good way to start! 
 
 Yeah...that is theoretically possible, but good luck getting libc to match, let alone your other bindings, architectures, etc.

NodeNinja:

Note that having a module that will hapilly 'npm install' on all three OSes does NOT require pre-building them. The Gyp scripting you saw does exactly that: it detects which OS it's building on and follows different instructions for each. Testing all three, on the other hand...I second mscdex' vote for using a virtual machine to test in a native environment. :)

If you're looking at an embedded system, you *might* want to cross-compile the module down to a binary on a different machine, at which point you have stepped into a different arena from simply building something that will compile and run successfully across platforms. (I'd stick it out for native compilation; YMMV.)

Good luck!
--Jon

NodeNinja

unread,
Nov 3, 2012, 2:09:39 AM11/3/12
to nod...@googlegroups.com
NodeNinja:

Note that having a module that will hapilly 'npm install' on all three OSes does NOT require pre-building them. The Gyp scripting you saw does exactly that: it detects which OS it's building on and follows different instructions for each. Testing all three, on the other hand...I second mscdex' vote for using a virtual machine to test in a native environment. :)

Good luck!
--Jon


Excellent Info Jonathan, the part that modules don't necessarily need to be pre built with the node-gyp system almost skipped me. 

Krishield Santoyo

unread,
Feb 19, 2019, 10:58:39 AM2/19/19
to nodejs

Note that having a module that will hapilly 'npm install' on all three OSes does NOT require pre-building them. The Gyp scripting you saw does exactly that: it detects which OS it's building on and follows different instructions for each. Testing all three, on the other hand...I second mscdex' vote for using a virtual machine to test in a native environment. :)

If you're looking at an embedded system, you *might* want to cross-compile the module down to a binary on a different machine, at which point you have stepped into a different arena from simply building something that will compile and run successfully across platforms. (I'd stick it out for native compilation; YMMV.)

Good luck!
--Jon

Hi i created a node addon and uploaded it to npm. but when trying to "npm install --save mymodule" in another normal windows  pc it gave me an error that something related to the 'msbuild' (i think the tool that i use to build to addon) how can i make my addon work on just 'npm install' it? i red your reply but i don't understand. do i need to install every single OSes, build the addon there and copy the .node file in to one folder?
Reply all
Reply to author
Forward
0 new messages