0

I am trying to test a node-js addon (built with cmake-js) with google test. I am building on Mac OS.

The addon target builds and runs ok, but I have problems with the googletest target.

I am getting linking errors related to undefined V8 methods.

In CMakeLists.txt I printed (with message) the variables CMAKE_JS_SRC and CMAKE_JS_LIB and they are both empty. If CMAKE_JS_LIB is empty I don't see how target_link_libraries() should work to add the node/v8 library to my test executable...

If instead of using add_executable() I use add_library() the google_test target builds but of course I can not run it since it is not an executable anymore.

Can you help?

Below is my CMakeLists.txt:

make_minimum_required(VERSION 3.14)

include_directories(my_project)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_C_FLAGS "-Wno-pointer-sign -fno-signed-char -std=c99 -fpermissive")
set(CMAKE_CXX_FLAGS "-Wno-pointer-sign -fno-signed-char -std=c99 -fpermissive")

project (addon)

include(FetchContent)

FetchContent_Declare(
  googletest
  GIT_REPOSITORY https://github.com/google/googletest.git
  GIT_TAG release-1.12.1
)
FetchContent_MakeAvailable(googletest)
include_directories(${CMAKE_JS_INC} "my_project/include/pkcs11/v2.40/")
file(GLOB SOURCE_FILES "my_project/*.c" "my_project/*.h" "my_project/*.cpp")
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})

set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
target_link_libraries(${PROJECT_NAME} ${CMAKE_JS_LIB} )
## google_test
enable_testing()
file(GLOB TEST_FILES "test/*.cc" "test/utils/*.c")

add_executable(
  my_project_google_test
  ${TEST_FILES)
)
target_include_directories(my_project_google_test
PRIVATE
test/utils/*.h
)

target_link_libraries(
  my_project_google_test
  PRIVATE
  GTest::gtest_main
  ${CMAKE_JS_LIB}
)

include(GoogleTest)
gtest_discover_tests(my_project_google_test)
273K
  • 29,503
  • 10
  • 41
  • 64
  • What errors are you getting? – 273K Oct 16 '22 at 03:59
  • Undefined symbols for architecture x86_64: "v8::FunctionTemplate::New(v8::Isolate*, void (*)(v8::FunctionCallbackInfo const&), v8::Local, v8::Local, int, v8::ConstructorBehavior, v8::SideEffectType, v8::CFunction const*)", referenced from: ... "v8::V8::ToLocalEmpty()", referenced from: ... "v8::V8::FromJustIsNothing()", referenced from: ... "node::Buffer::Data(v8::Local)", referenced from: ... "_find_by_attr", referenced from: ... ld: symbol(s) not found for architecture x86_64 – user2960174 Oct 16 '22 at 08:32

1 Answers1

0

CMAKE_JS_LIB is empty everywhere except on Windows.

Install v8 from Homebrew if you haven't done it yet: brew install v8.

Link the app to v8

target_link_libraries(
  my_project_google_test
  PRIVATE
  v8
)

Other v8 libraries can be required, you have not post all errors: v8_libbase v8_libplatform.

273K
  • 29,503
  • 10
  • 41
  • 64
  • I installed v8 and uv libraries in brew; I built node from sources, but don't know which node libraries to link to. Changes to cmakelists (output shortened because of comment length): link_directories( libuv_path v8_path node-from-sources-build ) target_link_libraries( my_project_google_test PRIVATE GTest::gtest_main v8 v8_libbase v8_libplatform uv ) The following symbols were undefined (as before): v8::FunctionTemplate::New(),v8::V8::ToLocalEmpty, v8::V8::FromJustIsNothing, node::Buffer::Copy, node::Buffer::Data, _find_by_attr, _node_module_register – user2960174 Oct 17 '22 at 09:44