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

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

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 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)

Eclipse C++ for Mac settings (<iostream> fix) and error correction

I am testing out eclipse C++ for Mac. I have three specific questions.
This is the classic iostream problem for Mac. It does not know where to find it. So in the project properties I add the path where iostream is located (Preferences->C/C++ General->Paths and Symbols->includes). Now iostream works. But I am trying to find a way where I don't have to do this for every new project. Is there a specific setting I can select to include this directory automatically for each new project?
Is xCode necessary for eclipse to work? If yes, how can I get eclipse to work without it? xCode takes up 6 GBs.
I'm getting an error on a test project (see below) that I'm not sure how to fix. Something about "linker command failed". I seem to be able to compile regular C programs fine though. Note this project works fine in Visual Studio.
23:37:30 **** Incremental Build of configuration Debug for project pointClass ****
make all
Building target: pointClass
Invoking: MacOS X C++ Linker
g++ -o "pointClass" ./point.o ./test.o
Undefined symbols for architecture x86_64:
"std::istream::operator>>(double&)", referenced from:
_main in test.o
"std::ostream::operator<<(std::ostream& ()(std::ostream&))", referenced from:
_main in test.o
Point::Point(double, double) in test.o
Point::Point(Point const&) in test.o
"std::ostream::operator<<(double)", referenced from:
_main in test.o
Point::Point(double, double) in test.o
"std::basic_string, std::allocator >::~basic_string()", referenced from:
_main in test.o
"std::ios_base::Init::Init()", referenced from:
___cxx_global_var_init in test.o
"std::ios_base::Init::~Init()", referenced from:
___cxx_global_var_init in test.o
"std::cin", referenced from:
_main in test.o
"std::cout", referenced from:
_main in test.o
Point::Point(double, double) in test.o
Point::Point(Point const&) in test.o
"std::basic_ostream >& std::endl >(std::basic_ostream >&)", referenced from:
_main in test.o
Point::Point(double, double) in test.o
Point::Point(Point const&) in test.o
"std::basic_ostream >& std::operator<< >(std::basic_ostream >&, char const)", referenced from:
_main in test.o
Point::Point(double, double) in test.o
Point::Point(Point const&) in test.o
"std::basic_ostream >& std::operator<<, std::allocator >(std::basic_ostream >&, std::basic_string, std::allocator > const&)", referenced from:
_main in test.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: *** [pointClass] Error 1

opencv Apple Mach-O Linker

I am trying to run an opencv project by xcode6.
This tutorial is cool and works for me, first, without xcode: The application runs perfectly after make'ing.
Now, I want to use Xcode 6 as development environment.
But I get an Apple Mach-O Linker error.
Build Settings are:
Header Search Paths : /usr/local/include (non-recursive)
Library Search Paths: /urs/local/lib (non-recursive)
Build Phases > Link Binary With Libraries:
Libc++.dylib
This is the error:
Ld /Users/XXX/Library/Developer/Xcode/DerivedData/HelloWorld-ejnkjggujedpyibjbhvocitoxxaj/Build/Products/Debug/HelloWorld normal x86_64
cd /Users/XXX/Desktop/make/CLTproject/HelloWorld/HelloWorld
export MACOSX_DEPLOYMENT_TARGET=10.10
/Applications/Xcode-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -L/Users/XXX/Library/Developer/Xcode/DerivedData/HelloWorld-ejnkjggujedpyibjbhvocitoxxaj/Build/Products/Debug -L/usr/local/lib -F/Users/XXX/Library/Developer/Xcode/DerivedData/HelloWorld-ejnkjggujedpyibjbhvocitoxxaj/Build/Products/Debug -filelist /Users/XXX/Library/Developer/Xcode/DerivedData/HelloWorld-ejnkjggujedpyibjbhvocitoxxaj/Build/Intermediates/HelloWorld.build/Debug/HelloWorld.build/Objects-normal/x86_64/HelloWorld.LinkFileList -mmacosx-version-min=10.10 -stdlib=libc++ -lc++ -Xlinker -dependency_info -Xlinker /Users/XXX/Library/Developer/Xcode/DerivedData/HelloWorld-ejnkjggujedpyibjbhvocitoxxaj/Build/Intermediates/HelloWorld.build/Debug/HelloWorld.build/Objects-normal/x86_64/HelloWorld_dependency_info.dat -o /Users/XXX/Library/Developer/Xcode/DerivedData/HelloWorld-ejnkjggujedpyibjbhvocitoxxaj/Build/Products/Debug/HelloWorld
Undefined symbols for architecture x86_64:
"cv::namedWindow(cv::String const&, int)", referenced from:
_main in main.o
"cv::GaussianBlur(cv::_InputArray const&, cv::_OutputArray const&, cv::Size_<int>, double, double, int)", referenced from:
_main in main.o
"cv::Mat::deallocate()", referenced from:
cv::Mat::release() in main.o
"cv::Mat::copySize(cv::Mat const&)", referenced from:
cv::Mat::operator=(cv::Mat const&) in main.o
"cv::String::deallocate()", referenced from:
cv::String::~String() in main.o
"cv::String::allocate(unsigned long)", referenced from:
cv::String::String(char const*) in main.o
"cv::imread(cv::String const&, int)", referenced from:
_main in main.o
"cv::imshow(cv::String const&, cv::_InputArray const&)", referenced from:
_main in main.o
"cv::waitKey(int)", referenced from:
_main in main.o
"cv::fastFree(void*)", referenced from:
cv::Mat::~Mat() in main.o
"cv::Mat::copyTo(cv::_OutputArray const&) const", referenced from:
cv::Mat::clone() const in main.o
"vtable for cv::_InputArray", referenced from:
cv::_InputArray::_InputArray() in main.o
cv::_InputArray::_InputArray(cv::Mat const&) in main.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"vtable for cv::_OutputArray", referenced from:
cv::_OutputArray::_OutputArray(cv::Mat&) in main.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)
Any Idea?
Thanks,
isicom
Just to give some light to everybody in the same problem. I added all libraries and worked fine for me. I stayed in the libc++ with no building errors.

node-gyp linking issue for xcode solution

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.