Writing cross-platform C++ is easier in Node.js than it is outside of Node.js

Sam Johnson
HackerNoon.com
3 min readMar 10, 2017

--

Think about it the next time you try to set up a simple C++ project that you want to compile on Linux/Windows/OSX (or god forbid, a desktop app). Just setting up the build environment is a monumental task. Oh so you want a GUI? You’re going to have to use some insane framework like QT, or worse, write your own abstraction of the 3 native GUI APIs yourself. Oh you want to use the same build script on all 3 platforms? Hah!

Enter Node.js. The node-gyp tool handles native modules, compilation, shelling out to visual studio / GCC, dependencies, binaries, period. All you have to do is write a bindings.gyp file, and write your C++ code like there’s no tomorrow (in whatever IDE you want). That’s it. It’s simple. You can write a huge C++ (or C!) based app, and simply use Node.js as your cross-platform build tool. Hell, you could even write most of your app in JavaScript and just do a few key data structures in C++. If you want a GUI, you can use HTML 5 / CSS3 via Electron, and interact directly with your C++ code. This particular approach even allows for alpha-transparency and many other juicy features you seldom see in cross-platform development:

An early-development image of FlashSync, a client-side encrypted cloud storage app which uses Electron to achieve cross-platform alpha-translucency and native shell integration on Linux/Windows/OSX. credit: https://durosoft.com

The chances are you aren’t going to be able to design a framework as GUI-capable as Electron (have you heard of Chrome, it’s kind of a big deal) so this really is the best of the best — HTML 5 / CSS3 / JavaScript for GUI, native shell integration, and the ability to interface with cross-platform C++ code via Node.js and V8. In short, if you are developing a desktop application, or even a cross-platform C++ command line utility or library, and you are not using something in the Node.js stack in 2017, you are doing it wrong.

For the uninitiated, Electron allows you to build your desktop app in JavaScript using Chrome and Node.js, and generate a native binary — this is how the popular Atom IDE works. See https://electron.atom.io/ for more.

General info on building cross-platform Node.js native modules:
https://blog.risingstack.com/writing-native-node-js-modules/

You will find a great curated list of cross-platform node.js stuff here:
https://github.com/bcoe/awesome-cross-platform-nodejs

Have fun!

Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.

If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!

--

--