Compiling with external libraries - c++

I am having an issue with compiling one of the sample (example codes) for a C++ based jwt library from a github project: jwt-cpp
I cloned it and compiled it using the steps provided in the README file, which seemed successful.
After that I did a ldconfig.
Now, I am trying to build the example code.
#include <iostream>
#include "jwt/jwt_all.h"
using json = nlohmann::json;
int main()
{
// Setup a signer
HS256Validator signer("secret!");
// Create the json payload that expires 01/01/2017 # 12:00am (UTC)
json payload = {{"sub", "subject"}, {"exp", 1483228800}};
// Let's encode the token to a string
auto token = JWT::Encode(signer, payload);
std::cout << token << std::endl;
}
I compile this with a command on terminal, which says:
g++ -std=c++11 \
-I/usr/local/include \
-I/usr/local/include \
/usr/local/lib/libjwt.a \
/usr/local/lib/libcrypto.a \
sign.cpp -o sign
And it results in following error:
/tmp/ccX4ghoR.o: In function `main':
sign.cpp:(.text+0x24e): undefined reference to
JWT::Encode(MessageSigner const&, nlohmann::basic_json<std::map,
std::vector, std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >, bool, long, unsigned long, double,
std::allocator, nlohmann::adl_serializer> const&,
nlohmann::basic_json<std::map, std::vector,
std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >, bool, long, unsigned long, double,
std::allocator, nlohmann::adl_serializer>)'
/tmp/ccX4ghoR.o: In function
`HS256Validator::HS256Validator(std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&)': sign.cpp:(.text._ZN14HS256ValidatorC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN14HS256ValidatorC5ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x21): undefined reference to `EVP_sha256' sign.cpp:(.text._ZN14HS256ValidatorC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN14HS256ValidatorC5ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x5f): undefined reference to `HMACValidator::HMACValidator(std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&, evp_md_st
const*, std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&)'
/tmp/ccX4ghoR.o:
(.rodata._ZTV14HS256Validator[_ZTV14HS256Validator]+0x20): undefined
reference to `HMACValidator::Verify(nlohmann::basic_json<std::map,
std::vector, std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >, bool, long, unsigned long, double,
std::allocator, nlohmann::adl_serializer> const&, unsigned char
const*, unsigned long, unsigned char const*, unsigned long) const'
/tmp/ccX4ghoR.o:
(.rodata._ZTV14HS256Validator[_ZTV14HS256Validator]+0x28): undefined
reference to `HMACValidator::toJson[abi:cxx11]() const'
/tmp/ccX4ghoR.o:
(.rodata._ZTV14HS256Validator[_ZTV14HS256Validator]+0x38): undefined
reference to `MessageValidator::Accepts(nlohmann::basic_json<std::map,
std::vector, std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >, bool, long, unsigned long, double,
std::allocator, nlohmann::adl_serializer> const&) const'
/tmp/ccX4ghoR.o:
(.rodata._ZTV14HS256Validator[_ZTV14HS256Validator]+0x40): undefined
reference to `HMACValidator::Sign(unsigned char const*, unsigned long,
unsigned char*, unsigned long*) const'
/tmp/ccX4ghoR.o: In function `HS256Validator::~HS256Validator()':
sign.cpp:
(.text._ZN14HS256ValidatorD2Ev[_ZN14HS256ValidatorD5Ev]+0x20):
undefined reference to `HMACValidator::~HMACValidator()'
/tmp/ccX4ghoR.o:
(.rodata._ZTI14HS256Validator[_ZTI14HS256Validator]+0x10): undefined
reference to `typeinfo for HMACValidator'
collect2: error: ld returned 1 exit status
What have I tried:
I have read through these questions to know what I might be doing wrong:
How to use libraries and compile C file using external library from linux terminal, Although second question is specifically for C and not C++, the problem there and the solution is applicable to C++ as well.
I have also tried variants of command line compilation such as,
g++ -std=c++11 \
-I/usr/local/include \
-I/usr/local/include \
-L/usr/local/lib/ \
-lcrypto -ljwt \
sign.cpp -o sign
I am familiar with the fact that when I do a -lfoo, the linker tries to find a libfoo.a at the location provided with -L option.
I have confirmed that the options that are used for compilation contains what they should.
For -I options:
I can see jwt and openssl directories in /usr/local/include
For -L options:
I can see libjwt.a, libssl.a, libcrypto.a, libssl.so, etc at /usr/local/lib/
Question:
What am I doing wrong to compile this example?

You're gonna kick yourself when I tell you the issue.
Put the source file, sign.cpp before the library declarations in your program:
g++ -std=c++11 -I/usr/local/include -L/usr/local/lib sign.cpp -lcrypto -ljwt -o sign
The reason being is that the unix linker doesn't look backwards in the command line to resolve dependencies. There's a few command line switches (e.g. --start-group and --end-group) that will adjust this behavior, but the above will get you unblocked for now. More details here.
In the above example, I took the liberty of removing the duplicated INCLUDE path arguments. You probably don't even need the -I/usr/local/include or -L/usr/local/lib part because that's typically already in the standard compiler path.

Related

Linker Error with log4cxx

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.

how is linking done in c++?

i am using proxygen library by facebook to build a simple client example . in a directory i have two object files how do i link them :
i am using:
g++ -std=c++11 -o my_echo CurlClientMain.o CurlClient.o -lproxygenhttpserver -lfolly -lglog -lgflags -pthread
i think i ma missing some linker flag like -lgflags in above example. maybe after including some -someflag will help out compilation . how do i know what all possilble library flags are posiible like the one they have used -lproxyhttpserver.
In short where is all these libs defined or located. i am using ubuntu.
Here is my error message
In function `main':
/home/kshitij/proxygen/httpclient/samples/curl/CurlClientMain.cpp:91: undefined reference to `proxygen::HTTPConnector::HTTPConnector(proxygen::HTTPConnector::Callback*, folly::HHWheelTimer*)'
/home/kshitij/proxygen/httpclient/samples/curl/CurlClientMain.cpp:102: undefined reference to `proxygen::HTTPConnector::connect(folly::EventBase*, folly::SocketAddress const&, std::chrono::duration<long, std::ratio<1l, 1000l> >, std::map<folly::AsyncSocket::OptionKey, int, std::less<folly::AsyncSocket::OptionKey>, std::allocator<std::pair<folly::AsyncSocket::OptionKey const, int> > > const&, folly::SocketAddress const&)'
/home/kshitij/proxygen/httpclient/samples/curl/CurlClientMain.cpp:91: undefined reference to `proxygen::HTTPConnector::~HTTPConnector()'
/home/kshitij/proxygen/httpclient/samples/curl/CurlClientMain.cpp:99: undefined reference to `proxygen::HTTPConnector::connectSSL(folly::EventBase*, folly::SocketAddress const&, std::shared_ptr<folly::SSLContext> const&, ssl_session_st*, std::chrono::duration<long, std::ratio<1l, 1000l> >, std::map<folly::AsyncSocket::OptionKey, int, std::less<folly::AsyncSocket::OptionKey>, std::allocator<std::pair<folly::AsyncSocket::OptionKey const, int> > > const&, folly::SocketAddress const&, std::string const&)'
/home/kshitij/proxygen/httpclient/samples/curl/CurlClientMain.cpp:91: undefined reference to `proxygen::HTTPConnector::~HTTPConnector()'
CurlClient.o: In function `CurlService::CurlClient::connectSuccess(proxygen::HTTPUpstreamSession*)':
/home/kshitij/proxygen/httpclient/samples/curl/CurlClient.cpp:69: undefined reference to `proxygen::HTTPUpstreamSession::newTransaction(proxygen::HTTPTransactionHandler*)'
collect2: error: ld returned 1 exit status
kshitij#forgetit:~/proxygen/httpclient/samples/curl$ g++ -std=c++11 -o my_echo CurlClientMain.o CurlClient.o -lproxygenhttpserver -lfolly -lglog -lgflags -pthread
CurlClientMain.o: In function `main':
/home/kshitij/proxygen/httpclient/samples/curl/CurlClientMain.cpp:91: undefined reference to `proxygen::HTTPConnector::HTTPConnector(proxygen::HTTPConnector::Callback*, folly::HHWheelTimer*)'
/home/kshitij/proxygen/httpclient/samples/curl/CurlClientMain.cpp:102: undefined reference to `proxygen::HTTPConnector::connect(folly::EventBase*, folly::SocketAddress const&, std::chrono::duration<long, std::ratio<1l, 1000l> >, std::map<folly::AsyncSocket::OptionKey, int, std::less<folly::AsyncSocket::OptionKey>, std::allocator<std::pair<folly::AsyncSocket::OptionKey const, int> > > const&, folly::SocketAddress const&)'
/home/kshitij/proxygen/httpclient/samples/curl/CurlClientMain.cpp:91: undefined reference to `proxygen::HTTPConnector::~HTTPConnector()'
/home/kshitij/proxygen/httpclient/samples/curl/CurlClientMain.cpp:99: undefined reference to `proxygen::HTTPConnector::connectSSL(folly::EventBase*, folly::SocketAddress const&, std::shared_ptr<folly::SSLContext> const&, ssl_session_st*, std::chrono::duration<long, std::ratio<1l, 1000l> >, std::map<folly::AsyncSocket::OptionKey, int, std::less<folly::AsyncSocket::OptionKey>, std::allocator<std::pair<folly::AsyncSocket::OptionKey const, int> > > const&, folly::SocketAddress const&, std::string const&)'
/home/kshitij/proxygen/httpclient/samples/curl/CurlClientMain.cpp:91: undefined reference to `proxygen::HTTPConnector::~HTTPConnector()'
CurlClient.o: In function `CurlService::CurlClient::connectSuccess(proxygen::HTTPUpstreamSession*)':
/home/kshitij/proxygen/httpclient/samples/curl/CurlClient.cpp:69: undefined reference to `proxygen::HTTPUpstreamSession::newTransaction(proxygen::HTTPTransactionHandler*)'
collect2: error: ld returned 1 exit status
how do i know what all possilble library flags are posiible like the one they have used -lproxyhttpserver.
It depends on your compilation environment.
You appear to use the GNU compiler in linux. There is a tool to list all installed shared libraries:
ldconfig -p
You will get a list of lines like this
libpthread.so.0 (libc6,x86-64, OS ABI: Linux 2.6.32) => /lib/x86_64-linux-gnu/libpthread.so.0
Remove the lib prefix and the extension .so.X and you get the name of the library. In this case it's pthread. To link with a libray use the option -lNAME. So, -lpthread in this example.
In short where is all these libs defined or located
The righthand part of the => is the full path to the library.

How do I compile Contraction Hierarchies by KIT?

I am trying to compile the Contraction Hierarchies Implementation by KIT.
This software has been published in 2009 and obviously not been maintained since then. Since a few things have changed in the mean time (with new C++ standards and compiler versions) the code does not compile off the shelf anymore.
README is not very verbose when it comes to instructions on compiling and says you should only invoke make. However, using make gives me the following error:
g++ -Wall -W -Wno-unused-parameter -O6 -c -o main.o main.cpp
In file included from /usr/include/c++/4.8/ext/hash_map:60:0,
from command/../io/../datastr/graph/../../io/serialize.h:26,
from command/../io/../datastr/graph/edge.h:26,
from command/../io/../datastr/graph/graph.h:59,
from command/../io/createGraph.h:28,
from command/NodeOrder.h:29,
from main.cpp:35:
/usr/include/c++/4.8/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date. Please use a non-deprecated interface with equivalent functionality instead. For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated. [-Wcpp]
#warning \
^
command/../processing/../EliminationWeight.h:42:35: error: a call to a constructor cannot appear in a constant-expression
static const Type MAX_VALUE = __DBL_MAX__;
^
command/../processing/../EliminationWeight.h:43:36: error: a call to a constructor cannot appear in a constant-expression
static const Type MIN_VALUE = -__DBL_MAX__;
^
make: *** [main.o] Error 1
What do I have to do to make this compile?
To make the software compile without errors, a few steps are necessary. I have tested these instruction with gcc 4.8.1 and Boost 1.53.0.
a call to a constructor cannot appear in a constant-expression
(I have already answered this subproblem in detail there but will repeat the important steps here.)
The first compilation error we encounter is due to the software using non-standard extensions of the GCC compiler. It seems that at the time of the software's development these did not have to be enabled by a compiler switch but modern GCC requires this. However, for future compatibility, we will just get rid of them and implement the features in a standard compliant way.
The compiler tells us that the problem is in lines 42 and 43 of EliminationWeight.h. These are the lines in that file:
static const Type MAX_VALUE = __DBL_MAX__;
static const Type MIN_VALUE = -__DBL_MAX__;
GCC complains here because initialization of doubles in a class body is not covered by the standard. Instead, we should do it separately. So remove the initialization and change the lines to the following:
static const Type MAX_VALUE;
static const Type MIN_VALUE;
Also remove the #include <limits> as we no longer need that in this file.
Since we still want to initialize these values, we take a look at main.cpp and find line 84 and following:
// doesn't look nice, but required by the compiler (gcc 4)
const EdgeWeight Weight::MAX_VALUE;
const EliminationWeight::Type EliminationWeight::MAX_VALUE;
const EliminationWeight::Type EliminationWeight::MIN_VALUE;
This looks like the perfect place to do our initializations. Change lines 86 and 87 to contain the following code:
const EliminationWeight::Type EliminationWeight::MAX_VALUE = std::numeric_limits< EliminationWeight::Type >::max();
const EliminationWeight::Type EliminationWeight::MIN_VALUE = -std::numeric_limits< EliminationWeight::Type >::max();
Also add
#include <limits>
to main.cpp because we are now using the std::numeric_limits class from that header.
collect2: error: ld returned 1 exit status
Compilation will now work but linking will fail with a huge error message. (I have already covered this problem there but will repeat the gist of it here.)
All the errors will be about linking of Boost::Regex. This is the complete error message on my machine:
g++ -lboost_regex -lboost_iostreams -o main main.o
main.o: In function `boost::re_detail::perl_matcher, std::allocator > >, boost::regex_traits > >::unwind_extra_block(bool)':
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE18unwind_extra_blockEb[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE18unwind_extra_blockEb]+0x2c): undefined reference to `boost::re_detail::put_mem_block(void*)'
main.o: In function `void boost::re_detail::raise_error > > >(boost::regex_traits_wrapper > > const&, boost::regex_constants::error_type)':
main.cpp:(.text._ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE[_ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE]+0x7d): undefined reference to `boost::re_detail::get_default_error_string(boost::regex_constants::error_type)'
main.cpp:(.text._ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE[_ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE]+0xb1): undefined reference to `boost::re_detail::raise_runtime_error(std::runtime_error const&)'
main.cpp:(.text._ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE[_ZN5boost9re_detail11raise_errorINS_20regex_traits_wrapperINS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEEEvRKT_NS_15regex_constants10error_typeE]+0xcb): undefined reference to `boost::re_detail::get_default_error_string(boost::regex_constants::error_type)'
main.o: In function `__gnu_cxx::__normal_iterator boost::re_detail::re_is_set_member, char, boost::regex_traits >, unsigned int>(__gnu_cxx::__normal_iterator, __gnu_cxx::__normal_iterator, boost::re_detail::re_set_long const*, boost::re_detail::regex_data > > const&, bool)':
main.cpp:(.text._ZN5boost9re_detail16re_is_set_memberIN9__gnu_cxx17__normal_iteratorIPKcSsEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEjEET_SB_SB_PKNS0_11re_set_longIT2_EERKNS0_10regex_dataIT0_T1_EEb[_ZN5boost9re_detail16re_is_set_memberIN9__gnu_cxx17__normal_iteratorIPKcSsEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEjEET_SB_SB_PKNS0_11re_set_longIT2_EERKNS0_10regex_dataIT0_T1_EEb]+0x17b): undefined reference to `boost::re_detail::cpp_regex_traits_implementation::transform_primary(char const*, char const*) const'
main.cpp:(.text._ZN5boost9re_detail16re_is_set_memberIN9__gnu_cxx17__normal_iteratorIPKcSsEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEjEET_SB_SB_PKNS0_11re_set_longIT2_EERKNS0_10regex_dataIT0_T1_EEb[_ZN5boost9re_detail16re_is_set_memberIN9__gnu_cxx17__normal_iteratorIPKcSsEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEjEET_SB_SB_PKNS0_11re_set_longIT2_EERKNS0_10regex_dataIT0_T1_EEb]+0x4c0): undefined reference to `boost::re_detail::cpp_regex_traits_implementation::transform(char const*, char const*) const'
main.o: In function `boost::re_detail::perl_matcher, std::allocator > >, boost::regex_traits > >::extend_stack()':
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE12extend_stackEv[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE12extend_stackEv]+0x18): undefined reference to `boost::re_detail::get_mem_block()'
main.o: In function `boost::re_detail::perl_matcher, std::allocator > >, boost::regex_traits > >::match_imp()':
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv]+0xc): undefined reference to `boost::re_detail::get_mem_block()'
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv]+0x19e): undefined reference to `boost::re_detail::verify_options(unsigned int, boost::regex_constants::_match_flags)'
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv]+0x254): undefined reference to `boost::re_detail::put_mem_block(void*)'
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE9match_impEv]+0x3c6): undefined reference to `boost::re_detail::put_mem_block(void*)'
main.o: In function `bool boost::regex_match, std::allocator, std::allocator > >, char, boost::regex_traits > >(std::basic_string, std::allocator > const&, boost::match_results, std::allocator >::const_iterator, std::allocator > > >&, boost::basic_regex > > const&, boost::regex_constants::_match_flags)':
main.cpp:(.text._ZN5boost11regex_matchISt11char_traitsIcESaIcESaINS_9sub_matchIN9__gnu_cxx17__normal_iteratorIPKcSsEEEEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbRKSbIT2_T_T0_ERNS_13match_resultsINSJ_14const_iteratorET1_EERKNS_11basic_regexISG_T3_EENS_15regex_constants12_match_flagsE[_ZN5boost11regex_matchISt11char_traitsIcESaIcESaINS_9sub_matchIN9__gnu_cxx17__normal_iteratorIPKcSsEEEEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbRKSbIT2_T_T0_ERNS_13match_resultsINSJ_14const_iteratorET1_EERKNS_11basic_regexISG_T3_EENS_15regex_constants12_match_flagsE]+0xe9): undefined reference to `boost::re_detail::perl_matcher, std::allocator > >, boost::regex_traits > >::construct_init(boost::basic_regex > > const&, boost::regex_constants::_match_flags)'
main.o: In function `void Command::createVector(std::string const&, std::vector >&, double)':
main.cpp:(.text._ZN7Command12createVectorIdEEvRKSsRSt6vectorIT_SaIS4_EES4_[_ZN7Command12createVectorIdEEvRKSsRSt6vectorIT_SaIS4_EES4_]+0x4a): undefined reference to `boost::basic_regex > >::do_assign(char const*, char const*, unsigned int)'
main.cpp:(.text._ZN7Command12createVectorIdEEvRKSsRSt6vectorIT_SaIS4_EES4_[_ZN7Command12createVectorIdEEvRKSsRSt6vectorIT_SaIS4_EES4_]+0x7b): undefined reference to `boost::basic_regex > >::do_assign(char const*, char const*, unsigned int)'
main.cpp:(.text._ZN7Command12createVectorIdEEvRKSsRSt6vectorIT_SaIS4_EES4_[_ZN7Command12createVectorIdEEvRKSsRSt6vectorIT_SaIS4_EES4_]+0xac): undefined reference to `boost::basic_regex > >::do_assign(char const*, char const*, unsigned int)'
main.o: In function `void Command::createVector(std::string const&, std::vector >&, unsigned int)':
main.cpp:(.text._ZN7Command12createVectorIjEEvRKSsRSt6vectorIT_SaIS4_EES4_[_ZN7Command12createVectorIjEEvRKSsRSt6vectorIT_SaIS4_EES4_]+0x48): undefined reference to `boost::basic_regex > >::do_assign(char const*, char const*, unsigned int)'
main.cpp:(.text._ZN7Command12createVectorIjEEvRKSsRSt6vectorIT_SaIS4_EES4_[_ZN7Command12createVectorIjEEvRKSsRSt6vectorIT_SaIS4_EES4_]+0x79): undefined reference to `boost::basic_regex > >::do_assign(char const*, char const*, unsigned int)'
main.o:main.cpp:(.text._ZN7Command12createVectorIjEEvRKSsRSt6vectorIT_SaIS4_EES4_[_ZN7Command12createVectorIjEEvRKSsRSt6vectorIT_SaIS4_EES4_]+0xaa): more undefined references to `boost::basic_regex > >::do_assign(char const*, char const*, unsigned int)' follow
main.o: In function `boost::re_detail::perl_matcher, std::allocator > >, boost::regex_traits > >::match_match()':
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE11match_matchEv[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE11match_matchEv]+0x371): undefined reference to `boost::match_results, std::allocator > > >::maybe_assign(boost::match_results, std::allocator > > > const&)'
main.o: In function `boost::re_detail::perl_matcher, std::allocator > >, boost::regex_traits > >::match_dot_repeat_slow()':
main.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE21match_dot_repeat_slowEv[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE21match_dot_repeat_slowEv]+0x229): undefined reference to `boost::re_detail::get_mem_block()'
collect2: error: ld returned 1 exit status
make: *** [main] Error 1
It seems like Boost::Regex is not found at all. Note that this is one of the Boost libraries that need to be compiled and linked into your application to work. The following is under the assumption that you have obtained a compiled version and it is in your system's library directory.
When linking, the order in which libraries and object files are passed to GCC via command line parameters is important. For some reason what seems to have worked with GCC in 2009 is no longer working now. This can be fixed by changing the order in which make is passing the parameters to GCC.
Locate Makefile in the project root directory and find line 6:
$(CXX) $(LINK) -o $# $^ $(LIBS)
Here you can see that make is passing the linker switches containing the Boost libraries before the object file. (If you can't, don't worry. It is not necessary that you understand Makefiles to follow this explanation.) To make this work with current GCC we will change the order of the parameters. Line 6 of your Makefile should look like this:
$(CXX) -o $# $^ $(LIBS) $(LINK)
Save it and run make again. Compilation and linking should now proceed without errors.
warning: #warning This file includes at least one deprecated or antiquated header
You will still see a compiler warning telling you that you are using deprecated header files. You can ignore this warning. However, it might be possible that newer versions of GCC don't come with this header file and compilation will fail.
If you want to fix it, here is how.
The problem lies in io/serialize.h which includes <ext/hash_map>. The C++11 standard replaces this with unordered_map. So we fix the code to use that. Line 128 will be
typedef __gnu_cxx::hash_map<key_type, data_type> HashMap;
Change this to use unordered_map as follows:
typedef std::unordered_map<key_type, data_type> HashMap;
Now we also need to fix the header. Remove the include of <ext/hash_map> and replace line 26 with
#include <algorithm>
#include <unordered_map>
The <algorithm> include is needed because obviously <ext/hash_map> also provided std::sort and std::reverse for the hash_map. These functions are now in the <algorithm> header and the code relies on the fact that these are included by this file.
Since this is a C++11 feature we need to tell GCC that we want support for that. Go to compiler.make in the project's root directory and find line 6 which should be
CXXFLAGS = $(DEBUG) $(WARNING) $(OPTIMIZER)
At the end of the line add the switch to use C++11:
CXXFLAGS = $(DEBUG) $(WARNING) $(OPTIMIZER) -std=c++11
Run make clean and make again and your code should compile without any errors or warnings.

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.

Problem in Building mplsh-run in lshkit

been trying out this for quite some time but
I'm still unable to built mplsh-run from lshkit
Not sure if this would help to explain my situation during the
building process
/tmp/cc17kth4.o: In function `lshkit::MultiProbeLshRecallTable::reset(lshkit::MultiProbeLshModel, unsigned int, double, double)':
mplsh-run.cpp:(.text._ZN6lshkit24MultiProbeLshRecallTable5resetENS_18MultiProbeLshModelEjdd[lshkit::MultiProbeLshRecallTable::reset(lshkit::MultiProbeLshModel, unsigned int, double, double)]+0x230): undefined reference to `lshkit::MultiProbeLshModel::recall(double) const'
/tmp/cc17kth4.o: In function `void lshkit::MultiProbeLshIndex<unsigned int>::query_recall<lshkit::TopkScanner<lshkit::Matrix<float>::Accessor, lshkit::metric::l2sqr<float> > >(float const*, float, lshkit::TopkScanner<lshkit::Matrix<float>::Accessor, lshkit::metric::l2sqr<float> >&) const':
mplsh-run.cpp:(.text._ZNK6lshkit18MultiProbeLshIndexIjE12query_recallINS_11TopkScannerINS_6MatrixIfE8AccessorENS_6metric5l2sqrIfEEEEEEvPKffRT_[void lshkit::MultiProbeLshIndex<unsigned int>::query_recall<lshkit::TopkScanner<lshkit::Matrix<float>::Accessor, lshkit::metric::l2sqr<float> > >(float const*, float, lshkit::TopkScanner<lshkit::Matrix<float>::Accessor, lshkit::metric::l2sqr<float> >&) const]+0x2c4): undefined reference to `lshkit::MultiProbeLsh::genProbeSequence(float const*, std::vector<unsigned int, std::allocator<unsigned int> >&, unsigned int) const'
/tmp/cc17kth4.o: In function `void lshkit::MultiProbeLshIndex<unsigned int>::query<lshkit::TopkScanner<lshkit::Matrix<float>::Accessor, lshkit::metric::l2sqr<float> > >(float const*, unsigned int, lshkit::TopkScanner<lshkit::Matrix<float>::Accessor, lshkit::metric::l2sqr<float> >&)':
mplsh-run.cpp:(.text._ZN6lshkit18MultiProbeLshIndexIjE5queryINS_11TopkScannerINS_6MatrixIfE8AccessorENS_6metric5l2sqrIfEEEEEEvPKfjRT_[void lshkit::MultiProbeLshIndex<unsigned int>::query<lshkit::TopkScanner<lshkit::Matrix<float>::Accessor, lshkit::metric::l2sqr<float> > >(float const*, unsigned int, lshkit::TopkScanner<lshkit::Matrix<float>::Accessor, lshkit::metric::l2sqr<float> >&)]+0x4a): undefined reference to `lshkit::MultiProbeLsh::genProbeSequence(float const*, std::vector<unsigned int, std::allocator<unsigned int> >&, unsigned int) const'
collect2: ld returned 1 exit status
the command that i used to built mplsh-run is
g++ -I./lshkit/include -L/usr/lib -lm -lgsl -lgslcblas -lboost_program_options-mt mplsh-run.cpp
Do you guys have any clue on how I could solve this?
Put the .cpp file as the first argument.
g++ mplsh-run.cpp -I./lshkit/include -lm -lgsl -lgslcblas -lboost_program_options-mt
Also try with different permutations of -lm -lgsl and -lgslcblas