I want to run a unit test of a C++ class with gtest from with a command line under Ubuntu x64. I was following a tutorial from a book, which used the following command to to this:
g++ -o tester.exe MyClass1.cpp MyClass1Test.cpp
-I googletest/googletest -I googletest/googletest/include
-I googletest/googlemock -I googletest/googlemock/include
-I usr/lib/libgtest.a -l -lpthread
Original command suggested by "Mastering C++ Programming" (Jeganathan Swaminathan):
g++ -o tester.exe src/Math.cpp test/MathTest.cpp
-I googletest/googletest -I googletest/googletest/include
-I googletest/googlemock -I googletest/googlemock/include
-I src libgtest.a -lpthread
My 3 files look like this:
"MyClass1.cpp"
#include "MyClass1.h"
bool MyClass1::checkEquality(int number1, int number2) {
int result = number1 - number2;
if(result == 0) {
return true;
}
else {
return false;
}
}
int main() {}
"MyClass1.h"
class MyClass1 {
public:
bool checkEquality(int number1, int number2);
};
"MyClass1Test.cpp"
#include "MyClass1.h"
#include <gtest/gtest.h>
TEST ( MyClass1Test, checkIntEquality) {
MyClass1 myClass;
bool expectedResult = true;
int number1 = 10;
int number2 = 10;
bool result = myClass.checkEquality(number1, number2);
EXPECT_EQ(expectedResult, result);
}
I already tried playing around with the order of the parameters as suggested when I searched for an solution, but this did not give me a different output. I also read that the lpthread library has something to do with this issue but I am unsure what to do about this.
The output I get from the command above is:
/tmp/ccJVQJv2.o: In function `MyClass1Test_checkIntEquality_Test::TestBody()':
MyClass1Test.cpp:(.text+0x72): undefined reference to `testing::Message::Message()'
MyClass1Test.cpp:(.text+0x9f): undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
MyClass1Test.cpp:(.text+0xb2): undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
MyClass1Test.cpp:(.text+0xbe): undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
MyClass1Test.cpp:(.text+0xe7): undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/tmp/ccJVQJv2.o: In function `__static_initialization_and_destruction_0(int, int)':
MyClass1Test.cpp:(.text+0x17b): undefined reference to `testing::internal::GetTestTypeId()'
MyClass1Test.cpp:(.text+0x1e9): undefined reference to `testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
/tmp/ccJVQJv2.o: In function `MyClass1Test_checkIntEquality_Test::MyClass1Test_checkIntEquality_Test()':
MyClass1Test.cpp:(.text._ZN34MyClass1Test_checkIntEquality_TestC2Ev[_ZN34MyClass1Test_checkIntEquality_TestC5Ev]+0x14): undefined reference to `testing::Test::Test()'
/tmp/ccJVQJv2.o: In function `testing::internal::scoped_ptr<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::reset(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)':
MyClass1Test.cpp:(.text._ZN7testing8internal10scoped_ptrINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE5resetEPS7_[_ZN7testing8internal10scoped_ptrINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE5resetEPS7_]+0x24): undefined reference to `testing::internal::IsTrue(bool)'
/tmp/ccJVQJv2.o: In function `testing::internal::scoped_ptr<std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> > >::reset(std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >*)':
MyClass1Test.cpp:(.text._ZN7testing8internal10scoped_ptrINSt7__cxx1118basic_stringstreamIcSt11char_traitsIcESaIcEEEE5resetEPS7_[_ZN7testing8internal10scoped_ptrINSt7__cxx1118basic_stringstreamIcSt11char_traitsIcESaIcEEEE5resetEPS7_]+0x23): undefined reference to `testing::internal::IsTrue(bool)'
/tmp/ccJVQJv2.o: In function `testing::AssertionResult testing::internal::CmpHelperEQ<bool, bool>(char const*, char const*, bool const&, bool const&)':
MyClass1Test.cpp:(.text._ZN7testing8internal11CmpHelperEQIbbEENS_15AssertionResultEPKcS4_RKT_RKT0_[_ZN7testing8internal11CmpHelperEQIbbEENS_15AssertionResultEPKcS4_RKT_RKT0_]+0x36): undefined reference to `testing::AssertionSuccess()'
/tmp/ccJVQJv2.o: In function `testing::AssertionResult testing::internal::CmpHelperEQFailure<bool, bool>(char const*, char const*, bool const&, bool const&)':
MyClass1Test.cpp:(.text._ZN7testing8internal18CmpHelperEQFailureIbbEENS_15AssertionResultEPKcS4_RKT_RKT0_[_ZN7testing8internal18CmpHelperEQFailureIbbEENS_15AssertionResultEPKcS4_RKT_RKT0_]+0x6c): undefined reference to `testing::internal::EqFailure(char const*, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
/tmp/ccJVQJv2.o:(.rodata._ZTV34MyClass1Test_checkIntEquality_Test[_ZTV34MyClass1Test_checkIntEquality_Test]+0x20): undefined reference to `testing::Test::SetUp()'
/tmp/ccJVQJv2.o:(.rodata._ZTV34MyClass1Test_checkIntEquality_Test[_ZTV34MyClass1Test_checkIntEquality_Test]+0x28): undefined reference to `testing::Test::TearDown()'
/tmp/ccJVQJv2.o: In function `MyClass1Test_checkIntEquality_Test::~MyClass1Test_checkIntEquality_Test()':
MyClass1Test.cpp:(.text._ZN34MyClass1Test_checkIntEquality_TestD2Ev[_ZN34MyClass1Test_checkIntEquality_TestD5Ev]+0x20): undefined reference to `testing::Test::~Test()'
/tmp/ccJVQJv2.o:(.rodata._ZTI34MyClass1Test_checkIntEquality_Test[_ZTI34MyClass1Test_checkIntEquality_Test]+0x10): undefined reference to `typeinfo for testing::Test'
collect2: error: ld returned 1 exit status
There is a strange tutorial book. .exe extension is ridiculous for Linux, remove it.
g++ -o tester MyClass1.cpp MyClass1Test.cpp -Igoogletest/googletest -Igoogletest/googletest/include -Igoogletest/googlemock -Ioogletest/googlemock/include -lgtest -lpthread
Or
g++ -o tester MyClass1.cpp MyClass1Test.cpp -Igoogletest/googletest -Igoogletest/googletest/include -Igoogletest/googlemock -Ioogletest/googlemock/include /usr/lib/libgtest.a -lpthread
For Windows you could tune a development environment for Visual Studio with help of the tutorial How to use Google Test for C++ in Visual Studio?
Related
I am trying to run a sample program to create a SHA-1 hash using Cryptopp library in C++.
I have installed these packages on my system (running Ubuntu 20.04):
sudo apt-get install libcrypto++-dev libcrypto++-doc libcrypto++-utils
Here's the code of sample program I'm trying to run:
// test.cpp
#include "cryptopp/cryptlib.h"
#include "cryptopp/sha.h"
#include "cryptopp/files.h"
#include "cryptopp/hex.h"
#include <iostream>
int main(int argc, char *argv[])
{
using namespace CryptoPP;
HexEncoder encoder(new FileSink(std::cout));
std::string msg = "Yoda said, Do or do not. There is no try.";
std::string digest;
SHA1 hash;
hash.Update((const byte *)msg.data(), msg.size());
digest.resize(hash.DigestSize());
hash.Final((byte *)&digest[0]);
std::cout << "Message: " << msg << std::endl;
std::cout << "Digest: ";
StringSource(digest, true, new Redirector(encoder));
std::cout << std::endl;
return 0;
}
Upon compiling the program with given command, I am getting these errors:
elon#starlink:~/$ g++ -o test test.cpp -lcryptopp -lpthread
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP12StringSourceE[_ZTVN8CryptoPP12StringSourceE]+0xd8): undefined reference to `CryptoPP::BufferedTransformation::Skip(unsigned long)'
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP12StringSourceE[_ZTVN8CryptoPP12StringSourceE]+0x128): undefined reference to `CryptoPP::Filter::TransferTo2(CryptoPP::BufferedTransformation&, unsigned long&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP12StringSourceE[_ZTVN8CryptoPP12StringSourceE]+0x130): undefined reference to `CryptoPP::Filter::CopyRangeTo2(CryptoPP::BufferedTransformation&, unsigned long&, unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool) const'
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP14SourceTemplateINS_11StringStoreEEE[_ZTVN8CryptoPP14SourceTemplateINS_11StringStoreEEE]+0xd8): undefined reference to `CryptoPP::BufferedTransformation::Skip(unsigned long)'
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP14SourceTemplateINS_11StringStoreEEE[_ZTVN8CryptoPP14SourceTemplateINS_11StringStoreEEE]+0x128): undefined reference to `CryptoPP::Filter::TransferTo2(CryptoPP::BufferedTransformation&, unsigned long&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP14SourceTemplateINS_11StringStoreEEE[_ZTVN8CryptoPP14SourceTemplateINS_11StringStoreEEE]+0x130): undefined reference to `CryptoPP::Filter::CopyRangeTo2(CryptoPP::BufferedTransformation&, unsigned long&, unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool) const'
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP14InputRejectingINS_6FilterEEE[_ZTVN8CryptoPP14InputRejectingINS_6FilterEEE]+0xd8): undefined reference to `CryptoPP::BufferedTransformation::Skip(unsigned long)'
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP14InputRejectingINS_6FilterEEE[_ZTVN8CryptoPP14InputRejectingINS_6FilterEEE]+0x128): undefined reference to `CryptoPP::Filter::TransferTo2(CryptoPP::BufferedTransformation&, unsigned long&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP14InputRejectingINS_6FilterEEE[_ZTVN8CryptoPP14InputRejectingINS_6FilterEEE]+0x130): undefined reference to `CryptoPP::Filter::CopyRangeTo2(CryptoPP::BufferedTransformation&, unsigned long&, unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool) const'
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP17SimpleProxyFilterE[_ZTVN8CryptoPP17SimpleProxyFilterE]+0xd8): undefined reference to `CryptoPP::BufferedTransformation::Skip(unsigned long)'
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP17SimpleProxyFilterE[_ZTVN8CryptoPP17SimpleProxyFilterE]+0x128): undefined reference to `CryptoPP::Filter::TransferTo2(CryptoPP::BufferedTransformation&, unsigned long&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP17SimpleProxyFilterE[_ZTVN8CryptoPP17SimpleProxyFilterE]+0x130): undefined reference to `CryptoPP::Filter::CopyRangeTo2(CryptoPP::BufferedTransformation&, unsigned long&, unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool) const'
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP23CustomSignalPropagationINS_4SinkEEE[_ZTVN8CryptoPP23CustomSignalPropagationINS_4SinkEEE]+0xd8): undefined reference to `CryptoPP::BufferedTransformation::Skip(unsigned long)'
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP22CustomFlushPropagationINS_4SinkEEE[_ZTVN8CryptoPP22CustomFlushPropagationINS_4SinkEEE]+0xd8): undefined reference to `CryptoPP::BufferedTransformation::Skip(unsigned long)'
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP11UnflushableINS_6FilterEEE[_ZTVN8CryptoPP11UnflushableINS_6FilterEEE]+0xd8): undefined reference to `CryptoPP::BufferedTransformation::Skip(unsigned long)'
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP11UnflushableINS_6FilterEEE[_ZTVN8CryptoPP11UnflushableINS_6FilterEEE]+0x128): undefined reference to `CryptoPP::Filter::TransferTo2(CryptoPP::BufferedTransformation&, unsigned long&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP11UnflushableINS_6FilterEEE[_ZTVN8CryptoPP11UnflushableINS_6FilterEEE]+0x130): undefined reference to `CryptoPP::Filter::CopyRangeTo2(CryptoPP::BufferedTransformation&, unsigned long&, unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool) const'
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP10BufferlessINS_6FilterEEE[_ZTVN8CryptoPP10BufferlessINS_6FilterEEE]+0xd8): undefined reference to `CryptoPP::BufferedTransformation::Skip(unsigned long)'
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP10BufferlessINS_6FilterEEE[_ZTVN8CryptoPP10BufferlessINS_6FilterEEE]+0x128): undefined reference to `CryptoPP::Filter::TransferTo2(CryptoPP::BufferedTransformation&, unsigned long&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP10BufferlessINS_6FilterEEE[_ZTVN8CryptoPP10BufferlessINS_6FilterEEE]+0x130): undefined reference to `CryptoPP::Filter::CopyRangeTo2(CryptoPP::BufferedTransformation&, unsigned long&, unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool) const'
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP4SinkE[_ZTVN8CryptoPP4SinkE]+0xd8): undefined reference to `CryptoPP::BufferedTransformation::Skip(unsigned long)'
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP13AutoSignalingINS_14InputRejectingINS_22BufferedTransformationEEEEE[_ZTVN8CryptoPP13AutoSignalingINS_14InputRejectingINS_22BufferedTransformationEEEEE]+0xd8): undefined reference to `CryptoPP::BufferedTransformation::Skip(unsigned long)'
/usr/bin/ld: /tmp/ccZ6zru9.o:(.data.rel.ro._ZTVN8CryptoPP14InputRejectingINS_22BufferedTransformationEEE[_ZTVN8CryptoPP14InputRejectingINS_22BufferedTransformationEEE]+0xd8): undefined reference to `CryptoPP::BufferedTransformation::Skip(unsigned long)'
/usr/bin/ld: /tmp/ccZ6zru9.o: in function `CryptoPP::SourceTemplate<CryptoPP::StringStore>::Pump2(unsigned long&, bool)':
test.cpp:(.text._ZN8CryptoPP14SourceTemplateINS_11StringStoreEE5Pump2ERmb[_ZN8CryptoPP14SourceTemplateINS_11StringStoreEE5Pump2ERmb]+0x5b): undefined reference to `CryptoPP::StringStore::TransferTo2(CryptoPP::BufferedTransformation&, unsigned long&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
collect2: error: ld returned 1 exit status
Crypto++ Version:
elon#starlink:~/$ grep Library cryptopp/cryptlib.h
/*! \mainpage Crypto++ Library 8.5 API Reference
/// \details LibraryVersion can help detect inadvertent mixing and matching of library
. . .
g++ Version:
elon#starlink:~/$ gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
Does anyone know why the program fails to compile?
I have tried your codes and it seems that you need to include the header file "cryptopp/filters.h", after that, bugs may be solved.
I have some experience in C and C++. But I am new to googletest and googlemock.
I am trying unit testing for C++ Programs.
I did not get any error while I was working on googletest. But, when I started working on googlemock, I got problems.
I have a simple code for working with googlemock, and when I tried to build it with the command
g++ mock.cpp -lgtest -lgtest_main -lgmock -pthread
I get this error which I am not able to understand. (I have attached both the program and the error which I got).
Please help me to understand it and overcome.
Thanks in advance.
mock.cpp
#include<iostream>
#include<vector>
#include<gtest/gtest.h>
#include<gmock/gmock.h>
using namespace std;
using ::testing::AtLeast;
using ::testing::Return;
using ::testing::_;
class DataBaseConnect{
public:
virtual bool login(string username, string password){ return true; }
virtual bool logout(string username){ return true; }
virtual int fetchRecord(){ return -1; }
};
class MockDB : public DataBaseConnect{
public:
MOCK_METHOD0(fetchRecord, int());
MOCK_METHOD1(logout, bool(string username));
MOCK_METHOD2(login, bool(string username, string password));
};
class MyDatabase{
DataBaseConnect & dbC;
public:
MyDatabase(DataBaseConnect & _dbC) : dbC(_dbC) {}
int Init(string username, string password){
if(dbC.login(username, password) != true){
cout<<"DB FAILURE\n";
return -1;
}
else{
cout<<"DB SUCCESS\n";
return 1;
}
}
};
TEST(MyDBTest, LoginTest){
//Arrange
MockDB mdb;
MyDatabase db(mdb);
EXPECT_CALL(mdb, login(_, _))
.Times(1)
.WillOnce(Return(true));
//Act
int retVal = db.Init("Terminator", "I'm Back");
//Assert
EXPECT_EQ(retVal, 1);
}
int main(int argc, char **argv){
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
command:
g++ mock.cpp -lgtest -lgmock -lgtest_main -pthread
error:
g++ mock.cpp -lgtest -lgmock -lgtest_main -pthread
/usr/bin/ld: /tmp/ccFl5wbu.o: in function `testing::internal::linked_ptr_internal::join(testing::internal::linked_ptr_internal const*)':
mock.cpp:(.text._ZN7testing8internal19linked_ptr_internal4joinEPKS1_[_ZN7testing8internal19linked_ptr_internal4joinEPKS1_]+0x2a): undefined reference to `testing::internal::g_linked_ptr_mutex'
/usr/bin/ld: /tmp/ccFl5wbu.o: in function `testing::internal::linked_ptr_internal::depart()':
mock.cpp:(.text._ZN7testing8internal19linked_ptr_internal6departEv[_ZN7testing8internal19linked_ptr_internal6departEv]+0x27): undefined reference to `testing::internal::g_linked_ptr_mutex'
/usr/bin/ld: /tmp/ccFl5wbu.o: in function `testing::internal::FunctionMockerBase<int ()>::InvokeWith(std::tuple<> const&)':
mock.cpp:(.text._ZN7testing8internal18FunctionMockerBaseIFivEE10InvokeWithERKSt5tupleIJEE[_ZN7testing8internal18FunctionMockerBaseIFivEE10InvokeWithERKSt5tupleIJEE]+0x33): undefined reference to `testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const*)'
/usr/bin/ld: /tmp/ccFl5wbu.o: in function `testing::internal::FunctionMockerBase<bool (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)>::InvokeWith(std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&)':
mock.cpp:(.text._ZN7testing8internal18FunctionMockerBaseIFbNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE10InvokeWithERKSt5tupleIJS7_EE[_ZN7testing8internal18FunctionMockerBaseIFbNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEE10InvokeWithERKSt5tupleIJS7_EE]+0x33): undefined reference to `testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const*)'
/usr/bin/ld: /tmp/ccFl5wbu.o: in function `testing::internal::FunctionMockerBase<bool (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)>::InvokeWith(std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&)':
mock.cpp:(.text._ZN7testing8internal18FunctionMockerBaseIFbNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_EE10InvokeWithERKSt5tupleIJS7_S7_EE[_ZN7testing8internal18FunctionMockerBaseIFbNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_EE10InvokeWithERKSt5tupleIJS7_S7_EE]+0x33): undefined reference to `testing::internal::UntypedFunctionMockerBase::UntypedInvokeWith(void const*)'
/usr/bin/ld: /tmp/ccFl5wbu.o: in function `testing::internal::FunctionMockerBase<bool (std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)>::AddNewExpectation(char const*, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::tuple<testing::Matcher<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, testing::Matcher<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&)':
mock.cpp:(.text._ZN7testing8internal18FunctionMockerBaseIFbNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_EE17AddNewExpectationEPKciRKS7_RKSt5tupleIJNS_7MatcherIS7_EESG_EE[_ZN7testing8internal18FunctionMockerBaseIFbNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_EE17AddNewExpectationEPKciRKS7_RKSt5tupleIJNS_7MatcherIS7_EESG_EE]+0xda): undefined reference to `testing::Expectation::Expectation(testing::internal::linked_ptr<testing::internal::ExpectationBase> const&)'
collect2: error: ld returned 1 exit status
Seems the tutorial you followed have forgotten to install gmock.so, so after building finished, you need to type sudo make install to install all the libraries file.
And the version of gtest you used is out of date, suggest to use this version:
https://github.com/google/googletest/archive/refs/tags/release-1.11.0.tar.gz
Other answers around your tutorial have also pointed out that, we need to install gmock libraries. Then the problem should be fixed.
I have correctly installed crfsuite from source (https://github.com/downloads/chokkan/crfsuite/crfsuite-0.12.tar.gz).
But when I try to compile a very simple code, it seems that I have missed something.
Here is the code:
#include "crfsuite.hpp"
using namespace CRFSuite;
int main(int argc, char *argv[])
{
Tagger tagger;
}
Here's the command line to compile:
g++ -L/usr/local/lib -I/usr/local/include -lcrfsuite tagging.cpp
and the error:
/tmp/ccIkvCFv.o: In function `CRFSuite::Trainer::Trainer()':
tagging.cpp:(.text+0x48): undefined reference to `crfsuite_data_init'
/tmp/ccIkvCFv.o: In function `CRFSuite::Trainer::init()':
tagging.cpp:(.text+0x149): undefined reference to `crfsuite_create_instance'
tagging.cpp:(.text+0x1b7): undefined reference to `crfsuite_create_instance'
/tmp/ccIkvCFv.o: In function `CRFSuite::Trainer::clear()':
tagging.cpp:(.text+0x2dd): undefined reference to `crfsuite_data_finish'
tagging.cpp:(.text+0x2ed): undefined reference to `crfsuite_data_init'
/tmp/ccIkvCFv.o: In function `CRFSuite::Trainer::append(std::vector<std::vector<CRFSuite::Attribute, std::allocator<CRFSuite::Attribute> >, std::allocator<std::vector<CRFSuite::Attribute, std::allocator<CRFSuite::Attribute> > > > const&, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&, int)':
tagging.cpp:(.text+0x495): undefined reference to `crfsuite_instance_init_n'
tagging.cpp:(.text+0x51f): undefined reference to `crfsuite_item_init_n'
tagging.cpp:(.text+0x69b): undefined reference to `crfsuite_data_append'
tagging.cpp:(.text+0x6aa): undefined reference to `crfsuite_instance_finish'
/tmp/ccIkvCFv.o: In function `CRFSuite::Trainer::select(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
tagging.cpp:(.text+0x7e9): undefined reference to `crfsuite_create_instance'
/tmp/ccIkvCFv.o: In function `CRFSuite::Tagger::open(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
tagging.cpp:(.text+0x11a0): undefined reference to `crfsuite_create_instance_from_file'
/tmp/ccIkvCFv.o: In function `CRFSuite::Tagger::set(std::vector<std::vector<CRFSuite::Attribute, std::allocator<CRFSuite::Attribute> >, std::allocator<std::vector<CRFSuite::Attribute, std::allocator<CRFSuite::Attribute> > > > const&)':
tagging.cpp:(.text+0x16c6): undefined reference to `crfsuite_instance_init_n'
tagging.cpp:(.text+0x1731): undefined reference to `crfsuite_item_init'
tagging.cpp:(.text+0x17e1): undefined reference to `crfsuite_attribute_set'
tagging.cpp:(.text+0x17f4): undefined reference to `crfsuite_item_append_attribute'
tagging.cpp:(.text+0x1854): undefined reference to `crfsuite_instance_finish'
tagging.cpp:(.text+0x18ac): undefined reference to `crfsuite_instance_finish'
collect2: error: ld returned 1 exit status
The paths are correct (/usr/local/lib, /usr/local/include)
The order of arguments matters. The library should go after your cpp file:
g++ -L/usr/local/lib -I/usr/local/include tagging.cpp -lcrfsuite
See the answer Why does the order in which libraries are linked sometimes cause errors in GCC? for more information.
I've just installed Poco on Ubuntu (by compiling from the code on the git master branch at release 1.9.0). And now I'm trying to compile my HelloWorld.cpp.
This is what I've tried:
g++ -L/usr/local/lib -lPocoFoundation -lPocoUtil -lPocoNet -lPocoNetd -lPocoData -lPocoXML ./helloworld.cpp
This is the helloworld.cpp content:
#include "Poco/Net/HTTPServer.h"
#include "Poco/Net/HTTPRequestHandler.h"
#include "Poco/Net/HTTPRequestHandlerFactory.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "Poco/Net/ServerSocket.h"
#include "Poco/Util/ServerApplication.h"
#include <iostream>
using namespace Poco;
using namespace Poco::Net;
using namespace Poco::Util;
class HelloRequestHandler: public HTTPRequestHandler
{
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
Application& app = Application::instance();
app.logger().information("Request from %s", request.clientAddress().toString());
response.setChunkedTransferEncoding(true);
response.setContentType("text/html");
response.send()
<< "<html>"
<< "<head><title>Hello</title></head>"
<< "<body><h1>Hello from the POCO Web Server</h1></body>"
<< "</html>";
}
};
class HelloRequestHandlerFactory: public HTTPRequestHandlerFactory
{
HTTPRequestHandler* createRequestHandler(const HTTPServerRequest&)
{
return new HelloRequestHandler;
}
};
class WebServerApp: public ServerApplication
{
void initialize(Application& self)
{
loadConfiguration();
ServerApplication::initialize(self);
}
int main(const std::vector<std::string>&)
{
UInt16 port = static_cast<UInt16>(config().getUInt("port", 8080));
HTTPServer srv(new HelloRequestHandlerFactory, port);
srv.start();
logger().information("HTTP Server started on port %hu.", port);
waitForTerminationRequest();
logger().information("Stopping HTTP Server...");
srv.stop();
return Application::EXIT_OK;
}
};
POCO_SERVER_MAIN(WebServerApp)
I expect by keeping adding the libraries I link in the g++ command line it should eventually allow me to compile the program.
But it seems no matter how many libraries I tried to link I'm still getting the following errors (without eliminating any of the errors with how many libraries I add on the way):
kennyyu#kennyyu-ubuntu:~/poco/myexample$ g++ -L/usr/local/lib -lPocoFoundation -lPocoUtil -lPocoNet -lPocoNetd -lPocoData -lPocoXML ./helloworld.cpp
/tmp/ccWTd1HS.o: In function `main':
helloworld.cpp:(.text+0x53): undefined reference to `Poco::Util::ServerApplication::run(int, char**)'
helloworld.cpp:(.text+0xd1): undefined reference to `Poco::Exception::displayText[abi:cxx11]() const'
/tmp/ccWTd1HS.o: In function `Poco::Net::Impl::IPv4SocketAddressImpl::host() const':
helloworld.cpp:(.text._ZNK4Poco3Net4Impl21IPv4SocketAddressImpl4hostEv[_ZNK4Poco3Net4Impl21IPv4SocketAddressImpl4hostEv]+0x28): undefined reference to `Poco::Net::IPAddress::IPAddress(void const*, unsigned int)'
/tmp/ccWTd1HS.o: In function `Poco::Net::Impl::IPv6SocketAddressImpl::host() const':
helloworld.cpp:(.text._ZNK4Poco3Net4Impl21IPv6SocketAddressImpl4hostEv[_ZNK4Poco3Net4Impl21IPv6SocketAddressImpl4hostEv]+0x2e): undefined reference to `Poco::Net::IPAddress::IPAddress(void const*, unsigned int, unsigned int)'
/tmp/ccWTd1HS.o: In function `Poco::ReferenceCounter::ReferenceCounter()':
helloworld.cpp:(.text._ZN4Poco16ReferenceCounterC2Ev[_ZN4Poco16ReferenceCounterC5Ev]+0x19): undefined reference to `Poco::AtomicCounter::AtomicCounter(int)'
/tmp/ccWTd1HS.o: In function `Poco::Logger::log(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Poco::Message::Priority)':
helloworld.cpp:(.text._ZN4Poco6Logger3logERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS_7Message8PriorityE[_ZN4Poco6Logger3logERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS_7Message8PriorityE]+0xa0): undefined reference to `Poco::Message::Message(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Poco::Message::Priority)'
helloworld.cpp:(.text._ZN4Poco6Logger3logERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS_7Message8PriorityE[_ZN4Poco6Logger3logERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS_7Message8PriorityE]+0xbe): undefined reference to `Poco::Message::~Message()'
helloworld.cpp:(.text._ZN4Poco6Logger3logERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS_7Message8PriorityE[_ZN4Poco6Logger3logERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEENS_7Message8PriorityE]+0xd2): undefined reference to `Poco::Message::~Message()'
/tmp/ccWTd1HS.o: In function `Poco::Logger::information(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Poco::Any const&)':
helloworld.cpp:(.text._ZN4Poco6Logger11informationERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_3AnyE[_ZN4Poco6Logger11informationERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_3AnyE]+0x37): undefined reference to `Poco::format(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Poco::Any const&)'
/tmp/ccWTd1HS.o: In function `Poco::Util::Application::logger() const':
helloworld.cpp:(.text._ZNK4Poco4Util11Application6loggerEv[_ZNK4Poco4Util11Application6loggerEv]+0x30): undefined reference to `Poco::Bugcheck::nullPointer(char const*, char const*, int)'
/tmp/ccWTd1HS.o: In function `Poco::Util::Application::instance()':
helloworld.cpp:(.text._ZN4Poco4Util11Application8instanceEv[_ZN4Poco4Util11Application8instanceEv]+0x7): undefined reference to `Poco::Util::Application::_pInstance'
helloworld.cpp:(.text._ZN4Poco4Util11Application8instanceEv[_ZN4Poco4Util11Application8instanceEv]+0x24): undefined reference to `Poco::Bugcheck::nullPointer(char const*, char const*, int)'
helloworld.cpp:(.text._ZN4Poco4Util11Application8instanceEv[_ZN4Poco4Util11Application8instanceEv]+0x2b): undefined reference to `Poco::Util::Application::_pInstance'
/tmp/ccWTd1HS.o: In function `HelloRequestHandler::handleRequest(Poco::Net::HTTPServerRequest&, Poco::Net::HTTPServerResponse&)':
helloworld.cpp:(.text._ZN19HelloRequestHandler13handleRequestERN4Poco3Net17HTTPServerRequestERNS1_18HTTPServerResponseE[_ZN19HelloRequestHandler13handleRequestERN4Poco3Net17HTTPServerRequestERNS1_18HTTPServerResponseE]+0x73): undefined reference to `Poco::Net::SocketAddress::toString[abi:cxx11]() const'
helloworld.cpp:(.text._ZN19HelloRequestHandler13handleRequestERN4Poco3Net17HTTPServerRequestERNS1_18HTTPServerResponseE[_ZN19HelloRequestHandler13handleRequestERN4Poco3Net17HTTPServerRequestERNS1_18HTTPServerResponseE]+0x100): undefined reference to `Poco::Net::HTTPMessage::setChunkedTransferEncoding(bool)'
helloworld.cpp:(.text._ZN19HelloRequestHandler13handleRequestERN4Poco3Net17HTTPServerRequestERNS1_18HTTPServerResponseE[_ZN19HelloRequestHandler13handleRequestERN4Poco3Net17HTTPServerRequestERNS1_18HTTPServerResponseE]+0x139): undefined reference to `Poco::Net::HTTPMessage::setContentType(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/ccWTd1HS.o: In function `HelloRequestHandler::HelloRequestHandler()':
helloworld.cpp:(.text._ZN19HelloRequestHandlerC2Ev[_ZN19HelloRequestHandlerC5Ev]+0x14): undefined reference to `Poco::Net::HTTPRequestHandler::HTTPRequestHandler()'
/tmp/ccWTd1HS.o: In function `WebServerApp::initialize(Poco::Util::Application&)':
helloworld.cpp:(.text._ZN12WebServerApp10initializeERN4Poco4Util11ApplicationE[_ZN12WebServerApp10initializeERN4Poco4Util11ApplicationE]+0x1d): undefined reference to `Poco::Util::Application::loadConfiguration(int)'
helloworld.cpp:(.text._ZN12WebServerApp10initializeERN4Poco4Util11ApplicationE[_ZN12WebServerApp10initializeERN4Poco4Util11ApplicationE]+0x30): undefined reference to `Poco::Util::Application::initialize(Poco::Util::Application&)'
/tmp/ccWTd1HS.o: In function `HelloRequestHandlerFactory::HelloRequestHandlerFactory()':
helloworld.cpp:(.text._ZN26HelloRequestHandlerFactoryC2Ev[_ZN26HelloRequestHandlerFactoryC5Ev]+0x14): undefined reference to `Poco::Net::HTTPRequestHandlerFactory::HTTPRequestHandlerFactory()'
/tmp/ccWTd1HS.o: In function `WebServerApp::main(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&)':
helloworld.cpp:(.text._ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE[_ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE]+0x7c): undefined reference to `Poco::Util::AbstractConfiguration::getUInt(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int) const'
helloworld.cpp:(.text._ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE[_ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE]+0xb6): undefined reference to `Poco::Net::HTTPServerParams::HTTPServerParams()'
helloworld.cpp:(.text._ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE[_ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE]+0x11d): undefined reference to `Poco::Net::HTTPServer::HTTPServer(Poco::SharedPtr<Poco::Net::HTTPRequestHandlerFactory, Poco::ReferenceCounter, Poco::ReleasePolicy<Poco::Net::HTTPRequestHandlerFactory> >, unsigned short, Poco::AutoPtr<Poco::Net::HTTPServerParams>)'
helloworld.cpp:(.text._ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE[_ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE]+0x14a): undefined reference to `Poco::Net::TCPServer::start()'
helloworld.cpp:(.text._ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE[_ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE]+0x1f6): undefined reference to `Poco::Util::ServerApplication::waitForTerminationRequest()'
helloworld.cpp:(.text._ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE[_ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE]+0x273): undefined reference to `Poco::Net::TCPServer::stop()'
helloworld.cpp:(.text._ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE[_ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE]+0x287): undefined reference to `Poco::Net::HTTPServer::~HTTPServer()'
helloworld.cpp:(.text._ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE[_ZN12WebServerApp4mainERKSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS6_EE]+0x3a4): undefined reference to `Poco::Net::HTTPServer::~HTTPServer()'
/tmp/ccWTd1HS.o: In function `WebServerApp::WebServerApp()':
helloworld.cpp:(.text._ZN12WebServerAppC2Ev[_ZN12WebServerAppC5Ev]+0x14): undefined reference to `Poco::Util::ServerApplication::ServerApplication()'
/tmp/ccWTd1HS.o: In function `Poco::ReferenceCounter::~ReferenceCounter()':
helloworld.cpp:(.text._ZN4Poco16ReferenceCounterD2Ev[_ZN4Poco16ReferenceCounterD5Ev]+0x14): undefined reference to `Poco::AtomicCounter::~AtomicCounter()'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTV12WebServerApp[_ZTV12WebServerApp]+0x20): undefined reference to `Poco::Util::Application::name() const'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTV12WebServerApp[_ZTV12WebServerApp]+0x30): undefined reference to `Poco::Util::Application::uninitialize()'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTV12WebServerApp[_ZTV12WebServerApp]+0x38): undefined reference to `Poco::Util::Application::reinitialize(Poco::Util::Application&)'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTV12WebServerApp[_ZTV12WebServerApp]+0x40): undefined reference to `Poco::Util::ServerApplication::defineOptions(Poco::Util::OptionSet&)'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTV12WebServerApp[_ZTV12WebServerApp]+0x48): undefined reference to `Poco::Util::ServerApplication::run()'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTV12WebServerApp[_ZTV12WebServerApp]+0x50): undefined reference to `Poco::Util::Application::handleOption(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/ccWTd1HS.o: In function `WebServerApp::~WebServerApp()':
helloworld.cpp:(.text._ZN12WebServerAppD2Ev[_ZN12WebServerAppD5Ev]+0x22): undefined reference to `Poco::Util::ServerApplication::~ServerApplication()'
/tmp/ccWTd1HS.o: In function `HelloRequestHandlerFactory::~HelloRequestHandlerFactory()':
helloworld.cpp:(.text._ZN26HelloRequestHandlerFactoryD2Ev[_ZN26HelloRequestHandlerFactoryD5Ev]+0x22): undefined reference to `Poco::Net::HTTPRequestHandlerFactory::~HTTPRequestHandlerFactory()'
/tmp/ccWTd1HS.o: In function `HelloRequestHandler::~HelloRequestHandler()':
helloworld.cpp:(.text._ZN19HelloRequestHandlerD2Ev[_ZN19HelloRequestHandlerD5Ev]+0x22): undefined reference to `Poco::Net::HTTPRequestHandler::~HTTPRequestHandler()'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTVN4Poco3Net4Impl21IPv6SocketAddressImplE[_ZTVN4Poco3Net4Impl21IPv6SocketAddressImplE]+0x50): undefined reference to `Poco::Net::Impl::IPv6SocketAddressImpl::toString[abi:cxx11]() const'
/tmp/ccWTd1HS.o: In function `Poco::Net::Impl::IPv6SocketAddressImpl::~IPv6SocketAddressImpl()':
helloworld.cpp:(.text._ZN4Poco3Net4Impl21IPv6SocketAddressImplD2Ev[_ZN4Poco3Net4Impl21IPv6SocketAddressImplD5Ev]+0x22): undefined reference to `Poco::Net::Impl::SocketAddressImpl::~SocketAddressImpl()'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTVN4Poco3Net4Impl21IPv4SocketAddressImplE[_ZTVN4Poco3Net4Impl21IPv4SocketAddressImplE]+0x50): undefined reference to `Poco::Net::Impl::IPv4SocketAddressImpl::toString[abi:cxx11]() const'
/tmp/ccWTd1HS.o: In function `Poco::Net::Impl::IPv4SocketAddressImpl::~IPv4SocketAddressImpl()':
helloworld.cpp:(.text._ZN4Poco3Net4Impl21IPv4SocketAddressImplD2Ev[_ZN4Poco3Net4Impl21IPv4SocketAddressImplD5Ev]+0x22): undefined reference to `Poco::Net::Impl::SocketAddressImpl::~SocketAddressImpl()'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTI12WebServerApp[_ZTI12WebServerApp]+0x10): undefined reference to `typeinfo for Poco::Util::ServerApplication'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTI26HelloRequestHandlerFactory[_ZTI26HelloRequestHandlerFactory]+0x10): undefined reference to `typeinfo for Poco::Net::HTTPRequestHandlerFactory'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTI19HelloRequestHandler[_ZTI19HelloRequestHandler]+0x10): undefined reference to `typeinfo for Poco::Net::HTTPRequestHandler'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTIN4Poco3Net4Impl21IPv6SocketAddressImplE[_ZTIN4Poco3Net4Impl21IPv6SocketAddressImplE]+0x10): undefined reference to `typeinfo for Poco::Net::Impl::SocketAddressImpl'
/tmp/ccWTd1HS.o:(.data.rel.ro._ZTIN4Poco3Net4Impl21IPv4SocketAddressImplE[_ZTIN4Poco3Net4Impl21IPv4SocketAddressImplE]+0x10): undefined reference to `typeinfo for Poco::Net::Impl::SocketAddressImpl'
/tmp/ccWTd1HS.o:(.data.rel.local.DW.ref._ZTIN4Poco9ExceptionE[DW.ref._ZTIN4Poco9ExceptionE]+0x0): undefined reference to `typeinfo for Poco::Exception'
collect2: error: ld returned 1 exit status
kennyyu#kennyyu-ubuntu:~/poco/myexample$
kennyyu#kennyyu-ubuntu:~/poco/myexample$ ls /usr/local/lib/libPoco*.so
/usr/local/lib/libPocoCppParserd.so /usr/local/lib/libPocoDataMySQL.so /usr/local/lib/libPocoDataSQLited.so /usr/local/lib/libPocoFoundationd.so /usr/local/lib/libPocoMongoDBd.so /usr/local/lib/libPocoPDFd.so /usr/local/lib/libPocoUtild.so /usr/local/lib/libPocoZipd.so
/usr/local/lib/libPocoCppParser.so /usr/local/lib/libPocoDataODBCd.so /usr/local/lib/libPocoDataSQLite.so /usr/local/lib/libPocoFoundation.so /usr/local/lib/libPocoMongoDB.so /usr/local/lib/libPocoPDF.so /usr/local/lib/libPocoUtil.so /usr/local/lib/libPocoZip.so
/usr/local/lib/libPocoDatad.so /usr/local/lib/libPocoDataODBC.so /usr/local/lib/libPocoEncodingsd.so /usr/local/lib/libPocoJSONd.so /usr/local/lib/libPocoNetd.so /usr/local/lib/libPocoRedisd.so /usr/local/lib/libPocoXMLd.so
/usr/local/lib/libPocoDataMySQLd.so /usr/local/lib/libPocoData.so /usr/local/lib/libPocoEncodings.so /usr/local/lib/libPocoJSON.so /usr/local/lib/libPocoNet.so /usr/local/lib/libPocoRedis.so /usr/local/lib/libPocoXML.so
kennyyu#kennyyu-ubuntu:~/poco/myexample$
Can someone please shed me some light?
This worked for me in Ubuntu 19.10.
sudo apt install libpoco-dev
(All of the Poco libraries get installed with that.)
Create helloworld.cpp with contents from OP.
Compile with:
g++ helloworld.cpp -o helloworld.o -lPocoFoundation -lPocoNet -lPocoUtil
If that still doesn't work, you can include the location where the Poco .h files are with the -I option, and where the Poco .so files are with the -L option.
Find where the Poco .so files are located:
sudo find / -name "libPoco*.so" 2>/dev/null
/usr/lib/x86_64-linux-gnu/libPocoFoundation.so
...
sudo find / -name "ServerSocket.h" 2>/dev/null
/usr/include/Poco/Net/ServerSocket.h
The compile statement then becomes:
g++ -I/usr/include/Poco/ helloworld.cpp -o helloworld.o -L/usr/lib/x86_64-linux-gnu/ -lPocoFoundation -lPocoNet -lPocoUtil
recently I start study unit testing , and I want test my program with gtest. I install all with this order :
$ git clone https://github.com/google/googletest
$ cd googletest
$ cmake -DBUILD_SHARED_LIBS=ON .
$ make
$ cd googlemock
$ sudo cp ./libgmock_main.so ./gtest/libgtest.so gtest/libgtest_main.so ./libgmock.so /usr/lib/
$ sudo ldconfig
and now write code :
#include "gtest/gtest.h"
class Add
{
private:
int element;
public:
Add():element(0){}
~Add(){}
void setElement(int e){ element = e; }
int getElement() { return element; }
int adder(int e) { return element += e; }
};
class AddTest : public ::testing::Test
{
protected:
int abc(int a){
return a;
}
virtual void SetUp(){
add1.setElement(1);
add2.setElement(20);
}
virtual void TearDown(){}
Add add1;
Add add2;
};
TEST_F(AddTest, getTest)
{
EXPECT_EQ(1, add1.getElement());
EXPECT_EQ(20, add2.getElement());
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
end when I run test i get this error :
CMakeFiles/mock2.dir/main.cpp.o: In function AddTest_getTest_Test::TestBody()':
/home/artem/CLionProjects/mock2/main.cpp:33: undefined reference totesting::Message::Message()'
/home/artem/CLionProjects/mock2/main.cpp:33: undefined reference to testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
/home/artem/CLionProjects/mock2/main.cpp:33: undefined reference totesting::internal::AssertHelper::operator=(testing::Message const&) const'
/home/artem/CLionProjects/mock2/main.cpp:33: undefined reference to testing::internal::AssertHelper::~AssertHelper()'
/home/artem/CLionProjects/mock2/main.cpp:34: undefined reference totesting::Message::Message()'
/home/artem/CLionProjects/mock2/main.cpp:34: undefined reference to testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
/home/artem/CLionProjects/mock2/main.cpp:34: undefined reference totesting::internal::AssertHelper::operator=(testing::Message const&) const'
/home/artem/CLionProjects/mock2/main.cpp:34: undefined reference to testing::internal::AssertHelper::~AssertHelper()'
/home/artem/CLionProjects/mock2/main.cpp:33: undefined reference totesting::internal::AssertHelper::~AssertHelper()'
/home/artem/CLionProjects/mock2/main.cpp:34: undefined reference to testing::internal::AssertHelper::~AssertHelper()'
CMakeFiles/mock2.dir/main.cpp.o: In functionmain':
/home/artem/CLionProjects/mock2/main.cpp:39: undefined reference to testing::InitGoogleTest(int*, char**)'
CMakeFiles/mock2.dir/main.cpp.o: In function__static_initialization_and_destruction_0(int, int)':
/home/artem/CLionProjects/mock2/main.cpp:31: undefined reference to testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)'
CMakeFiles/mock2.dir/main.cpp.o: In functionRUN_ALL_TESTS()':
/usr/local/include/gtest/gtest.h:2235: undefined reference to testing::UnitTest::GetInstance()'
/usr/local/include/gtest/gtest.h:2235: undefined reference totesting::UnitTest::Run()'
CMakeFiles/mock2.dir/main.cpp.o: In function AddTest::AddTest()':
/home/artem/CLionProjects/mock2/main.cpp:15: undefined reference totesting::Test::Test()'
CMakeFiles/mock2.dir/main.cpp.o: In function AddTest::~AddTest()':
/home/artem/CLionProjects/mock2/main.cpp:15: undefined reference totesting::Test::~Test()'
CMakeFiles/mock2.dir/main.cpp.o: In function testing::internal::scoped_ptr<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::reset(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)':
/usr/local/include/gtest/internal/gtest-port.h:1172: undefined reference totesting::internal::IsTrue(bool)'
CMakeFiles/mock2.dir/main.cpp.o: In function testing::internal::scoped_ptr<std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> > >::reset(std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >*)':
/usr/local/include/gtest/internal/gtest-port.h:1172: undefined reference totesting::internal::IsTrue(bool)'
CMakeFiles/mock2.dir/main.cpp.o: In function testing::AssertionResult testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int const&, int const&)':
/usr/local/include/gtest/gtest.h:1394: undefined reference totesting::AssertionSuccess()'
CMakeFiles/mock2.dir/main.cpp.o: In function testing::AssertionResult testing::internal::CmpHelperEQFailure<int, int>(char const*, char const*, int const&, int const&)':
/usr/local/include/gtest/gtest.h:1384: undefined reference totesting::internal::EqFailure(char const*, char const*, std::__cxx11::basic_string, std::allocator > const&, std::__cxx11::basic_string, std::allocator > const&, bool)'
CMakeFiles/mock2.dir/main.cpp.o:(.rodata._ZTI7AddTest[_ZTI7AddTest]+0x10): undefined reference to `typeinfo for testing::Test'
collect2: error: ld returned 1 exit status
CMakeFiles/mock2.dir/build.make:94: recipe for target 'mock2' failed
make[3]: * [mock2] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/mock2.dir/all' failed
make[2]: [CMakeFiles/mock2.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/mock2.dir/rule' failed
make[1]: [CMakeFiles/mock2.dir/rule] Error 2
Makefile:118: recipe for target 'mock2' failed
make: * [mock2] Error 2
but when use command
g++ main.cpp -o test -lgtest -lpthread
everytheng is good. How can I fix it and run it not in command line ?
If you are using CLion then you may have a CMakeLists.txt file you should add rules to link to the libraries. To do this add the following line to your CMakeLists.txt
enable_testing()
find_package(GTest REQUIRED) # Find the google testing framework on your system
include_directories(${GTEST_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${GTEST_LIBRARIES}) # Replace ${PROJECT_NAME} with your target name
For more details go here.