How to link against cpp-netlib

2019-04-20 01:09发布

问题:

I would like to use the Library cpp-netlib for a C++ project. Therefore I installed the boost library with the help of homebrew (OS is Mac OS X 10.8). Then I downloaded cpp-netlib from the projects homepage, used cmake to create the Makefile for g++ and successfully applied make. "make test" passed all its tests. Then I copied the include folder of cpp-netlib into the boost directory.

So here is when the trouble began: I tried to compile the documentation's first example http-client but couldn't get it to work. When I used

g++ test.cpp -o out -I/usr/local/Cellar/boost/1.53.0/include 
-L/usr/local/Cellar/boost/1.53.0/lib 
-lboost_system-mt -lboost_filesystem-mt -lboost_thread-mt

I received these linker errors:

Undefined symbols for architecture x86_64:
  "boost::network::uri::detail::parse(__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::network::uri::detail::uri_parts<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&)", referenced from:
      boost::network::uri::uri::parse()  in ccs87Dq3.o
  "boost::network::http::impl::normal_delegate::normal_delegate(boost::asio::io_service&)", referenced from:
      boost::network::http::impl::connection_delegate_factory<boost::network::http::tags::http_async_8bit_udp_resolve>::new_connection_delegate(boost::asio::io_service&, bool, boost::optional<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::optional<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >)in ccs87Dq3.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

I really used the search function but I couldn't find any solution for my problem. What am I doing wrong?

Thanks a lot!

回答1:

After building cpp-netlib (>=0.9.3) there should 3 static libraries:

libcppnetlib-client-connections.a
libcppnetlib-server-parsers.a
libcppnetlib-uri.a

When building your http-client project, you should specify a library path for cpp-netlib (-L) and libraries to link (-l) against: cppnetlib-uri and libcppnetlib-client-connections.



回答2:

Here is what worked for me. You will need to modify certain parts to deal with different versions of boost, different install paths, and so on.

g++ -o demo \
    demo.cpp \
    -lcppnetlib-uri \
    -lcppnetlib-server-parsers \
    -lcppnetlib-client-connections \
    -lboost_thread-mt \
    -lboost_system-mt \
    -lssl \
    -lcrypto \
    -I/usr/local/include \
    -L/usr/local/lib

If you are writing server-side code then I imagine you will also need to include -lcppnetlib-server-parsers.



回答3:

In case someone is having the same issue with visual studio, provide the library input to the linker.

Project properties > Linker > input >

libcppnetlib-client-connectionsd.lib
libcppnetlib-urid.lib

for Debug configuration and

libcppnetlib-client-connections.lib
libcppnetlib-uri.lib

for Release configuration.

Specify the Library path here,

Project properties > Linker > general > Additional include directories