Using Mac M1 Chip, Clang++, and CMake.
I tried to make a node-api addon function using the napi.h given from npm i node-addon-api
in my frontend. I'm pretty new to using C++.
main.cpp:
#include <iostream>
#include <string>
#include <napi.h>
napi_value retrieveData(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
std::string date;
napi_value output;
napi_get_cb_info(env, info, &argc, args, NULL, NULL);
napi_get_value_string_utf8(env, args[0], (char *) &date, (size_t) 5, NULL);
napi_create_string_utf8(env, (const char*) &date, (size_t) 5, &output);
return output;
}
napi_value init(napi_env env, napi_value exports) {
napi_value retrieve;
napi_create_function(env, nullptr, 0, retrieveData, nullptr, &retrieve);
return retrieve;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, init);
CMakeLists.txt:
cmake_minimum_required(VERSION 3.13)
set(CMAKE_OSX_ARCHITECTURES "arm64" CACHE STRING "" FORCE)
set (CMAKE_CXX_COMPILER "/usr/bin/clang++" CACHE STRING "C++ compiler" FORCE)
set(CMAKE_CXX_STANDARD 20)
project(BCKND)
add_compile_options(-std=c++20 -fcolor-diagnostics -Wall -L/opt/homebrew/Cellar/libpq/15.1/lib -L/opt/homebrew/Cellar/libpqxx/7.7.4/lib -lpqxx -lpq)
add_executable(${PROJECT_NAME} main.cpp)
target_link_directories(BCKND PRIVATE /opt/homebrew/Cellar/libpq/15.1/lib)
target_link_directories(BCKND PRIVATE /opt/homebrew/Cellar/libpqxx/7.7.4/lib)
target_include_directories(BCKND PRIVATE /opt/homebrew/Cellar/libpq/15.1/include)
target_include_directories(BCKND PRIVATE /opt/homebrew/Cellar/libpqxx/7.7.4/include)
target_include_directories(BCKND PRIVATE /Users/name/project/Frontend/node_modules/node-addon-api)
target_include_directories(BCKND PRIVATE /usr/local/include/node)
target_link_libraries(BCKND pq)
target_link_libraries(BCKND pqxx)
However, I get this error:
Undefined symbols for architecture arm64:
"_napi_create_function", referenced from:
init(napi_env__*, napi_value__*) in main.cpp.o
"_napi_create_string_utf8", referenced from:
retrieveData(napi_env__*, napi_callback_info__*) in main.cpp.o
"_napi_get_cb_info", referenced from:
retrieveData(napi_env__*, napi_callback_info__*) in main.cpp.o
"_napi_get_value_string_utf8", referenced from:
retrieveData(napi_env__*, napi_callback_info__*) in main.cpp.o
"_napi_module_register", referenced from:
_register_main() in main.cpp.o
ld: symbol(s) not found for architecture arm64
Which I discovered are all in the napi-inl.h header file for node-addon-api Nothing from napi.h is creating this error like napi_value and napi_env I'm also using postgresql's libpqxx and libpq which require arm64 to compile.
Tried looking for errors in my CMake, since it is my first time using CMake but I could not find anything and I am unsure how this error is caused and how to fix it. I used different architectures like arm64 and x86_64 but none of them worked. I think I could have included the node-addon-api folder headers wrong or used deprecated functions?