I've seen the official tutorial as well as this one showing how to build python from source and edit it with visual studio etc., but how do I build it with node-gyp to use it as a native application in node?
For example, in the tutorial, they tell you to download the source code, and then (in windows) type in that directory:
"PCbuild/build.bat"
and then that builds the Visual Studio solution and python in general etc., but now how do I do that with node-gyp? In general, to include external dependencies to build with node-gyp, you just include them in the binding.gyp file. For python, I first installed 64 bit (and later 32 bit, same error as below) python to C:/Python382, then I copied that folder to my another folder in my C++ application, and set the binding.gyp file to this (to get the include and libs):
{
"targets": [
{
"target_name": "addon",
"sources": [
"<!@(node -p \"var fs=require('fs'),path=require('path'),walk=function(r){let t,e=[],n=null;try{t=fs.readdirSync(r)}catch(r){n=r.toString()}if(n)return n;var a=0;return function n(){var i=t[a++];if(!i)return e;let u=path.resolve(r,i);i=r+'/'+i;let c=fs.statSync(u);if(c&&c.isDirectory()){let r=walk(i);return e=e.concat(r),n()}return e.push(i),n()}()};walk('./sources').join(' ');\")"
],
"libraries":[
"C:/Users/Coby/Documents/aa/atzmus/CPPtesting/other/ok/Python382/libs/python38.lib",
"C:/Users/Coby/Documents/aa/atzmus/CPPtesting/other/ok/Python382/libs/python3.lib",
"C:/Users/Coby/Documents/aa/atzmus/CPPtesting/other/ok/Python382/libs/_tkinter.lib"
],
"include_dirs": [
"C:/Users/Coby/Documents/aa/atzmus/CPPtesting/other/ok/Python382/include"
],
"dll_files": [
"C:/Users/Coby/Documents/aa/atzmus/CPPtesting/other/ok/Python382/python38.dll"
]
}
]
}
and my hello.cc file, which is the other source, simply includes Python.h. When I run this build using node-gyp build or node-gyp rebuild, it actually builds fine with no errors at all, but then when I copy over the file addon.node to my actual nodeJS server, which simply has the one line var addon = require("./addon"), I get the following error output in CMD (however, it works fine with no errors without including Python.h in my .cc source file):
internal/modules/cjs/loader.js:1197
return process.dlopen(module, path.toNamespacedPath(filename));
^
Error: \\?\C:\Users\Coby\Documents\aa\atzmus\testServer\addon.node is not a valid Win32 application.
\\?\C:\Users\Coby\Documents\aa\atzmus\testServer\addon.node
←[90m at Object.Module._extensions..node (internal/modules/cjs/loader.js:1197
:18)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:983:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:891:14)←[39m
←[90m at Module.require (internal/modules/cjs/loader.js:1023:19)←[39m
←[90m at require (internal/modules/cjs/helpers.js:72:18)←[39m
at Object.<anonymous> (C:\Users\Coby\Documents\aa\atzmus\testServer\oy.js:2:
9)
←[90m at Module._compile (internal/modules/cjs/loader.js:1128:30)←[39m
←[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:1
0)←[39m
←[90m at Module.load (internal/modules/cjs/loader.js:983:32)←[39m
←[90m at Function.Module._load (internal/modules/cjs/loader.js:891:14)←[39m
The main part seems to be:
addon.node is not a valid Win32 application.
I tried doing this with python for 64bit windows, and for 32bit windows, my system in 64 bit, my nodeJS installation is 64 bit, but I also tried it with 32 bit note; I don't know how to fix this exactly, or if there is another way entirely to build python with node-gyp?
so:
What steps need to be taken in windows to build python through node-gyp, to be able to use python natively with nodejs without having to resort to child-process?