Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined reference linking errors during cross-compilation (using cmake-js) #1021

Closed
sakthi-vivek opened this issue Jul 14, 2021 · 5 comments

Comments

@sakthi-vivek
Copy link

Hello,

In order to demonstrate this issue, I used this example https://github.com/nodejs/node-addon-examples/tree/main/build_with_cmake/node-addon-api.

After running npm installtion (cmake-js was globally installed), I was able to successfully create node modules for the linux with just a cmake-js compile. I then attempted a cross-compilation for Windows using mingw-w64 (x86_64-w64-mingw32-g++ (GCC) 8.3-posix 20190406 - used the non-default posix since std::mutex used by napi was not supported by the default cross-compiler).

cmake-js compile --CDCMAKE_CXX_COMPILER="/usr/bin/x86_64-w64-mingw32-g++"

However, I get the following output which states that compilation failed due to a lot of undefined references at the linking step.

Thank you for any help.

[
  '/usr/local/bin/node',
  '/usr/local/bin/cmake-js',
  'compile',
  '--CDCMAKE_CXX_COMPILER=/usr/bin/x86_64-w64-mingw32-g++'
]
info TOOL Using Unix Makefiles generator.
info CMD CONFIGURE
info RUN cmake "/tmp/tmp/node-addon-examples-main/build_with_cmake/node-addon-api" --no-warn-unused-cli -G"Unix Makefiles" -DCMAKE_JS_VERSION="6.1.0" -DCMAKE_BUILD_TYPE="Release" -DCMAKE_LIBRARY_OUTPUT_DIRECTORY="/tmp/tmp/node-addon-examples-main/build_with_cmake/node-addon-api/build/Release" -DCMAKE_JS_INC="/home/node/.cmake-js/node-x64/v14.16.0/include/node" -DCMAKE_JS_SRC="" -DNODE_RUNTIME="node" -DNODE_RUNTIMEVERSION="14.16.0" -DNODE_ARCH="x64" -DCMAKE_CXX_COMPILER="/usr/bin/x86_64-w64-mingw32-g++"
Not searching for unused variables given on the command line.
-- The C compiler identification is GNU 8.3.0
-- The CXX compiler identification is GNU 8.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Check for working CXX compiler: /usr/bin/x86_64-w64-mingw32-g++
-- Check for working CXX compiler: /usr/bin/x86_64-w64-mingw32-g++ - works
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/tmp/node-addon-examples-main/build_with_cmake/node-addon-api/build
info CMD BUILD
info RUN cmake --build "/tmp/tmp/node-addon-examples-main/build_with_cmake/node-addon-api/build" --config Release
Scanning dependencies of target build-node-addon-api-with-cmake
[ 50%] Building CXX object CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o
[100%] Linking CXX shared library Release/build-node-addon-api-with-cmake.node
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text+0x70): undefined reference to `napi_create_function'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text+0xc0): undefined reference to `napi_create_symbol'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text+0xe3): undefined reference to `napi_create_external'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text+0x150): undefined reference to `napi_define_properties'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text+0x175): undefined reference to `napi_create_string_utf8'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text+0x18c): undefined reference to `napi_set_property'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text+0x253): undefined reference to `napi_create_string_utf8'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi5ErrorD1Ev[_ZN4Napi5ErrorD1Ev]+0x4b): undefined reference to `napi_delete_reference'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi12AsyncContextD1Ev[_ZN4Napi12AsyncContextD1Ev]+0x18): undefined reference to `napi_async_destroy'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi12AsyncContextD0Ev[_ZN4Napi12AsyncContextD0Ev]+0x20): undefined reference to `napi_async_destroy'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi13CallbackScopeD1Ev[_ZN4Napi13CallbackScopeD1Ev]+0x13): undefined reference to `napi_close_callback_scope'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi13CallbackScopeD0Ev[_ZN4Napi13CallbackScopeD0Ev]+0x1b): undefined reference to `napi_close_callback_scope'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi5ErrorD0Ev[_ZN4Napi5ErrorD0Ev]+0x55): undefined reference to `napi_delete_reference'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi5Error5FatalEPKcS2_[_ZN4Napi5Error5FatalEPKcS2_]+0x12): undefined reference to `napi_fatal_error'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi5Error3NewEP10napi_env__[_ZN4Napi5Error3NewEP10napi_env__]+0x1f): undefined reference to `napi_get_last_error_info'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi5Error3NewEP10napi_env__[_ZN4Napi5Error3NewEP10napi_env__]+0x54): undefined reference to `napi_is_exception_pending'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi5Error3NewEP10napi_env__[_ZN4Napi5Error3NewEP10napi_env__]+0x82): undefined reference to `napi_create_string_utf8'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi5Error3NewEP10napi_env__[_ZN4Napi5Error3NewEP10napi_env__]+0xbb): undefined reference to `napi_create_error'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi5Error3NewEP10napi_env__[_ZN4Napi5Error3NewEP10napi_env__]+0x10d): undefined reference to `napi_create_reference'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi5Error3NewEP10napi_env__[_ZN4Napi5Error3NewEP10napi_env__]+0x12d): undefined reference to `napi_get_and_clear_last_exception'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi5Error3NewEP10napi_env__[_ZN4Napi5Error3NewEP10napi_env__]+0x159): undefined reference to `napi_get_and_clear_last_exception'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi5Error3NewEP10napi_env__[_ZN4Napi5Error3NewEP10napi_env__]+0x174): undefined reference to `napi_create_type_error'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi5Error3NewEP10napi_env__[_ZN4Napi5Error3NewEP10napi_env__]+0x209): undefined reference to `napi_delete_reference'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZNK4Napi5Error4whatEv[_ZNK4Napi5Error4whatEv]+0x31): undefined reference to `napi_open_escapable_handle_scope'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZNK4Napi5Error4whatEv[_ZNK4Napi5Error4whatEv]+0x53): undefined reference to `napi_get_reference_value'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZNK4Napi5Error4whatEv[_ZNK4Napi5Error4whatEv]+0x78): undefined reference to `napi_get_named_property'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZNK4Napi5Error4whatEv[_ZNK4Napi5Error4whatEv]+0x99): undefined reference to `napi_escape_handle'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZNK4Napi5Error4whatEv[_ZNK4Napi5Error4whatEv]+0xb8): undefined reference to `napi_close_escapable_handle_scope'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZNK4Napi5Error4whatEv[_ZNK4Napi5Error4whatEv]+0xd3): undefined reference to `napi_get_value_string_utf8'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZNK4Napi5Error4whatEv[_ZNK4Napi5Error4whatEv]+0x149): undefined reference to `napi_get_value_string_utf8'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZNK4Napi5Error4whatEv[_ZNK4Napi5Error4whatEv]+0x3a9): undefined reference to `napi_close_escapable_handle_scope'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi11AsyncWorker4OnOKEv[_ZN4Napi11AsyncWorker4OnOKEv]+0x5e): undefined reference to `napi_get_reference_value'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi11AsyncWorker4OnOKEv[_ZN4Napi11AsyncWorker4OnOKEv]+0x7e): undefined reference to `napi_open_escapable_handle_scope'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi11AsyncWorker4OnOKEv[_ZN4Napi11AsyncWorker4OnOKEv]+0xa0): undefined reference to `napi_get_reference_value'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi11AsyncWorker4OnOKEv[_ZN4Napi11AsyncWorker4OnOKEv]+0xdf): undefined reference to `napi_call_function'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi11AsyncWorker4OnOKEv[_ZN4Napi11AsyncWorker4OnOKEv]+0xfb): undefined reference to `napi_is_exception_pending'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi11AsyncWorker4OnOKEv[_ZN4Napi11AsyncWorker4OnOKEv]+0x116): undefined reference to `napi_escape_handle'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi11AsyncWorker4OnOKEv[_ZN4Napi11AsyncWorker4OnOKEv]+0x12d): undefined reference to `napi_close_escapable_handle_scope'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi11AsyncWorker4OnOKEv[_ZN4Napi11AsyncWorker4OnOKEv]+0x16a): undefined reference to `napi_get_reference_value'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi11AsyncWorker4OnOKEv[_ZN4Napi11AsyncWorker4OnOKEv]+0x2aa): undefined reference to `napi_close_escapable_handle_scope'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE[_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE]+0x2e): undefined reference to `napi_get_reference_value'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE[_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE]+0x58): undefined reference to `napi_get_reference_value'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE[_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE]+0x77): undefined reference to `napi_open_escapable_handle_scope'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE[_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE]+0x99): undefined reference to `napi_get_reference_value'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE[_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE]+0xd2): undefined reference to `napi_call_function'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE[_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE]+0xee): undefined reference to `napi_is_exception_pending'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE[_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE]+0x109): undefined reference to `napi_escape_handle'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE[_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE]+0x120): undefined reference to `napi_close_escapable_handle_scope'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE[_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE]+0x14a): undefined reference to `napi_get_reference_value'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE[_ZN4Napi11AsyncWorker7OnErrorERKNS_5ErrorE]+0x296): undefined reference to `napi_close_escapable_handle_scope'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZNK4Napi5Error26ThrowAsJavaScriptExceptionEv[_ZNK4Napi5Error26ThrowAsJavaScriptExceptionEv]+0x18): undefined reference to `napi_open_handle_scope'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZNK4Napi5Error26ThrowAsJavaScriptExceptionEv[_ZNK4Napi5Error26ThrowAsJavaScriptExceptionEv]+0x33): undefined reference to `napi_get_reference_value'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZNK4Napi5Error26ThrowAsJavaScriptExceptionEv[_ZNK4Napi5Error26ThrowAsJavaScriptExceptionEv]+0x45): undefined reference to `napi_throw'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZNK4Napi5Error26ThrowAsJavaScriptExceptionEv[_ZNK4Napi5Error26ThrowAsJavaScriptExceptionEv]+0x58): undefined reference to `napi_close_handle_scope'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZNK4Napi5Error26ThrowAsJavaScriptExceptionEv[_ZNK4Napi5Error26ThrowAsJavaScriptExceptionEv]+0x100): undefined reference to `napi_close_handle_scope'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi7details12CallbackDataIPFNS_6StringERKNS_12CallbackInfoEES2_E7WrapperEP10napi_env__P20napi_callback_info__[_ZN4Napi7details12CallbackDataIPFNS_6StringERKNS_12CallbackInfoEES2_E7WrapperEP10napi_env__P20napi_callback_info__]+0x76): undefined reference to `napi_get_cb_info'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text$_ZN4Napi7details12CallbackDataIPFNS_6StringERKNS_12CallbackInfoEES2_E7WrapperEP10napi_env__P20napi_callback_info__[_ZN4Napi7details12CallbackDataIPFNS_6StringERKNS_12CallbackInfoEES2_E7WrapperEP10napi_env__P20napi_callback_info__]+0x11a): undefined reference to `napi_get_cb_info'
/usr/bin/x86_64-w64-mingw32-ld: CMakeFiles/build-node-addon-api-with-cmake.dir/hello.cc.o:hello.cc:(.text.startup+0x8): undefined reference to `napi_module_register'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/build-node-addon-api-with-cmake.dir/build.make:103: Release/build-node-addon-api-with-cmake.node] Error 1
make[1]: *** [CMakeFiles/Makefile2:95: CMakeFiles/build-node-addon-api-with-cmake.dir/all] Error 2
make: *** [Makefile:103: all] Error 2
info REP Build has been failed, trying to do a full rebuild.
@NickNaso
Copy link
Member

NickNaso commented Jul 14, 2021

Hi @sakthi-vivek,
based on my experience whan you compile using mingw you will always obtain linker error like you reported. The problem is that you cannot use the flags -Wl,-unresolved-symbols=ignore-all (Linux) or -Wl,-undefined,dynamic_lookup (macOS), but you need to specify all the symblos you will use to do that you can use a library stub for the symbls exported by Node-API.

In some projects I used the library stub of Node-API that you can downlod from here: https://github.com/napi-bindings/node-api-stub/releases

The CMake file that I sued is like the following:

cmake_minimum_required(VERSION 3.16)
cmake_policy(SET CMP0042 NEW)
project(addon)

set(CMAKE_CXX_STANDARD 11)

set(CMAKE_NODE_API_LIB libnode.a)

include_directories(${CMAKE_SOURCE_DIR}/include)

set(SOURCE_FILES
        ${CMAKE_CURRENT_SOURCE_DIR}/addon.cc
        )

add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES})
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
target_link_libraries(${PROJECT_NAME} ${CMAKE_SOURCE_DIR}/lib/${CMAKE_NODE_API_LIB})


# Define NAPI_VERSION
add_definitions(-DNAPI_VERSION=3)

I put the library stub in the lib folder and the include Node-API header files in the include folder. You can find the Node-API headers file in this repo: https://github.com/nodejs/node-api-headers.

In my case I compiled and built the addon directly from WIndows using the following command:

cmake.exe -DCMAKE_BUILD_TYPE=Debug -DCMAKE_MAKE_PROGRAM=C:/TDM-GCC-64/bin/mingw32-make.exe -DCMAKE_C_COMPILER=C:/TDM-GCC-64/bin/gcc.exe -DCMAKE_CXX_COMPILER=C:/TDM-GCC-64/bin/g++.exe -G "CodeBlocks - MinGW Makefiles" 

I hope that the these info could help you.

@sakthi-vivek
Copy link
Author

@NickNaso Thank you for the reply. I managed to cross-compile for Windows from Linux, thanks to your node-api-stub. However I am getting a not a Win32 application error when I run the module in windows. Maybe it is a different issue and I would do a search for if someone had faced similar issues, and how they solved it.

I am closing this issue, but could you please share any tips that we should be aware of when we are using the stub library? For more context, I am using the resultant node artifact in an electron application.

Thank you again for the help :)

@NickNaso
Copy link
Member

NickNaso commented Jul 19, 2021

Hi @sakthi-vivek,
I will try to read an artile for the stub and in the past I also met your problem and I well remenber is in how to you export your addon when you are on windows you should use __declspec(dllexport) like reported in the example below:

#include <node_api.h>
#include "addon.h"
extern "C" {

extern __declspec(dllexport)  napi_value napi_register_module_v1(napi_env env, napi_value exports) {
    return  Initialize(env, exports);
}
 
}

I hope that this works for you.

@sakthi-vivek
Copy link
Author

Thank you @NickNaso,

May I ask where exactly is addon.h present ? I am using node-addon-api v3.1.0.

I have also discovered that the cmake-js doesn't support MinGW compilation cmake-js/cmake-js#195. This is unfortunate and now I am left with the choice of MSVC for windows compilation and a solution like https://github.com/mstorsjo/msvc-wine for cross-compilation from linux.

@NickNaso
Copy link
Member

Hi @sakthi-vivek,
addon.h contains my code nothing particular. Yes you're right cmake-js doesn't support MinGW and this pushed to me to create my workaround to build the addon. I hope to use my knowledge to create a tool to build the addon using Node-API. Another correlated issue is nodejs/node#38782.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants