node-gyp linking issue for xcode solution - c++

I am generating a solution with node-gyp for xcode under MacOS using:
node-gyp configure -- -f xcode
So for so good, the solution gets generated properly, but it doesn't seem to link properly when building it in xcode, it is complaining about v8 undefined symbols. I am using node 0.10.28.
Undefined symbols for architecture x86_64:
"v8::HandleScope::RawClose(v8::internal::Object**)", referenced from:
v8::Local<v8::String> v8::HandleScope::Close<v8::String>(v8::Handle<v8::String>) in binding.o
"v8::HandleScope::HandleScope()", referenced from:
Method(v8::Arguments const&) in binding.o
"v8::HandleScope::~HandleScope()", referenced from:
Method(v8::Arguments const&) in binding.o
"v8::FunctionTemplate::GetFunction()", referenced from:
void node::SetMethod<v8::Handle<v8::Object> >(v8::Handle<v8::Object>, char const*, v8::Handle<v8::Value> (*)(v8::Arguments const&)) in binding.o
"v8::FunctionTemplate::New(v8::Handle<v8::Value> (*)(v8::Arguments const&), v8::Handle<v8::Value>, v8::Handle<v8::Signature>)", referenced from:
void node::SetMethod<v8::Handle<v8::Object> >(v8::Handle<v8::Object>, char const*, v8::Handle<v8::Value> (*)(v8::Arguments const&)) in binding.o
"v8::Object::Set(v8::Handle<v8::Value>, v8::Handle<v8::Value>, v8::PropertyAttribute)", referenced from:
void node::SetMethod<v8::Handle<v8::Object> >(v8::Handle<v8::Object>, char const*, v8::Handle<v8::Value> (*)(v8::Arguments const&)) in binding.o
"v8::String::New(char const*, int)", referenced from:
Method(v8::Arguments const&) in binding.o
"v8::String::NewSymbol(char const*, int)", referenced from:
void node::SetMethod<v8::Handle<v8::Object> >(v8::Handle<v8::Object>, char const*, v8::Handle<v8::Value> (*)(v8::Arguments const&)) in binding.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I also tried to generate a CMake project that creates a nodejs extension and I am getting the same linking errors. What is the magic twist to link under MacOS with v8/node?
Thanks

Finally the magic trick was to use
-undefined dynamic_lookup
as a linker flag.

Related

symbol(s) not found for architecture arm64 while writing unit tests through Google Test library on M1 Chip [duplicate]

This question already has answers here:
CMake: Unable to link with GMock
(1 answer)
Linking errors GoogleMock with my C++project under linux
(1 answer)
Closed 1 year ago.
I am developing a project in C++ and writing unit tests and using Google Test library for the same. I am on a MacBook Pro with M1 Chip. I installed Google Test through homebrew. On homebrew GTest page, it's mentioned that Gtest is compatible with Apple Silicon.
Apple Silicon Big Sur ✅
I am using CLion.
Here's what my CMakeLists.txt looks like:
cmake_minimum_required(VERSION 3.19)
project(GTest)
include_directories(/opt/homebrew/Cellar/googletest/1.10.0/include)
link_directories(/opt/homebrew/Cellar/googletest/1.10.0/lib)
set(CMAKE_CXX_STANDARD 14)
add_executable(GTest main.cpp)
target_link_libraries(GTest libgmock_main.a)
Do I need a .dylib for this to work on arm64? I was trying to link the libpq static library a while back and it gave me the same linking error. Then I tried to link .dylib instead of .a and it worked on arm64. But I don't see a dynamic library for GTest in lib directory.
Here are all the libraries that are there after installing GTest through homebrew:
libgmock_main.a
libgtest_main.a
libgmock.a
libgtest.a
The linking error I am getting is:
[ 50%] Linking CXX executable GTest
Undefined symbols for architecture arm64:
"testing::InitGoogleMock(int*, char**)", referenced from:
_main in libgmock_main.a(gmock_main.cc.o)
"testing::Test::SetUp()", referenced from:
vtable for TestGetStr_First_Test in main.cpp.o
"testing::Test::TearDown()", referenced from:
vtable for TestGetStr_First_Test in main.cpp.o
"testing::Test::Test()", referenced from:
TestGetStr_First_Test::TestGetStr_First_Test() in main.cpp.o
"testing::Test::~Test()", referenced from:
TestGetStr_First_Test::~TestGetStr_First_Test() in main.cpp.o
"testing::Message::Message()", referenced from:
TestGetStr_First_Test::TestBody() in main.cpp.o
"testing::UnitTest::GetInstance()", referenced from:
_main in libgmock_main.a(gmock_main.cc.o)
"testing::UnitTest::Run()", referenced from:
_main in libgmock_main.a(gmock_main.cc.o)
"testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)", referenced from:
TestGetStr_First_Test::TestBody() in main.cpp.o
"testing::internal::AssertHelper::~AssertHelper()", referenced from:
TestGetStr_First_Test::TestBody() in main.cpp.o
"testing::internal::GetTestTypeId()", referenced from:
___cxx_global_var_init in main.cpp.o
"testing::internal::CmpHelperSTREQ(char const*, char const*, char const*, char const*)", referenced from:
TestGetStr_First_Test::TestBody() in main.cpp.o
"testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)", referenced from:
___cxx_global_var_init in main.cpp.o
"testing::internal::IsTrue(bool)", referenced from:
testing::internal::SuiteApiResolver<testing::Test>::GetSetUpCaseOrSuite(char const*, int) in main.cpp.o
testing::internal::SuiteApiResolver<testing::Test>::GetTearDownCaseOrSuite(char const*, int) in main.cpp.o
"testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)", referenced from:
testing::internal::SuiteApiResolver<testing::Test>::GetSetUpCaseOrSuite(char const*, int) in main.cpp.o
testing::internal::SuiteApiResolver<testing::Test>::GetTearDownCaseOrSuite(char const*, int) in main.cpp.o
"testing::internal::GTestLog::~GTestLog()", referenced from:
testing::internal::SuiteApiResolver<testing::Test>::GetSetUpCaseOrSuite(char const*, int) in main.cpp.o
testing::internal::SuiteApiResolver<testing::Test>::GetTearDownCaseOrSuite(char const*, int) in main.cpp.o
"testing::internal::AssertHelper::operator=(testing::Message const&) const", referenced from:
TestGetStr_First_Test::TestBody() in main.cpp.o
"typeinfo for testing::Test", referenced from:
typeinfo for TestGetStr_First_Test in main.cpp.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [GTest] Error 1
make[2]: *** [CMakeFiles/GTest.dir/all] Error 2
make[1]: *** [CMakeFiles/GTest.dir/rule] Error 2
make: *** [GTest] Error 2
Here's the simple code that I wrote to test Google Test:
#include <iostream>
#include <gtest/gtest.h>
using namespace std;
string getStr(string str)
{
return "1";
}
TEST(TestGetStr, First)
{
ASSERT_STREQ("2", getStr("Arun").c_str());
}
int main()
{
::testing::InitGoogleTest();
return RUN_ALL_TESTS();
}

Linking Tbb library in already set up c++ code (via makefile) Mac OS system

I'm trying to parallelize my code via TBB library, using the tool tbb::parallel_for. I use a Mac OS computer and I downloaded and installed Tbb with Homebrew.
The Tbb is in Macintosh HD⁩ ▸ ⁨opt⁩ ▸ ⁨intel⁩ ▸ ⁨compilers_and_libraries_2020.1.216⁩ ▸ ⁨mac⁩ ▸ ⁨tbb⁩ ▸ ⁨include⁩.
When I try to compile the following error appears:
"tbb::interface5::internal::task_base::destroy(tbb::task&)", referenced from:
tbb::flow::interface10::graph::~graph() in interface_stokes.cpp.o
"tbb::interface7::internal::task_arena_base::internal_terminate()", referenced from:
tbb::interface7::task_arena::terminate() in interface_stokes.cpp.o
"tbb::interface7::internal::task_arena_base::internal_initialize()", referenced from:
tbb::interface7::task_arena::initialize() in interface_stokes.cpp.o
"tbb::task_group_context::init()", referenced from:
tbb::task_group_context::task_group_context(tbb::internal::string_index) in interface_stokes.cpp.o
"tbb::task_group_context::reset()", referenced from:
tbb::flow::interface10::graph::wait_for_all() in interface_stokes.cpp.o
"tbb::task_group_context::~task_group_context()", referenced from:
tbb::flow::interface10::graph::~graph() in interface_stokes.cpp.o
tbb::interface9::internal::start_for<tbb::blocked_range<unsigned long>, tbb::internal::parallel_for_body<main::$_0, unsigned long>, tbb::auto_partitioner const>::run(tbb::blocked_range<unsigned long> const&, tbb::internal::parallel_for_body<main::$_0, unsigned long> const&, tbb::auto_partitioner const&) in interface_stokes.cpp.o
"tbb::task::note_affinity(unsigned short)", referenced from:
vtable for tbb::interface9::internal::flag_task in interface_stokes.cpp.o
"tbb::internal::throw_exception_v4(tbb::internal::exception_id)", referenced from:
tbb::internal::throw_exception(tbb::internal::exception_id) in interface_stokes.cpp.o
"tbb::internal::get_initial_auto_partitioner_divisor()", referenced from:
tbb::interface9::internal::adaptive_mode<tbb::interface9::internal::auto_partition_type>::adaptive_mode() in interface_stokes.cpp.o
"tbb::interface7::internal::task_arena_base::internal_execute(tbb::interface7::internal::delegate_base&) const", referenced from:
void tbb::interface7::task_arena::execute_impl<void, tbb::flow::interface10::graph::wait_functor const>(tbb::flow::interface10::graph::wait_functor const&) in interface_stokes.cpp.o
"tbb::task_group_context::is_group_execution_cancelled() const", referenced from:
tbb::flow::interface10::graph::wait_for_all() in interface_stokes.cpp.o
tbb::task::is_cancelled() const in interface_stokes.cpp.o
"tbb::internal::allocate_child_proxy::allocate(unsigned long) const", referenced from:
tbb::interface9::internal::allocate_sibling(tbb::task*, unsigned long) in interface_stokes.cpp.o
"tbb::internal::allocate_continuation_proxy::free(tbb::task&) const", referenced from:
operator delete(void*, tbb::internal::allocate_continuation_proxy const&) in interface_stokes.cpp.o
"tbb::internal::allocate_continuation_proxy::allocate(unsigned long) const", referenced from:
operator new(unsigned long, tbb::internal::allocate_continuation_proxy const&) in interface_stokes.cpp.o
"tbb::internal::allocate_root_with_context_proxy::free(tbb::task&) const", referenced from:
operator delete(void*, tbb::internal::allocate_root_with_context_proxy const&) in interface_stokes.cpp.o
"tbb::internal::allocate_root_with_context_proxy::allocate(unsigned long) const", referenced from:
operator new(unsigned long, tbb::internal::allocate_root_with_context_proxy const&) in interface_stokes.cpp.o
"typeinfo for tbb::task", referenced from:
typeinfo for tbb::interface9::internal::start_for<tbb::blocked_range<unsigned long>, tbb::internal::parallel_for_body<main::$_0, unsigned long>, tbb::auto_partitioner const> in interface_stokes.cpp.o
typeinfo for tbb::interface9::internal::flag_task in interface_stokes.cpp.o
"vtable for tbb::task", referenced from:
tbb::task::task() in interface_stokes.cpp.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [apps/interface_stokes/interface_stokes] Error 1
make[1]: *** [apps/interface_stokes/CMakeFiles/interface_stokes.dir/all] Error 2
make: *** [all] Error 2
To compile my work (saved in a different folder than TBB) I use a Cmakelists, in which I have add:
#Additional modules path for cmake
set (CMAKE_MODULE_PATH
${CMAKE_MODULE_PATH}
${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include(safeguards)
set(CMAKE_CXX_FLAGS_DEBUG "-std=c++14 -g -fpermissive -fsanitize=address")
set(CMAKE_CXX_FLAGS_RELEASE "-std=c++14 -O3 -mavx -g -fpermissive")
set(CMAKE_CXX_FLAGS_RELEASEASSERT "-std=c++14 -O3 -mavx -g -fpermissive")
find_package(TBB)
if (TBB_FOUND)
include_directories(${TBB_INCLUDE_DIRS})
set(LINK_LIBS ${LINK_LIBS} ${TBB_LIBRARIES})
set(HAVE_INTEL_TBB TRUE)
endif()
include_directories("${PROJECT_BINARY_DIR}")
include_directories("${PROJECT_SOURCE_DIR}")
include_directories(src)
include_directories(contrib)
add_subdirectory(apps)
add_subdirectory(tests)
add_subdirectory(output)
In my program, there is also another folder, named cmake, in which there are some files .cmake, that actually I don't know their purposes.
I've never studied this subject and I 'm working on an already built-up code. I guess that the error is due to a bad linking of my code and the Tbb library.
Does someone know how to fix it?
Thank you so much in advance
Stefano

What is causing GoogleTest Link Error on macOS Mojave? [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
I'm not sure if I have a problem w/ requiring additional arguments to cmake, or a problem with my own Makefiles. I am trying to build and install googletest for use in one of my projects. I am running on macOS mojave, with cmake installed via Homebrew. To install googletest, I have done the following:
git clone https://github.com/google/googletest
cd googletest
mkdir build
cd build
cmake ..
make
make install
I compile my source and test files. Here is an example command that gets generated by the Makefile for compiling a file named Point2d.cpp:
clang++ -arch x86_64 -c -std=c++11 -stdlib=libc++ -O2 -I /usr/local/include -c -o ../build/core/Point2d.o core/Point2d.cpp
I then try to link all my object files. Here is the command generated by the makefile (where main.o is the object file containing the main function for running the tests):
clang++ -arch x86_64 ../build/core/Point2d.o ../build/PointTest.o ../build/main.o -o ../bin/test -L /usr/local/lib
Resulting link error:
Undefined symbols for architecture x86_64:
"testing::InitGoogleTest(int*, char**)", referenced from:
_main in main.o
"testing::Test::SetUp()", referenced from:
vtable for Point2dTests_Constructors_Test in PointTest.o
"testing::Test::TearDown()", referenced from:
vtable for Point2dTests_Constructors_Test in PointTest.o
"testing::Test::Test()", referenced from:
testing::internal::TestFactoryImpl<Point2dTests_Constructors_Test>::CreateTest() in PointTest.o
"testing::Test::~Test()", referenced from:
Point2dTests_Constructors_Test::~Point2dTests_Constructors_Test() in PointTest.o
Point2dTests_Constructors_Test::~Point2dTests_Constructors_Test() in PointTest.o
"testing::Message::Message()", referenced from:
Point2dTests_Constructors_Test::TestBody() in PointTest.o
"testing::UnitTest::GetInstance()", referenced from:
_main in main.o
"testing::UnitTest::Run()", referenced from:
_main in main.o
"testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)", referenced from:
Point2dTests_Constructors_Test::TestBody() in PointTest.o
"testing::internal::AssertHelper::~AssertHelper()", referenced from:
Point2dTests_Constructors_Test::TestBody() in PointTest.o
"testing::internal::GetTestTypeId()", referenced from:
__GLOBAL__sub_I_PointTest.cpp in PointTest.o
"testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)", referenced from:
__GLOBAL__sub_I_PointTest.cpp in PointTest.o
"testing::internal::GetBoolAssertionFailureMessage(testing::AssertionResult const&, char const*, char const*, char const*)", referenced from:
Point2dTests_Constructors_Test::TestBody() in PointTest.o
"testing::internal::IsTrue(bool)", referenced from:
testing::internal::SuiteApiResolver<testing::Test>::GetSetUpCaseOrSuite(char const*, int) in PointTest.o
testing::internal::SuiteApiResolver<testing::Test>::GetTearDownCaseOrSuite(char const*, int) in PointTest.o
"testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)", referenced from:
testing::internal::SuiteApiResolver<testing::Test>::GetSetUpCaseOrSuite(char const*, int) in PointTest.o
testing::internal::SuiteApiResolver<testing::Test>::GetTearDownCaseOrSuite(char const*, int) in PointTest.o
"testing::internal::GTestLog::~GTestLog()", referenced from:
testing::internal::SuiteApiResolver<testing::Test>::GetSetUpCaseOrSuite(char const*, int) in PointTest.o
testing::internal::SuiteApiResolver<testing::Test>::GetTearDownCaseOrSuite(char const*, int) in PointTest.o
"testing::internal::AssertHelper::operator=(testing::Message const&) const", referenced from:
Point2dTests_Constructors_Test::TestBody() in PointTest.o
"typeinfo for testing::Test", referenced from:
typeinfo for Point2dTests_Constructors_Test in PointTest.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [../bin/test] Error 1
make: *** [test] Error 2
Solved. Please see the comments of the question, posted by #AlexDenisov

Cant compile googletest on OSX Yosemite

I have read a few related answers and some articles online as well as the official Google Test documentation, but since I lack experience in this area I am very insecure about a few things and seek guidance.
I am starting a brand new pet project in c++ and I want to write proper tests as I code along, ran into Google Test and decided to give it a try.
Currently this is my folder structure:
/
/bin
/main.cpp
/lib
/lib/db
/lib/external/googletest
My idea is to have the tests and class files in their own folders, for example:
/lib/db/db.h
/lib/db/db.cpp
/lib/db/db.test
Question #1
Is there a naming convention or expectation for the file containing the tests? (/lib/db/db.test in that example above)
Question #2
I ran the following commands
cd lib/external/googletest/googletest
cmake .
make
make install
cp lib*.a /usr/local/lib
cp -r include/gtest/ /usr/local/include/
And seeing how it ended up creating a series of .h files in /usr/local/include/gtest, I expected to be able to #include <gtest/gtest.h> and be able to run tests, I added this to my main.cpp:
TEST(Param1, Param2) {
EXPECT_EQ(1, 1);
}
and when building the following error showed up:
Undefined symbols for architecture x86_64:
"testing::AssertionSuccess()", referenced from:
testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&) in main-230b96.o
"testing::Test::SetUp()", referenced from:
vtable for Param1_Param2_Test in main-230b96.o
"testing::Test::TearDown()", referenced from:
vtable for Param1_Param2_Test in main-230b96.o
"testing::Test::Test()", referenced from:
Param1_Param2_Test::Param1_Param2_Test() in main-230b96.o
"testing::Test::~Test()", referenced from:
Param1_Param2_Test::~Param1_Param2_Test() in main-230b96.o
"testing::Message::Message()", referenced from:
Param1_Param2_Test::TestBody() in main-230b96.o
"testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)", referenced from:
Param1_Param2_Test::TestBody() in main-230b96.o
"testing::internal::AssertHelper::~AssertHelper()", referenced from:
Param1_Param2_Test::TestBody() in main-230b96.o
"testing::internal::GetTestTypeId()", referenced from:
___cxx_global_var_init in main-230b96.o
"testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)", referenced from:
___cxx_global_var_init in main-230b96.o
"testing::internal::IsTrue(bool)", referenced from:
testing::internal::scoped_ptr<std::__1::basic_stringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::reset(std::__1::basic_stringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) in main-230b96.o
testing::internal::scoped_ptr<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::reset(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) in main-230b96.o
"testing::internal::EqFailure(char const*, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool)", referenced from:
testing::AssertionResult testing::internal::CmpHelperEQFailure<int, int>(char const*, char const*, int const&, int const&) in main-230b96.o
"testing::internal::AssertHelper::operator=(testing::Message const&) const", referenced from:
Param1_Param2_Test::TestBody() in main-230b96.o
"typeinfo for testing::Test", referenced from:
typeinfo for Param1_Param2_Test in main-230b96.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [all] Error 1
Solution
Thanks to trojanfoe I fixed my makefile to look like this:
CC=g++
target_folder=bin
all:
$(CC) *.cpp -o $(target_folder)/prod
./bin/prod
test:
$(CC) *.cpp -o $(target_folder)/test lib/external/googletest/googletest/libgtest.a lib/external/googletest/googletest/libgtest_main.a
./bin/test
clean:
rm -f $(target_folder)/* *.o
And now it works just fine, please keep in mind that this is a skeleton project and I need to structure the test files and entry point, so at this point its just compiling a basic test.
You have forgotten to link-in the google test library with your executable.

Linking external code in XCode

I'm working with an external code in my project which I have access to the headers and the .cc files but I don't have any access to .dylibs. I'm getting some errors, related to link of the libs, that is described bellow.
This code runs perfectly well if I run it directly from the terminal. The problem only occured when I tried to incorporate it on my own project (that have other modules) in XCode. I guess that if I add the .dylib files to XCode it will solve the problem. However, I don't have the .dylibs. How can I put this code to run?
Undefined symbols for architecture x86_64:
"FACETRACKER::IO::LoadCon(char const*)", referenced from:
mark(int, char const**) in main.o
"FACETRACKER::IO::LoadTri(char const*)", referenced from:
mark(int, char const**) in main.o
"FACETRACKER::CLM::GetViewIdx()", referenced from:
mark(int, char const**) in main.o
"FACETRACKER::FDet::~FDet()", referenced from:
FACETRACKER::Tracker::~Tracker() in main.o
FACETRACKER::Tracker::Tracker(char const*) in main.o
"FACETRACKER::Tracker::Load(char const*)", referenced from:
FACETRACKER::Tracker::Tracker(char const*) in main.o
"FACETRACKER::Tracker::Track(cv::Mat, std::__1::vector<int, std::__1::allocator<int> >&, int, int, double, double, bool)", referenced from:
mark(int, char const**) in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)