I'm trying to use the gcc command to compile and run a .cpp file to test google protobuf.
CPP_TEST.cpp
#include "GameInfo.pb.h"
int main() {
// ...
}
GameInfo.pb.h generated by protoc
#ifndef PROTOBUF_GameInfo_2eproto__INCLUDED
#define PROTOBUF_GameInfo_2eproto__INCLUDED
#include <string>
#include <google/protobuf/stubs/common.h>
// ...
and my files in folder looks like this
- test
CPP_TEST.cpp
GameInfo.pb.h
GameInfo.pb.cc
// the lib file for protobuf
libprotobuf.a
// the srouce code of protobuf
- google
- protobuf
// ...
- stubs
common.h
// ...
and then I tried to compile the .cpp file
gcc CPP_TEST.cpp -l ./google -o OUT_CPP_TEST
But get the error:
#include <google/protobuf/stubs/common.h>
'google/protobuf/stubs/common.h' file not found with <angled> include; use "quotes" instead
I think it is an error with the gcc compiler flag but can not figure out why...
Any advice will be appreciated, thanks :)
UPDATE:
After changing the command to
gcc CPP_TEST.cpp -I./ -o OUT_CPP_TEST
The file is ok to compiler and run.
But if I add a code to the main function :
game::info::GameInfo gameInfoOut;
And compiler it will fail with:
Undefined symbols for architecture x86_64:
"game::info::GameInfo::GameInfo()", referenced from:
_main in CPP_TEST-9245ef.o
"game::info::GameInfo::~GameInfo()", referenced from:
_main in CPP_TEST-9245ef.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
So I tried to add the libprotobuf.a file to gcc command
gcc CPP_TEST.cpp -L./ -lprotobuf -I./ -o CPP_TEST_OUT
But I am still getting the same error:
Undefined symbols for architecture x86_64:
"game::info::GameInfo::GameInfo()", referenced from:
_main in CPP_TEST-0e3576.o
"game::info::GameInfo::~GameInfo()", referenced from:
_main in CPP_TEST-0e3576.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
UPDATE:
I tried to complier and link all the files step by step like:
gcc -c -I./ GameInfo.pb.cc
gcc -c -I./ GameEnum.pb.cc
gcc -c -I./ CPP_TEST.cpp
and link
gcc CPP_TEST.o GameInfo.pb.o GameEnum.pb.o -L./ -lprotobuf -o main
And now I am getting bunch of errors like:
Undefined symbols for architecture x86_64:
"___dynamic_cast", referenced from:
game::info::RoleInfo const* google::protobuf::internal::dynamic_cast_if_available<game::info::RoleInfo const*, google::protobuf::Message const*>(google::protobuf::Message const*) in GameInfo.pb.o
game::info::ItemInfo const* google::protobuf::internal::dynamic_cast_if_available<game::info::ItemInfo const*, google::protobuf::Message const*>(google::protobuf::Message const*) in GameInfo.pb.o
game::info::GameInfo const* google::protobuf::internal::dynamic_cast_if_available<game::info::GameInfo const*, google::protobuf::Message const*>(google::protobuf::Message const*) in GameInfo.pb.o
google::protobuf::FileDescriptorSet const* google::protobuf::internal::dynamic_cast_if_available<google::protobuf::FileDescriptorSet const*, google::protobuf::Message const*>(google::protobuf::Message const*) in libprotobuf.a(descriptor.pb.o)
google::protobuf::FileDescriptorProto const* google::protobuf::internal::dynamic_cast_if_available<google::protobuf::FileDescriptorProto const*, google::protobuf::Message const*>(google::protobuf::Message const*) in libprotobuf.a(descriptor.pb.o)
google::protobuf::DescriptorProto_ExtensionRange const* google::protobuf::internal::dynamic_cast_if_available<google::protobuf::DescriptorProto_ExtensionRange const*, google::protobuf::Message const*>(google::protobuf::Message const*) in libprotobuf.a(descriptor.pb.o)
google::protobuf::DescriptorProto const* google::protobuf::internal::dynamic_cast_if_available<google::protobuf::DescriptorProto const*, google::protobuf::Message const*>(google::protobuf::Message const*) in libprotobuf.a(descriptor.pb.o)
...
"___gxx_personality_v0", referenced from:
game::enumeration::protobuf_AssignDesc_GameEnum_2eproto() in GameEnum.pb.o
google::protobuf::GoogleOnceInit(long*, void (*)()) in GameEnum.pb.o
Dwarf Exception Unwind Info (__eh_frame) in GameEnum.pb.o
game::info::protobuf_AssignDesc_GameInfo_2eproto() in GameInfo.pb.o
game::info::protobuf_AddDesc_GameInfo_2eproto() in GameInfo.pb.o
game::info::RoleInfo::RoleInfo() in GameInfo.pb.o
game::info::RoleInfo::RoleInfo(game::info::RoleInfo const&) in GameInfo.pb.o
// more here
It is really weird since the I can complier and run it successfully in the xcode with all the GameInfo.pb.h/cc GameEnum.pb.h/cc, libprotobuf.a and "google source".
gcc doesn't know the include directory of google headers until you mention the path using -I flag not -l. As google directory is in the current working directory so no need to pass ./google for -I.
g++ -I./ CPP_TEST.cpp -o OUT_CPP_TEST
or else you can just use what your error suggestion said (use quotes instead of angles) because your file hierarchy matches this suggestion.
#include "google/protobuf/stubs/common.h"
Related
Error:
"subhook_unprotect(void*, unsigned long)", referenced from:
_subhook_new in subhook-9679a6.o
ld: symbol(s) not found for architecture x86_64
Linking command: g++ -dynamiclib -fPIC -v -o finalcalling.dylib finalversion.cpp /Users/~/Desktop/c/subhook-master/subhook.c -std=c++11
After going through my code I found that subhook_unprotect(void*, unsigned long) is not even in my code.
If this is your code https://github.com/Zeex/subhook then it seems you are supposed to also include subhook_unix.c in your build. That file does define subhook_unprotect. So does subhook_windows.c but I'm assuming you are on a unix like platform.
I am trying to SNAP with C++ on Xcode. I installed SNAP and provided these two header search paths to Xcode: /usr/local/Cellar/Snap-3.0/snap-core/ and /usr/local/Cellar/Snap-3.0/glib-core/.
But it still produces the error below. I believe I need to fill in "other linker flags" in Xcode, but I can't seem to get it right. Thanks for help.
The C++ Code is below:
#include "Snap.h"
int main() {
return 0;
}
Error Message Below
Undefined symbols for architecture x86_64:
"ExeStop(char const*, char const*, char const*, char const*, int const&)", referenced from:
TRStr::UnRef() in main.o
TRStr::~TRStr() in main.o
TRStr::GetNullRStr() in main.o
"TStr::GetRStr(char const*)", referenced from:
TStr::TStr(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)**
It works once I add -lSnap.o in "other linker flags" in Xcode
I upgraded to maverics and now the macports version of boost-python doesn't work.
I installed ports: gcc48 and boost +python33.
I tried to compile this program with c++11 or without c++11:
#include <boost/python.hpp>
class A {};
BOOST_PYTHON_MODULE(libblah) {
boost::python::class_<A>("A");
}
I used cmake, but the build commands generated are below along with the linker error that I get:
[100%] Building CXX object CMakeFiles/a.dir/a.cc.o
g++ -Da_EXPORTS -std=gnu++0x -save-temps=obj -arch x86_64 -fPIC -I/Users/neil/nn/src -I/opt/local/include -I/opt/local/include/freetype2 -I/opt/local/Library/Frameworks/Python.framework/Headers -F/usr/local/Qt-5.2.0/lib -I/usr/local/Qt-5.2.0/lib/QtCore.framework/Headers -I/usr/local/Qt-5.2.0/mkspecs/macx-clang -I/usr/local/Qt-5.2.0/lib/QtGui.framework/Headers -I/System/Library/Frameworks/OpenGL.framework/Headers -I/usr/local/Qt-5.2.0/lib/QtOpenGL.framework/Headers -I/usr/local/Qt-5.2.0/lib/QtWidgets.framework/Headers -I/opt/local/Library/Frameworks/Python.framework/Versions/3.3/include/python3.3m -I/Users/neil/nn/src/. -o CMakeFiles/a.dir/a.cc.o -c /Users/neil/nn/src/a.cc
Linking CXX shared module liba.so
g++ -std=gnu++0x -save-temps=obj -arch x86_64 -bundle -Wl,-headerpad_max_install_names -o liba.so CMakeFiles/a.dir/a.cc.o -L/opt/local/lib -L/Users/neil/nn/src /opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/libpython3.3.dylib /opt/local/lib/libboost_python-mt.dylib -Wl,-rpath,/opt/local/lib -Wl,-rpath,/Users/neil/nn/src
Undefined symbols for architecture x86_64:
"boost::python::objects::function_object(boost::python::objects::py_function const&, std::pair<boost::python::detail::keyword const*, boost::python::detail::keyword const*> const&)", referenced from:
boost::python::api::object boost::python::detail::make_function_aux<void (*)(_object*), boost::python::default_call_policies, boost::mpl::vector2<void, _object*>, mpl_::int_<0> >(void (*)(_object*), boost::python::default_call_policies const&, boost::mpl::vector2<void, _object*> const&, std::pair<boost::python::detail::keyword const*, boost::python::detail::keyword const*> const&, mpl_::int_<0>) in a.cc.o
"boost::python::objects::register_dynamic_id_aux(boost::python::type_info, std::pair<void*, boost::python::type_info> (*)(void*))", referenced from:
void boost::python::objects::register_dynamic_id<A>(A*) in a.cc.o
ld: symbol(s) not found for architecture x86_64
How can I work around this bug without installing boost by myself. As with this Related question, the symbols in the library
nm libboost_python-mt.dylib | c++filt | grep boost::python::objects::function_object
are found:
0000000000013d60 T boost::python::objects::function_object(boost::python::objects::py_function const&)
0000000000013d00 T boost::python::objects::function_object(boost::python::objects::py_function const&, std::__1::pair<boost::python::detail::keyword const*, boost::python::detail::keyword const*> const&)
However, they have std::__1 prefixes, which do not match what is expected in the undefined symbols errors.
I changed the compiler to clang++, and that worked. Would have preferred a better solution.
I'm trying to compile the Sam Hare's Struck code.
I'm using mac OSX10.9, opencv 2.4.6 and Eigen 2.0.17.
Eigen and opencv headers are stored in /opt/local/include while opencv dylib in /opt/local/lib.
I modified the Hare's Makefile to work on this folder. When I type make on the terminal:
g++ -L/opt/local/lib -lopencv_core -lopencv_highgui -lopencv_imgproc src/Config.o src/Features.o src/HaarFeature.o src/HaarFeatures.o src/HistogramFeatures.o src/ImageRep.o src/LaRank.o src/MultiFeatures.o src/RawFeatures.o src/Sampler.o src/Tracker.o src/main.o src/GraphUtils/GraphUtils.o -o struck
I get these errors:
Undefined symbols for architecture x86_64:
"cv::namedWindow(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > const&,
int)", referenced from:
_main in main.o "cv::split(cv::Mat const&, std::__1::vector<cv::Mat, std::__1::allocator<cv::Mat> >&)",
referenced from:
ImageRep::ImageRep(cv::Mat const&, bool, bool, bool) in ImageRep.o "cv::imread(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > const&,
int)", referenced from:
_main in main.o "cv::imshow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&,
cv::_InputArray const&)", referenced from:
LaRank::Debug() in LaRank.o
Tracker::Debug() in Tracker.o
_main 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)
Any ideas? Thanks!
I had a similar warning/error/failure when I was simply trying to make an executable from two different object files (main.o and add.o). I was using the command:
gcc -o exec main.o add.o
But my program is a C++ program. Using the g++ compiler solved my issue:
g++ -o exec main.o add.o
I was always under the impression that gcc could figure these things out on its own. Apparently not. I hope this helps someone else searching for this error.
finally solved my problem.
I created a new project in XCode with the sources and changed the C++ Standard Library from the default libc++ to libstdc++ as in this and this.
I am getting the same error. I don't think it is a "fix", but my work-around is to also include the cpp file. So instead of just putting
#include "MyClass.h"
I have to put
#include "MyClass.h"
#include "MyClass.cpp"
I wrote my declarition in the .h file
class String
{
String (const char * cstr = 0);
};
and I used inline in another .cpp file (implementation)
inline String::String(const char * cstr)
{
//code ...
}
then I use g++ and get this:
Undefined symbols for architecture x86_64:
"String::String(char const*)", referenced from:
_main in test-d389f3.o
ld: symbol(s) not found for architecture x86_64
solution: do not write inline when declarition and implementation is independent.
When I using clang to compile a C++ program, I have the similar error.
But after I changing to use chang++, the compilation worked.
I am currently trying to build some test code that uses Google C++ Test framework but I keep getting an error stating
ld: warning: in /usr/local/lib/libgtest.dylib, file was built for unsupported file format which is not the architecture being linked (i386)
I have tried to make the issue as simple as possible:
I have a main function
cmtest.cc
#include <gtest/gtest.h>
/** Main entry point */
int main(int argc, char**argv, char**envArg)
{
testing::InitGoogleTest(&argc, argv);
return(RUN_ALL_TESTS());
}
really basic test code
CrazyTest.cc
#include <gtest/gtest.h>
TEST(CrazyTest, one) {
EXPECT_EQ(2, 2);
}
I use the following commands to build gtest and my test code.
g++ -o CrazyTest.o -c -Wall -Werror=non-virtual-dtor -pipe -std=c++98 -fno-rtti -fno-exceptions -fno-strict-aliasing -Wno-deprecated -g -arch i386 -arch x86_64 -mmacosx-version-min=10.5.4 -DGTEST_HAS_TR1_TUPLE=0 -DGTEST_HAS_RTTI=0 -I/opt/gtest-1.6.0/include CrazyTest.cc
g++ -o cmtest.o -c -Wall -Werror=non-virtual-dtor -pipe -std=c++98 -fno-rtti -fno-exceptions -fno-strict-aliasing -Wno-deprecated -g -arch i386 -arch x86_64 -mmacosx-version-min=10.5.4 -DGTEST_HAS_TR1_TUPLE=0 -DGTEST_HAS_RTTI=0 -I/opt/gtest-1.6.0/include cmtest.cc
g++ -o gtest-all.o -c -Wall -Werror=non-virtual-dtor -pipe -std=c++98 -fno-rtti -fno-exceptions -fno-strict-aliasing -Wno-deprecated -g -arch i386 -arch x86_64 -mmacosx-version-min=10.5.4 -DGTEST_HAS_TR1_TUPLE=0 -DGTEST_HAS_RTTI=0 -I/opt/gtest-1.6.0 -I/opt/gtest-1.6.0/include /opt/gtest-1.6.0/src/gtest-all.cc
ar rc libgtest.a gtest-all.o
ranlib libgtest.a
g++ -o cmtest -arch i386 -arch x86_64 -mmacosx-version-min=10.5.4 CrazyTest.o cmtest.o -lstdc++ -lgtest
The final build step gives me the following error and I am unable to figure out why. I am able to get the actual tests (not the simple one shown) to build on other OSs the mac OS (leopard) is giving me problems.
ld: warning: in /usr/local/lib/libgtest.dylib, file was built for unsupported file format which is not the architecture being linked (i386)
Undefined symbols for architecture i386:
"testing::Test::~Test()", referenced from:
CrazyTest_one_Test::~CrazyTest_one_Test()in CrazyTest.o
CrazyTest_one_Test::~CrazyTest_one_Test()in CrazyTest.o
"testing::internal::AssertHelper::~AssertHelper()", referenced from:
CrazyTest_one_Test::TestBody() in CrazyTest.o
"testing::Test::Test()", referenced from:
CrazyTest_one_Test::CrazyTest_one_Test()in CrazyTest.o
"testing::internal::GetTestTypeId()", referenced from:
__static_initialization_and_destruction_0(int, int)in CrazyTest.o
"testing::Test::TearDown()", referenced from:
vtable for CrazyTest_one_Testin CrazyTest.o
"testing::internal::EqFailure(char const*, char const*, testing::internal::String const&, testing::internal::String const&, bool)", referenced from:
testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&)in CrazyTest.o
"testing::UnitTest::GetInstance()", referenced from:
_main in cmtest.o
"testing::internal::IsTrue(bool)", referenced from:
testing::internal::scoped_ptr<std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> > >::reset(std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >*)in CrazyTest.o
testing::internal::scoped_ptr<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::reset(std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)in CrazyTest.o
"testing::UnitTest::Run()", referenced from:
_main in cmtest.o
"testing::internal::AssertHelper::operator=(testing::Message const&) const", referenced from:
CrazyTest_one_Test::TestBody() in CrazyTest.o
"testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)", referenced from:
CrazyTest_one_Test::TestBody() in CrazyTest.o
"testing::AssertionSuccess()", referenced from:
testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&)in CrazyTest.o
"testing::Test::SetUp()", referenced from:
vtable for CrazyTest_one_Testin CrazyTest.o
"testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)", referenced from:
__static_initialization_and_destruction_0(int, int)in CrazyTest.o
"testing::InitGoogleTest(int*, char**)", referenced from:
_main in cmtest.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
lipo: can't open input file: /var/tmp//ccZQiF8k.out (No such file or directory)
I have defined -arch i386 and -arch x86_64 for everything I have built so I am unable to figure out what I have missed. I don't do a lot of programming on Macs and this particular issue has me stuck.
Any suggestions would be helpful.
Solution found. It turns out that the build instructions used are correct the issue was that on the computer that the build was being performed another developer had done some work with Google Test Framework 1.5 and they had installed the libraries on the computer in the compilers search path. This resulted in the compiler finding the other library first that was not compiled using multiple architecture options.
Compile gtest and your project with the same compiler (-flags).
If you compiled gtest with Apple g++ and meanwhile installed gcc with e.g. homebrew, linking to gtest will cause this error. This is exactly the reason why Google advices to integrate gtest statically linked in your project, and not to use precompiled binaries.
You might also have this problem if gmock and gtest are out of sync. I had similar build errors with gmock-1.6.0 and gtest-1.7.0:
../../../third_party/gmock/build/libgmock.a(gmock-all.cc.o): In function `testing::internal::ParseGoogleMockFlagValue(char const*, char const*, bool)':
gmock-all.cc:(.text+0x3b5c): undefined reference to `testing::internal::String::Format(char const*, ...)'
../../../third_party/gmock/build/libgmock.a(gmock-all.cc.o): In function `testing::internal::String::operator==(char const*) const':
gmock-all.cc:(.text._ZNK7testing8internal6StringeqEPKc[testing::internal::String::operator==(char const*) const]+0x33): undefined reference to `testing::internal::String::Compare(testing::internal::String const&) const'
collect2: ld returned 1 exit status
Switching to both 1.7.0 or both 1.6.0 fixed the problem.