googlemock giving error while building a program with mock - c++

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.

Related

getting "undefined reference to `check' "error on Windows

I try to connect a DB to C++ with the Connector C++ 8.0.
When I compile a really simple code :
#include <iostream>
#include <jdbc.h>
int main(){
sql::Driver *driver;
driver = get_driver_instance();
return 0;
}
I get this error:
c:/program files (x86)/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\newva\AppData\Local\Temp\ccjFB5x8.o:Main.cpp:(.text+0x1f): undefined reference to `check(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
c:/program files (x86)/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\newva\AppData\Local\Temp\ccjFB5x8.o:Main.cpp:(.text+0x53): undefined reference to `check(std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<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&)'
collect2.exe: error: ld returned 1 exit status
When I comment the "driver = get_driver_instance();" line, the code compile.
I compile with a Makefile :
MYSQL_CONCPP_DIR = "C:\Program Files\MySQL\Connector C++ 8.0"
CPPFLAGS=-I $(MYSQL_CONCPP_DIR)\include\mysql -L $(MYSQL_CONCPP_DIR)\lib64\vs14
LDLIBS=-lmysqlcppconn
CXXFLAGS=-std=c++20
result.exe:Main.cpp
g++ Main.cpp ${CXXFLAGS} ${CPPFLAGS} ${LDLIBS} -o result.exe
I am on Windows 10.
Connector C++ has 2 APIs.
JDBC (the one you are using)
X DevAPI
You are linking with mysqlcppconn8 which is DevAPI. You should link with mysqlcppconn.
Both are bundled on the same package.

Using mysql connector-c++ to connect mysql server ,a little demo,but stucked

My server is Ubuntu,code is:
#include <mysqlx/xdevapi.h>
#include <iostream>
using namespace mysqlx;
int main()
{
const char *url = "mysqlx://root#127.0.0.1";
std::cout << "Create session on " << url <<std::endl;
Session sess(url);
Schema sch = sess.getSchema("IM.User");
}
when I compile it,got this:
g++ dbtest.cpp -g -std=c++11 -L/usr/lib64/ -lmysqlcppconn8-static -I/usr/include/mysqlx -lssl -lpthread -lcrypto -o dbtest.o
----------------------------------------------------------------------
/tmp/cccFQIzq.o: In function `mysqlx::string::string(char const*)':
/usr/include/mysqlx/devapi/common.h:100: undefined reference to `mysqlx::string::Impl::from_utf8(mysqlx::string&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/tmp/cccFQIzq.o: In function `mysqlx::string::operator std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >() const':
/usr/include/mysqlx/devapi/common.h:115: undefined reference to `mysqlx::string::Impl::to_utf8[abi:cxx11](mysqlx::string const&)'
/tmp/cccFQIzq.o: In function `mysqlx::SessionSettings::SessionSettings(mysqlx::string const&)':
/usr/include/mysqlx/devapi/settings.h:339: undefined reference to `mysqlx::common::Settings_impl::set_from_uri(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: error: ld returned 1 exit status
Am I lost something?

gcc won't compile C++ code

My code won't compile and I don't understand the cryptic output from the compiler. Can you spot the error?
main.cpp
#include <vector>
#include <string>
int main() {
std::vector <std::string> myVar;
return 0;
}
Then in bash:
$ gcc main.cpp
/tmp/ccvmVBRq.o: In function `__gnu_cxx::new_allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::deallocate(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, unsigned long)':
main.cpp:(.text._ZN9__gnu_cxx13new_allocatorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE10deallocateEPS6_m[_ZN9__gnu_cxx13new_allocatorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE10deallocateEPS6_m]+0x1c): undefined reference to `operator delete(void*)'
/tmp/ccvmVBRq.o: In function `void std::_Destroy<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)':
main.cpp:(.text._ZSt8_DestroyINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEvPT_[_ZSt8_DestroyINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEvPT_]+0x14): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/tmp/ccvmVBRq.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
This is C++ code, so you have to compile it with g++. Try:
$ g++ main.cpp

Running gtest with a command line gives errors

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?

Boost linking fail

I have the following code which i m trying to compile:
#include <boost/filesystem/convenience.hpp>
#include <boost/foreach.hpp>
#include <boost/range.hpp>
#include <iostream>
int main(int, char**)
{
namespace bf = boost::filesystem;
BOOST_FOREACH(bf::path path,
boost::make_iterator_range(
bf::recursive_directory_iterator(bf::path("/home")),
bf::recursive_directory_iterator())) {
std::cout << path.string() << std::endl;
}
return 0;
}
My boost library is in /home/foo/include . and the include files are actually there.
when i run the following:
g++ -I/home/foo/include/ test.cc
I get the following error. how can i resolve this. what should i follow?
/tmp/ccvDmFNL.o(.text+0x502): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `boost::system::get_system_category()'
/tmp/ccvDmFNL.o(.text+0x51b): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `boost::system::get_generic_category()'
/tmp/ccvDmFNL.o(.text+0x534): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `boost::system::get_generic_category()'
/tmp/ccvDmFNL.o(.text+0x54d): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `boost::system::get_generic_category()'
/tmp/ccvDmFNL.o(.text+0x566): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `boost::system::get_system_category()'
/tmp/ccvDmFNL.o(.gnu.linkonce.t._ZN5boost10filesystem24basic_directory_iteratorINS0_10basic_pathISsNS0_11path_traitsEEEE6m_initERKS4_+0x2e): In function `boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >::m_init(boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> const&)':
: undefined reference to `boost::filesystem::detail::not_found_error()'
/tmp/ccvDmFNL.o(.gnu.linkonce.t._ZN5boost10filesystem24basic_directory_iteratorINS0_10basic_pathISsNS0_11path_traitsEEEE6m_initERKS4_+0xbe): In function `boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >::m_init(boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> const&)':
: undefined reference to `boost::filesystem::detail::dir_itr_first(void*&, void*&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, boost::filesystem::file_status&, boost::filesystem::file_status&)'
/tmp/ccvDmFNL.o(.gnu.linkonce.t._ZN5boost6system10error_codeC1Ev+0x14): In function `boost::system::error_code::error_code()':
: undefined reference to `boost::system::get_system_category()'
/tmp/ccvDmFNL.o(.gnu.linkonce.t._ZN5boost10filesystem24basic_directory_iteratorINS0_10basic_pathISsNS0_11path_traitsEEEE9incrementEv+0xde): In function `boost::filesystem::basic_directory_iterator<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >::increment()':
: undefined reference to `boost::filesystem::detail::dir_itr_increment(void*&, void*&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, boost::filesystem::file_status&, boost::filesystem::file_status&)'
/tmp/ccvDmFNL.o(.gnu.linkonce.t._ZN5boost10filesystem6statusINS0_10basic_pathISsNS0_11path_traitsEEEEENS_9enable_ifINS0_13is_basic_pathIT_EENS0_11file_statusEE4typeERKS7_+0x34): In function `boost::enable_if<boost::filesystem::is_basic_path<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >, boost::filesystem::file_status>::type boost::filesystem::status<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >(boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> const&)':
: undefined reference to `boost::filesystem::detail::status_api(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, boost::system::error_code&)'
/tmp/ccvDmFNL.o(.gnu.linkonce.t._ZN5boost10filesystem6detail11dir_itr_impINS0_10basic_pathISsNS0_11path_traitsEEEED1Ev+0x1d): In function `boost::filesystem::detail::dir_itr_imp<boost::filesystem::basic_path<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::filesystem::path_traits> >::~dir_itr_imp()':
: undefined reference to `boost::filesystem::detail::dir_itr_close(void*&, void*&)'
collect2: ld returned 1 exit status
EDIT:
Now i tried:
g++ -I/home/foo/include/ test.cc -lboost_system -lboost_filesystem
and get the following error:
/usr/bin/ld: cannot find -lboost_system
collect2: ld returned 1 exit status
I have the libboost_system-gcc34-1_38.so within
/home/foo/lib
how can I point to that?
from gcc man page:
-Ldir Add directory dir to the list of directories to be searched for -l.
So, is it that you are missing -L/home/foo/lib to the command line?
Your code compiled properly on my linux machine (Ubuntu 10.04, boost-filesystem 1.40) with the following command:
g++ test.cpp -lboost_filesystem
or
g++ test.cpp -lboost_system -lboost_filesystem
It gave me compile errors with g++ test.cpp -lboost_system
You must tell the compiler/linker where your libraries are as well, if they are not in the default location. For this you must use the -L flag to the compiler:
g++ -I/home/y/include/ test.cc -L/home/foo/lib -lboost_system -lboost_filesystem
Add -lboost_system (-lboost_system-mt if you're going to be threading) and -lboost_filesystem (-lboost_filesystem-mt if you're going to be threading) to the cmdline, before any input files.
Your boost libraries appear to be decorated with the suffix gcc34-1_38. Are you using gcc 3.4? Is your boost library on the library path? If not you may need to add the the path to your boost libraries using the -L flag to g++ or you can add the path to the LD_LIBRARY_PATH environment variable. In any case, you can link to your boost libraries by using -lboost_system-gcc34-1_38 and -lboost_filesystem-gcc34-1_38.