0

I'm trying to create a Node.js addon in C++ following the example provided in this guide.

I've linked the node.h and v8.h libraries setting my CMakeLists.txt in this way:

cmake_minimum_required(VERSION 3.6) project(node___C__)

set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp) 

add_executable(node___C__ ${SOURCE_FILES})

include_directories(/usr/include/nodejs/src)
include_directories(/usr/include/nodejs/deps/v8/include)

I think libraries are correctly setted in my CMakeList.txt but when I try to compile the file in Clion I get this error:

/usr/include/nodejs/src/node.h:239: undefined reference to `v8::Isolate::GetCurrent()' 
/usr/include/nodejs/src/node.h:240: undefined reference to `v8::HandleScope::HandleScope(v8::Isolate*)'
/usr/include/nodejs/src/node.h:242: undefined reference to `v8::FunctionTemplate::New(v8::Isolate*, void (*)(v8::FunctionCallbackInfo<v8::Value> const&), v8::Local<v8::Value>, v8::Local<v8::Signature>, int)' 
/usr/include/nodejs/src/node.h:243: undefined reference to `v8::FunctionTemplate::GetFunction()'
/usr/include/nodejs/src/node.h:244: undefined reference to `v8::String::NewFromUtf8(v8::Isolate*, char const*, v8::String::NewStringType, int)' 
/usr/include/nodejs/src/node.h:245: undefined reference to `v8::Function::SetName(v8::Local<v8::String>)'
/usr/include/nodejs/src/node.h:246: undefined reference to `v8::Object::Set(v8::Local<v8::Value>, v8::Local<v8::Value>)'
/usr/include/nodejs/src/node.h:240: undefined reference to `v8::HandleScope::~HandleScope()' 
/usr/include/nodejs/src/node.h:240: undefined reference to `v8::HandleScope::~HandleScope()'

What I'm doing wrong? I'm quite new with C++ so maybe I'm doing some dumb mistake. I've forgot to link some dependencies?

Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213
Jacob
  • 595
  • 1
  • 7
  • 25
  • You're not linking in your dependencies, See here: [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Steve Lorimer May 31 '17 at 10:52
  • @SteveLorimer Hi, thanks for your reply. I'm quite new with C++. I've read the post you've linked but I don't understand what I'm doing wrong. In my CMake file I've included the dependencies, I'm doing it in a wrong way? include_directories(/usr/include/nodejs/src) include_directories(/usr/include/nodejs/deps/v8/include) – Jacob May 31 '17 at 14:57
  • That just adds include directories, you need to **link** against the libraries (see [`taget_link_libraries`](https://cmake.org/cmake/help/v3.3/command/target_link_libraries.html)) – Steve Lorimer May 31 '17 at 15:47
  • You should also use [`target_include_directories`](https://cmake.org/cmake/help/v3.3/command/target_include_directories.html) instead of `include_directories` (not strictly necessary, but more modern idiomatic cmake) – Steve Lorimer May 31 '17 at 15:48
  • In any event, `/usr/include` should already be on your search path (it's a standard system search path), so you likely don't even need to use `target_include_directories`, and as such you should try removing them and see if it still works (*after* **linking** the requisite libraries) – Steve Lorimer May 31 '17 at 15:50
  • You might also want to look at [node-cmake](https://www.npmjs.com/package/node-cmake) which will do the heavy lifting for you – Steve Lorimer May 31 '17 at 15:51

0 Answers0