I have some "simple" code in C++ to connect to a MySQL database (which is localhost, btw).
#include <stdlib.h>
#include <iostream>
#include "mysql/include/mysql/jdbc.h"
using namespace sql;
int main() {
std::cout << "Test"<< std::endl;
try {
sql::Driver *myDriver;
sql::Connection *myConn;
sql::Statement *myStmt;
sql::ResultSet *myRes;
myDriver = sql::mysql::get_mysql_driver_instance();
myConn = myDriver ->connect("tcp://127.0.0.1", "root", "");
myConn->setSchema("root");
myStmt = myConn->createStatement();
myRes = myStmt->executeQuery("SELECT 'Hello World' AS _message");
while (myRes->next()) {
std::cout << myRes->getString("_message") << std::endl;
}
delete myRes;
delete myStmt;
delete myConn;
} catch (sql::SQLException &e) {
std::cout << "Filed connect to Database" << std::endl;
std::cout << "Error: " << e.what() << std::endl;
std::cout << "Error code: " << e.getErrorCode() << std::endl;
}
}
The file jdbc.h is just a lot of different #include for header files.
I use 2 commands to compile:
g++ -c -Wall -I/usr/include/cppconn connect.cpp -o connect.o and
g++ -lm -L/usr/lib/x86_64-linux-gnu -lmysqlcppconn connect.o -o connect
Which produced this linking error:
/usr/bin/ld: connect.o: in function "check_lib()':
connect.cpp:(.text+0x2c): undefined reference to `check(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)"
/usr/bin/ld: connect.cpp:(.text+0x77): 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&)"
/usr/bin/ld: connect.o: in function `sql::mysql::get_driver_instance_by_name(char const*)':
connect.cpp:(.text+0xfa): undefined reference to "sql::mysql::_get_driver_instance_by_name(char const*)"
collect2: error: ld returned 1 exit status
Which I kinda resolved using this answer: https://stackoverflow.com/a/33395489/14814564 , essentially by putting #define _GLIBCXX_USE_CXX11_ABI 0 at the top of the file, which after compilation, produces this linking error:
/usr/bin/ld: connect.o: in function "check_lib()':
connect.cpp:(.text+0x2c): undefined reference to "check(std::string const&)'
/usr/bin/ld: connect.cpp:(.text+0x77): undefined reference to "check(std::map<std::string, std::string, std::less<std::string>, std::allocator<std::pair<std::string const, std::string> > > const&)'
/usr/bin/ld: connect.o: in function `sql::mysql::get_driver_instance_by_name(char const*)':
connect.cpp:(.text+0xfa): undefined reference to "sql::mysql::_get_driver_instance_by_name(char const*)'
collect2: error: ld returned 1 exit status
Why does this linking error keep popping up and how do I resolve it?
I had the same problem, I solved it by building a Makefile`
MYSQL_CONCPP_DIR = ./mysql
CPPFLAGS=-I $(MYSQL_CONCPP_DIR)/include/mysql -L $(MYSQL_CONCPP_DIR)/lib64
LDLIBS=-lmysqlcppconn
CXXFLAGS=-std=c++11
out.exe:test.cpp
g++ test.cpp ${LDLIBS} ${CXXFLAGS} ${CPPFLAGS} -o out.exe
You have there
#include "mysql/include/mysql/jdbc.h"
In my case include needs to be done like this`
#include <jdbc.h>
Related
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.
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?
I’m trying to compile a program, which uses boost libraries on Ubuntu and I'm getting error:
stepan#stepan-System-Product-Name:~/dev$ g++ -std=c++11 -Wall -pedantic -g -O0 -DBOOST_LOG_DYN_LINK -c test.cpp
stepan#stepan-System-Product-Name:~/dev$ g++ test.o -lboost_log -lboost_thread -lpthread -lboost_system -o test
test.o: In function `boost::log::v2_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >& boost::log::v2_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >::formatted_write<wchar_t>(wchar_t const*, long)':
/usr/include/boost/log/utility/formatting_ostream.hpp:575: undefined reference to `boost::log::v2_mt_posix::aux::code_convert(wchar_t const*, unsigned long, std::string&, std::locale const&)'
test.o: In function `void boost::log::v2_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >::aligned_write<wchar_t>(wchar_t const*, long)':
/usr/include/boost/log/utility/formatting_ostream.hpp:696: undefined reference to `boost::log::v2_mt_posix::aux::code_convert(wchar_t const*, unsigned long, std::string&, std::locale const&)'
/usr/include/boost/log/utility/formatting_ostream.hpp:702: undefined reference to `boost::log::v2_mt_posix::aux::code_convert(wchar_t const*, unsigned long, std::string&, std::locale const&)'
test.o: In function `void boost::log::v2_mt_posix::sinks::basic_formatting_sink_frontend<char>::feed_record<boost::recursive_mutex, boost::log::v2_mt_posix::sinks::text_file_backend>(boost::log::v2_mt_posix::record_view const&, boost::recursive_mutex&, boost::log::v2_mt_posix::sinks::text_file_backend&)':
/usr/include/boost/log/sinks/basic_sink_frontend.hpp:445: undefined reference to `boost::log::v2_mt_posix::sinks::text_file_backend::consume(boost::log::v2_mt_posix::record_view const&, std::string const&)'
test.o: In function `void boost::log::v2_mt_posix::sinks::basic_formatting_sink_frontend<char>::feed_record<boost::log::v2_mt_posix::aux::fake_mutex, boost::log::v2_mt_posix::sinks::text_file_backend>(boost::log::v2_mt_posix::record_view const&, boost::log::v2_mt_posix::aux::fake_mutex&, boost::log::v2_mt_posix::sinks::text_file_backend&)':
/usr/include/boost/log/sinks/basic_sink_frontend.hpp:445: undefined reference to `boost::log::v2_mt_posix::sinks::text_file_backend::consume(boost::log::v2_mt_posix::record_view const&, std::string const&)'
collect2: error: ld returned 1 exit status
stepan#stepan-System-Product-Name:~/dev$ g++ -DBOOST_LOG_DYN_LINK test.cpp -Wall -L$BOOST/lib/ -I $BOOST/include/ -pthread -lboost_system -lboost_log_setup -lboost_log -lboost_date_time -lboost_thread -lrt -lboost_filesystem
/tmp/cczxT0rE.o: In function `boost::log::v2_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >& boost::log::v2_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >::formatted_write<wchar_t>(wchar_t const*, long)':
test.cpp:(.text._ZN5boost3log11v2_mt_posix24basic_formatting_ostreamIcSt11char_traitsIcESaIcEE15formatted_writeIwEERS6_PKT_l[_ZN5boost3log11v2_mt_posix24basic_formatting_ostreamIcSt11char_traitsIcESaIcEE15formatted_writeIwEERS6_PKT_l]+0xb2): undefined reference to `boost::log::v2_mt_posix::aux::code_convert(wchar_t const*, unsigned long, std::string&, std::locale const&)'
/tmp/cczxT0rE.o: In function `void boost::log::v2_mt_posix::basic_formatting_ostream<char, std::char_traits<char>, std::allocator<char> >::aligned_write<wchar_t>(wchar_t const*, long)':
test.cpp:(.text._ZN5boost3log11v2_mt_posix24basic_formatting_ostreamIcSt11char_traitsIcESaIcEE13aligned_writeIwEEvPKT_l[_ZN5boost3log11v2_mt_posix24basic_formatting_ostreamIcSt11char_traitsIcESaIcEE13aligned_writeIwEEvPKT_l]+0xa2): undefined reference to `boost::log::v2_mt_posix::aux::code_convert(wchar_t const*, unsigned long, std::string&, std::locale const&)'
test.cpp:(.text._ZN5boost3log11v2_mt_posix24basic_formatting_ostreamIcSt11char_traitsIcESaIcEE13aligned_writeIwEEvPKT_l[_ZN5boost3log11v2_mt_posix24basic_formatting_ostreamIcSt11char_traitsIcESaIcEE13aligned_writeIwEEvPKT_l]+0x12e): undefined reference to `boost::log::v2_mt_posix::aux::code_convert(wchar_t const*, unsigned long, std::string&, std::locale const&)'
/tmp/cczxT0rE.o: In function `void boost::log::v2_mt_posix::sinks::basic_formatting_sink_frontend<char>::feed_record<boost::recursive_mutex, boost::log::v2_mt_posix::sinks::text_file_backend>(boost::log::v2_mt_posix::record_view const&, boost::recursive_mutex&, boost::log::v2_mt_posix::sinks::text_file_backend&)':
test.cpp:(.text._ZN5boost3log11v2_mt_posix5sinks30basic_formatting_sink_frontendIcE11feed_recordINS_15recursive_mutexENS2_17text_file_backendEEEvRKNS1_11record_viewERT_RT0_[_ZN5boost3log11v2_mt_posix5sinks30basic_formatting_sink_frontendIcE11feed_recordINS_15recursive_mutexENS2_17text_file_backendEEEvRKNS1_11record_viewERT_RT0_]+0x16b): undefined reference to `boost::log::v2_mt_posix::sinks::text_file_backend::consume(boost::log::v2_mt_posix::record_view const&, std::string const&)'
/tmp/cczxT0rE.o: In function `void boost::log::v2_mt_posix::sinks::basic_formatting_sink_frontend<char>::feed_record<boost::log::v2_mt_posix::aux::fake_mutex, boost::log::v2_mt_posix::sinks::text_file_backend>(boost::log::v2_mt_posix::record_view const&, boost::log::v2_mt_posix::aux::fake_mutex&, boost::log::v2_mt_posix::sinks::text_file_backend&)':
test.cpp:(.text._ZN5boost3log11v2_mt_posix5sinks30basic_formatting_sink_frontendIcE11feed_recordINS1_3aux10fake_mutexENS2_17text_file_backendEEEvRKNS1_11record_viewERT_RT0_[_ZN5boost3log11v2_mt_posix5sinks30basic_formatting_sink_frontendIcE11feed_recordINS1_3aux10fake_mutexENS2_17text_file_backendEEEvRKNS1_11record_viewERT_RT0_]+0x16b): undefined reference to `boost::log::v2_mt_posix::sinks::text_file_backend::consume(boost::log::v2_mt_posix::record_view const&, std::string const&)'
collect2: error: ld returned 1 exit status
Code of test.cpp:
/*
* Copyright Andrey Semashev 2007 - 2013.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
#define BOOST_LOG_DYN_LINK 1
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace keywords = boost::log::keywords;
//[ example_tutorial_formatters_stream
void init()
{
logging::add_file_log
(
keywords::file_name = "sample_%N.log",
// This makes the sink to write log records that look like this:
// 1: <normal> A normal severity message
// 2: <error> An error severity message
keywords::format =
(
expr::stream
<< expr::attr< unsigned int >("LineID")
<< ": <" << logging::trivial::severity
<< "> " << expr::smessage
)
);
}
//]
#if 0
//[ example_tutorial_formatters_stream_date_time
void init()
{
logging::add_file_log
(
keywords::file_name = "sample_%N.log",
// This makes the sink to write log records that look like this:
// YYYY-MM-DD HH:MI:SS: <normal> A normal severity message
// YYYY-MM-DD HH:MI:SS: <error> An error severity message
keywords::format =
(
expr::stream
<< expr::format_date_time< boost::posix_time::ptime >("TimeStamp", "%Y-%m-%d %H:%M:%S")
<< ": <" << logging::trivial::severity
<< "> " << expr::smessage
)
);
}
//]
#endif
int main(int, char*[])
{
init();
logging::add_common_attributes();
using namespace logging::trivial;
src::severity_logger< severity_level > lg;
BOOST_LOG_SEV(lg, trace) << "A trace severity message";
BOOST_LOG_SEV(lg, debug) << "A debug severity message";
BOOST_LOG_SEV(lg, info) << "An informational severity message";
BOOST_LOG_SEV(lg, warning) << "A warning severity message";
BOOST_LOG_SEV(lg, error) << "An error severity message";
BOOST_LOG_SEV(lg, fatal) << "A fatal severity message";
return 0;
}
I tried this:
g++ -std=c++11 -Wall -pedantic -g -O0 -DBOOST_LOG_DYN_LINK -c test.cpp
g++ test.o -lboost_log -lboost_thread -lpthread -lboost_system -lboost_log_setup -o test
And this:
g++ -DBOOST_LOG_DYN_LINK test.cpp -Wall -L$BOOST/lib/ -I $BOOST/include/ -pthread -lboost_system -lboost_log_setup -lboost_log -lboost_date_time -lboost_thread -lrt -lboost_filesystem
From responses to these questions:
linker error while linking boost log tutorial (undefined references)
Boost logger linking issue
Boost.Log linking errors under GNU/Linux
Without success - the same error during linking and error message looks a bit different than in the questions above
Therefore, I assume that the root cause of my error is different from all mentioned above
Installed boost version 1.58
After some digging, I found a clue:
It looks like some files are left even after uninstalling of previous boost version 1.58 in /usr/lib/x86_64-linux-gnu
The issue was resolved after I deleted manually all libboost* files with exception for *.so.1.58.0 (see description below why) from /usr/lib/x86_64-linux-gnu left from the previous installation (my currently installed boost v 1.64 is in /usr/local):
Be aware that deleting of *. so. 1.58.0 files can make some apps not working (Firefox and chrome in my case), so I had to build boost v 1.58 and place them back to make them operational again
I'd like to use Apache log4cxx and am running into a problem. I'm running on Debian 8 and installed log4cxx using
apt-get install liblog4cxx10-dev
I have embedded one of the code snippets in the Documentation Page into the code below:
#include <log4cxx/logger.h>
#include <math.h>
int main(int argc, char* argv[]) {
log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("com.foo"));
int i = 10;
const char* region = "World";
LOG4CXX_INFO(logger, "Simple message text.")
LOG4CXX_INFO(logger, "Hello, " << region)
LOG4CXX_DEBUG(logger, L"Iteration " << i)
LOG4CXX_DEBUG(logger, "e^10 = " << std::scientific << exp(10.0))
LOG4CXX_WARN(logger, L"" << i << L" is the number of the iteration.")
return 0;
}
When compiling with Eclipse CDT (gcc 5.3.0 built from source) I get the following linker errors:
make all
Building file: ../src/tester.cxx
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/tester.d" -MT"src/tester.o" -o "src/tester.o" "../src/tester.cxx"
Finished building: ../src/tester.cxx
Building target: logtest
Invoking: GCC C++ Linker
g++ -L/usr/lib/x86_64-linux-gnu -o "logtest" ./src/tester.o -llog4cxx
./src/tester.o: In function `main':
/home/andand/workspace/logtest/Debug/../src/tester.cxx:9: undefined reference to `log4cxx::helpers::MessageBuffer::str[abi:cxx11](log4cxx::helpers::CharMessageBuffer&)'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:9: undefined reference to `log4cxx::Logger::forcedLog(log4cxx::helpers::ObjectPtrT<log4cxx::Level> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, log4cxx::spi::LocationInfo const&) const'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:10: undefined reference to `log4cxx::helpers::MessageBuffer::str[abi:cxx11](log4cxx::helpers::CharMessageBuffer&)'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:10: undefined reference to `log4cxx::Logger::forcedLog(log4cxx::helpers::ObjectPtrT<log4cxx::Level> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, log4cxx::spi::LocationInfo const&) const'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:11: undefined reference to `log4cxx::helpers::MessageBuffer::str[abi:cxx11](std::basic_ostream<wchar_t, std::char_traits<wchar_t> >&)'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:11: undefined reference to `log4cxx::Logger::forcedLog(log4cxx::helpers::ObjectPtrT<log4cxx::Level> const&, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&, log4cxx::spi::LocationInfo const&) const'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:12: undefined reference to `log4cxx::helpers::MessageBuffer::str[abi:cxx11](std::ostream&)'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:12: undefined reference to `log4cxx::Logger::forcedLog(log4cxx::helpers::ObjectPtrT<log4cxx::Level> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, log4cxx::spi::LocationInfo const&) const'
/home/andand/workspace/logtest/Debug/../src/tester.cxx:13: undefined reference to `log4cxx::helpers::MessageBuffer::str[abi:cxx11](std::basic_ostream<wchar_t, std::char_traits<wchar_t> >&)'
makefile:45: recipe for target 'logtest' failed
/home/andand/workspace/logtest/Debug/../src/tester.cxx:13: undefined reference to `log4cxx::Logger::forcedLog(log4cxx::helpers::ObjectPtrT<log4cxx::Level> const&, std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&, log4cxx::spi::LocationInfo const&) const'
collect2: error: ld returned 1 exit status
make: *** [logtest] Error 1
I double checked where apt installed the libraries:
root#starsim-dev:/home/andand# find / -name *log4cxx*
/usr/lib/x86_64-linux-gnu/liblog4cxx.a
/usr/lib/x86_64-linux-gnu/liblog4cxx.so.10.0.0
/usr/lib/x86_64-linux-gnu/liblog4cxx.so.10
/usr/lib/x86_64-linux-gnu/liblog4cxx.so
/usr/lib/x86_64-linux-gnu/pkgconfig/liblog4cxx.pc
/usr/include/log4cxx
/usr/include/log4cxx/private/log4cxx_private.h
/usr/include/log4cxx/log4cxx.h
/usr/share/doc/liblog4cxx10
/usr/share/doc/liblog4cxx10-dev
/usr/share/lintian/overrides/liblog4cxx10
/usr/share/lintian/overrides/liblog4cxx10-dev
/var/cache/apt/archives/liblog4cxx10-dev_0.10.0-4_amd64.deb
/var/cache/apt/archives/liblog4cxx10_0.10.0-4_amd64.deb
/var/lib/dpkg/info/liblog4cxx10:amd64.postinst
/var/lib/dpkg/info/liblog4cxx10:amd64.shlibs
/var/lib/dpkg/info/liblog4cxx10-dev.list
/var/lib/dpkg/info/liblog4cxx10:amd64.list
/var/lib/dpkg/info/liblog4cxx10:amd64.md5sums
/var/lib/dpkg/info/liblog4cxx10:amd64.postrm
/var/lib/dpkg/info/liblog4cxx10-dev.md5sums
The documentation on Apache's website isn't entirely clear what other library dependencies exist. Does anybody have some insight into what I'm missing?
I ran into the same set of linker errors trying to use log4cxx on Ubuntu 18.04. I was able to get their code snippets to work - including the one above - by adding the following additional Apache libs to my MakeFile
LIBS+= -llog4cxx -laprutil-1 -lapr-1
Make sure to list them in the above order.
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.