Cannot build Node.js addon example in C++ - c++

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?

Related

How to correctly link opencv library to clion?

I need help with linking opencv library to my clion project. I am trying to do this on Windows 10. I've tried to do following steps:- Install OpenCV from opencv.org in verssion 4.5.1- Add C:\OpenCV_4.5.1\build\x64\vc15\bin to system PATH variable- Add lines to CMakeLists.txt:
cmake_minimum_required(VERSION 3.17)
project(opencv)
set(CMAKE_CXX_STANDARD 17)
add_executable(opencv main.cpp)
set(OpenCV_DIR "C:\\OpenCV_4.5.1\\build\\x64\\vc15\\lib")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(opencv ${OpenCV_LIBS})
But the library still doesn't work. After compiling I'm getting these errors:
undefined reference to `cv::Mat::Mat()'
undefined reference to `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'
undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
undefined reference to `cv::waitKey(int)'
undefined reference to `cv::Mat::~Mat()'
undefined reference to `cv::VideoCapture::~VideoCapture()'
undefined reference to `cv::Mat::~Mat()'
I don't know how to solve this, I will be verry greatful for helping me. Thanks in advance.

Problems setting external headers and linker libraries in CLion and Cmake [duplicate]

This question already has answers here:
CMake link to external library
(6 answers)
Closed 2 years ago.
I've just started to use CLion as my IDE after mainly using code::blocks on Linux for the last year or so. However I'm finding it really difficult to set up a project that worked and compiled fine in code::blocks because I just don't understand cmake yet.
Here's a quick description of my project - it consists of a main file that is calling a few header and source files, which all in the same directory. In the main file I'm also calling an external header file ("coupling.h", which is located in "/opt/package/API_SRC_Files/Coupling") which describes some classes relating to coupling to that software. The "coupling.h" file in turn references some files in "/opt/package/API_SRC_Files/Coupling". I also need to link to a shared library file "libcoupling.so" that is located in "/opt/package/lib".
Setting this up in code::blocks was pretty easy - just go to build options, and the respective paths to the search directories, and the path to the linked file and then build. Compiles in a few seconds.
I've tried to setup the project in CLion and CMake but I'm truly lost and I don't really understand why CMake is not finding the "coupling.h" file and throws countless "undefined reference to " errors. I'm sure I'll also have a problem setting up the shared library and I'm scared to even think about the difference in debug and release versions at the moment!
Here's my current CMake.txt file, hopefully someone can help. I'm using CLion 2020.1 on Fedora.
cmake_minimum_required(VERSION 3.16)
project(MBD VERSION 0.6.1)
set(CMAKE_CXX_STANDARD 14)
# add extra include directories
include_directories(.)
include_directories(/opt/package/API_SRC_Files)
include_directories(/opt/package/API_SRC_Files/Core)
include_directories(/opt/package/API_SRC_Files/Coupling)
set(PROJECT_HEADERS
coupling_utilities.h
geometry.h
io.h
shapelib.h
pid.h
spline.h
)
set(PROJECT_SOURCES
main.cpp
io.cpp
coupling_utilities.cpp
shapelib.cpp
pid.cpp
)
# add extra lib directories
link_directories(/opt/package/lib)
add_executable(MBD ${PROJECT_SOURCES} ${PROJECT_HEADERS})
Here's the error log from the output related to the include_directories():
/snap/clion/114/bin/cmake/linux/bin/cmake --build /home/user/CLionProjects/mbd/cmake-build-debug --target mbd -- -j 24
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/CLionProjects/mbd/cmake-build-debug
[ 16%] Linking CXX executable mbd
/usr/bin/ld: CMakeFiles/mbd.dir/main.cpp.o: in function `main':
/home/user/CLionProjects/mbd/main.cpp:224: undefined reference to `NApi::Coupling::getGeometryId(char const*, int&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:260: undefined reference to `NApi::Coupling::getTimeStep(double&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:268: undefined reference to `NApi::Coupling::getTime(double&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:273: undefined reference to `NApi::Coupling::setTime(double const&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:276: undefined reference to `NApi::Coupling::getTime(double&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:285: undefined reference to `NApi::Coupling::setGridCellSize(double const&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:288: undefined reference to `NApi::Coupling::setNumberOfCores(int const&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:343: undefined reference to `NApi::Coupling::getGeometryPosition(int, NApi::C3dValue&, NApi::C3x3Matrix&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:345: undefined reference to `NApi::Coupling::getGeometryTranslation(int, NApi::C3dValue&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:348: undefined reference to `NApi::Coupling::getGeometryVelocity(int, NApi::C3dValue&, NApi::C3dValue&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:442: undefined reference to `NApi::Coupling::isConnected() const'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:444: undefined reference to `NApi::Coupling::getGeometryForces(int, NApi::C3dValue&, NApi::C3dValue&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:531: undefined reference to `NApi::Coupling::isConnected() const'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:534: undefined reference to `NApi::Coupling::setGeometryMotion(int, NApi::C3dValue const&, NApi::C3x3Matrix const&, NApi::C3dValue const&, NApi::C3dValue const&, double, bool)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:554: undefined reference to `NApi::Coupling::isConnected() const'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:556: undefined reference to `NApi::Coupling::simulate(double const&, double)'
/usr/bin/ld: CMakeFiles/mbd.dir/main.cpp.o: in function `__static_initialization_and_destruction_0(int, int)':
/home/user/CLionProjects/mbd/main.cpp:74: undefined reference to `NApi::Coupling::ICoupling()'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:74: undefined reference to `NApi::Coupling::~ICoupling()'
/usr/bin/ld: CMakeFiles/mbd.dir/coupling_utilities.cpp.o: in function `connect(NApi::Coupling&)':
/home/user/CLionProjects/mbd/coupling_utilities.cpp:54: undefined reference to `NApi::Coupling::initialiseCoupling()'
/usr/bin/ld: /home/user/CLionProjects/mbd/coupling_utilities.cpp:67: undefined reference to `NApi::Coupling::connectCoupling(bool, char const*)'
/usr/bin/ld: /home/user/CLionProjects/mbd/coupling_utilities.cpp:90: undefined reference to `NApi::Coupling::connectCoupling(bool, char const*)'
/usr/bin/ld: CMakeFiles/mbd.dir/coupling_utilities.cpp.o: in function `getResumeTimestep(NApi::Coupling&, double, double)':
/home/user/CLionProjects/mbd/coupling_utilities.cpp:109: undefined reference to `NApi::Coupling::getNumberOfTimesteps(unsigned int&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/coupling_utilities.cpp:136: undefined reference to `NApi::Coupling::getTimesteps(double*, unsigned int&, unsigned int)'
collect2: error: ld returned 1 exit status
gmake[3]: *** [CMakeFiles/mbd.dir/build.make:144: mbd] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:76: CMakeFiles/mbd.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/mbd.dir/rule] Error 2
gmake: *** [Makefile:118: mbd] Error 2
Here is how I would write the cmakelists.txt file:
cmake_minimum_required(VERSION 3.16)
project(MBD VERSION 0.6.1)
set(CMAKE_CXX_STANDARD 14)
set(API_SRC_Files /opt/package/API_SRC_Files) # ${API_SRC_Files}
set(SRC_FILES
main.cpp
coupling_utilities.h coupling_utilities.cpp
geometry.h
io.h io.cpp
pid.h pid.cpp
shapelib.h shapelib.cpp
spline.h
)
# add extra include directories
include_directories(
includes
.
${API_SRC_Files}
${API_SRC_Files}/Core
${API_SRC_Files}/Coupling
)
# add extra lib directories
link_directories(/opt/package/lib)
add_executable(${PROJECT_NAME} ${SRC_FILES})
Use something like the API_SRC_Files variable for two reasons: reduces errors from having to repeat the same path information, makes it easier if the library is ever moved to a new location. The same thing with using the PROJECT_NAME variable. The comment is to remind myself how to utilize the variable later on in the file.
Combine your SRC_FILES and SRC_HEADERS into one array with source and matching header on the same line. This helps to make sure both are listed. Please note when you tell Clion you want to add a new C++ Class, it will be in the form class.cpp class.h, and not alphabetized. I always list the main.cpp, first.
The dot, ., in the include_directories may or may not be necessary.
Hope this helps.

Cmake not linking when building sfml

I have my project, nested in another directory with the required libraries for my project. I'm using cmake that's bundled with Clion, 3.14. I am using subdirectories with cmake. I have it building just fine, but it's not linking SFML
Root-|
-lib1
-smfl
-lib3
-my_project
I've tried using things like using 'target_link_directories()' but either I did it wrong, or it's absolutely not the right option.
Root CMakeList.txt
cmake_minimum_required(VERSION 3.10)
include_directories("ChaiScript/include" Catch2/include freetype2/include SFML/include )
add_subdirectory(Catch2)
add_subdirectory(ChaiScript)
add_subdirectory(freetype2)
add_subdirectory(SFML)
add_subdirectory(Purrmaid)
./Purrmaid/CMakeList.txt
cmake_minimum_required (VERSION 3.10)
set(CMAKE_CXX_STANDARD 17)
project (purrmaid)
add_executable( purrmaid
main.cpp
Base_Object.cpp
...
ThreadManager.cpp)
target_link_libraries(purrmaid sfml-system sfml-window sfml-graphics sfml-network sfml-audio pthread dl)
../SFML/lib/libsfml-graphics-d.so.2.5.1: undefined reference to `FT_MulFix'
../SFML/lib/libsfml-graphics-d.so.2.5.1: undefined reference to `FT_Init_FreeType'
../SFML/lib/libsfml-graphics-d.so.2.5.1: undefined reference to `FT_Get_Char_Index'
../SFML/lib/libsfml-graphics-d.so.2.5.1: undefined reference to `FT_Get_Kerning'
../SFML/lib/libsfml-graphics-d.so.2.5.1: undefined reference to `FT_Get_Glyph'
../SFML/lib/libsfml-graphics-d.so.2.5.1: undefined reference to `FT_New_Face'
../SFML/lib/libsfml-graphics-d.so.2.5.1: undefined reference to `FT_Stroker_Set'
../SFML/lib/libsfml-graphics-d.so.2.5.1: undefined reference to `FT_Glyph_To_Bitmap'
../SFML/lib/libsfml-graphics-d.so.2.5.1: undefined reference to `FT_Outline_Embolden'
../SFML/lib/libsfml-graphics-d.so.2.5.1: undefined reference to `FT_Load_Char'
../SFML/lib/libsfml-graphics-d.so.2.5.1: undefined reference to `FT_Done_Glyph'
../SFML/lib/libsfml-graphics-d.so.2.5.1: undefined reference to `FT_Stroker_New'
../SFML/lib/libsfml-graphics-d.so.2.5.1: undefined reference to `FT_Open_Face'
../SFML/lib/libsfml-graphics-d.so.2.5.1: undefined reference to `FT_Glyph_Stroke'
../SFML/lib/libsfml-graphics-d.so.2.5.1: undefined reference to `FT_Bitmap_Embolden'
../SFML/lib/libsfml-graphics-d.so.2.5.1: undefined reference to `FT_Done_Face'
../SFML/lib/libsfml-graphics-d.so.2.5.1: undefined reference to `FT_New_Memory_Face'
../SFML/lib/libsfml-graphics-d.so.2.5.1: undefined reference to `FT_Stroker_Done'
../SFML/lib/libsfml-graphics-d.so.2.5.1: undefined reference to `FT_Done_FreeType'
../SFML/lib/libsfml-graphics-d.so.2.5.1: undefined reference to `FT_Set_Pixel_Sizes'
../SFML/lib/libsfml-graphics-d.so.2.5.1: undefined reference to `FT_Select_Charmap'
The error shown is that SFML cannot link because it cannot find reference to different FT_ symbols (FT_MulFix, FT_Init_FreeType, etc.)
These are defined by the FreeType library. I assume you are building the different SFML targets when you add_subdirectory(SFML)
In the CMakeLists.txt file in the SFML subdirectory, do you link the sfml-graphics target against Free type?
add_library(sfml-graphics
...)
# This is assuming you have a "freetype2" target available
# please replace by the actual name of the freetype target
target_link_libraries(sfml-graphics freetype2 ...)
EDIT: Thanks to Tsyvarev for pointing me in the direction of the actual target that was not able to link.

undefined reference to `curl_easy_init' on CLion/Mingw [duplicate]

This question already has answers here:
How to add "-l" (ell) compiler flag in CMake
(2 answers)
Closed 3 years ago.
I am trying to use curl inside c++ code. How should i do it properly ?
I have pasted libcurl.a & libcurl.dll.a in this directory:
C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\lib
This is my CMakeLists.txt from CLion/Mingw
cmake_minimum_required(VERSION 3.3)
project(tan)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -lcurl.dll")
add_executable(tan Tan/mail.cpp)
project(untitled6)
When I add "#define CURL_STATICLIB" i get these errors:
reference to `curl_easy_init'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:84: undefined
reference to `curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:86: undefined reference to `curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:91: undefined reference to `curl_slist_append'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:92: undefined reference to `curl_slist_append'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:93: undefined reference to `curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:98: undefined reference to `curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:99: undefined reference to `curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:100: undefined reference to `curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:103: undefined reference to `curl_easy_perform'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:107: undefined reference to `curl_easy_strerror'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:111: undefined reference to `curl_slist_free_all'
C:/Users/John/CLionProjects/tan/Tam/mail.cpp:121: undefined reference to ````curl_easy_cleanup
Without "#define CURL_STATICLIB" i get these errors:
CMakeFiles\tan.dir/objects.a(mail.cpp.obj): In function `main':
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:81: undefined reference to `__imp_curl_easy_init'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:84: undefined reference to `__imp_curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:86: undefined reference to `__imp_curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:91: undefined reference to `__imp_curl_slist_append'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:92: undefined reference to `__imp_curl_slist_append'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:93: undefined reference to `__imp_curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:98: undefined reference to `__imp_curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:99: undefined reference to `__imp_curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:100: undefined reference to `__imp_curl_easy_setopt'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:103: undefined reference to `__imp_curl_easy_perform'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:107: undefined reference to `__imp_curl_easy_strerror'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:111: undefined reference to `__imp_curl_slist_free_all'
C:/Users/John/CLionProjects/tan/Tan/mail.cpp:121: undefined reference to `__imp_curl_easy_cleanup'
If anybody comes looking use this in CMakeLists.txt. The last line solved the issue.
Step1:
Copy libcurl.dll.a in C:/curl.
No need to copy "libcurl.a" OR "libcurl.dll.a" at any other place.
No need for #define CURL_STATICLIB in main.cpp file
Step2: Edit the CMakeLists.txt as below.
set(CMAKE_CXX_STANDARD 14)
add_executable(tan Tan/mail.cpp)
project(untitled6)
target_link_libraries(tan "C:/curl/libcurl.dll.a")
Step3: If your program flashes quickly and closes. You might be missing some dll files. To check which ones you are missing , run the program (in my case tan.exe) with cmd. Take that dll from curl package (in my case curl-7.65.0-win64-mingw) and put it along side of your executable file. Make sure curl and your executable is compiled with same compiler. In my case it's Mingw.

Linking errors GoogleMock with my C++project under linux

I want to use GMock in my project. First of all I compiled GMock and GTest. The sequence of my actions (I use Linux):
git clone googlemock and googletest
go to GIT/googletest/googlemock/build-aux/ directory run cmake ..
and then make
As the result I got: libgmock.a and libgmock_main.a
My project has the structure: build, inc, src and lib directories. In build directory I run cmake .. and make. In inc dir I placed all headers from GIT/googletest/googlemock/include/ and GIT/googletest/googletest/include/. In lib dir lays libgmock.a only. In src - sources of my project.
My CMakeLists.txt contains:
project(blockchain)
cmake_minimum_required(VERSION 2.6)
set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_SOURCE_DIR})
file(GLOB CPPS "*.cpp")
include_directories("../inc/")
link_directories("../lib/")
add_definitions(-Wall -O2 -std=c++11)
add_executable(${PROJECT_NAME} ${CPPS})
target_link_libraries(blockchain gmock)
install(TARGETS ${PROJECT_NAME} DESTINATION bin)
When I try to make my project (typing cmake .. && make in build dir) I get linking errors:
CMakeFiles/blockchain.dir/main.cpp.o: In function main':
main.cpp:(.text.startup+0x13): undefined reference
totesting::UnitTest::GetInstance()'
main.cpp:(.text.startup+0x1b): undefined reference to testing::UnitTest::Run()' ../lib/libgmock.a(gmock-all.cc.o): In
functiontesting::internal::scoped_ptr
::reset(std::__cxx11::basic_stringstream) [clone .part.81] [clone .constprop.367]':
gmock-all.cc:(.text+0x21a): undefined reference to testing::internal::IsTrue(bool)' ../lib/libgmock.a(gmock-all.cc.o): In
functiontesting::internal::MutexBase::AssertHeld() const [clone
.constprop.368]':
gmock-all.cc:(.text+0x274): undefined reference to testing::internal::IsTrue(bool)' gmock-all.cc:(.text+0x2b2): undefined
reference
totesting::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity,
char const, int)'
gmock-all.cc:(.text+0x2fd): undefined reference to testing::internal::GTestLog::~GTestLog()' gmock-all.cc:(.text+0x312):
undefined reference totesting::internal::GTestLog::~GTestLog()'
../lib/libgmock.a(gmock-all.cc.o): In function testing::internal::Expect(bool, char const*, int,
std::__cxx11::basic_string,
std::allocator > const&) [clone .part.51]':
gmock-all.cc:(.text+0x910): undefined reference
totesting::Message::Message()'
gmock-all.cc:(.text+0x926): undefined reference to testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type,
char const*, int, char const*)' gmock-all.cc:(.text+0x931): undefined
reference
totesting::internal::AssertHelper::operator=(testing::Message const&)
const'
gmock-all.cc:(.text+0x939): undefined reference to testing::internal::AssertHelper::~AssertHelper()'
gmock-all.cc:(.text+0x94b): undefined reference
totesting::internal::IsTrue(bool)'
And a lot of others linking errors. What am I doing wrong?
gmock depends on gtest. You added the former, but not the latter.