0

I'm trying to use an external binary library using modern CMake. I've been reading a number of examples, and trying to understand the CMake documentation, but I'm having trouble applying it to my specific case.

Here is the "modern" CMake talk: https://www.youtube.com/watch?v=eC9-iRN2b04 He describes a modular approach to CMake that I'd like to use.

My external module looks like this:

/usr/local/...
include
├── corvusoft
│   └── restbed
│       ├── byte.hpp
│       ├── common.hpp
│       ├── context_placeholder_base.hpp
│       ├── context_placeholder.hpp
│       ├── context_value.hpp
│       ├── http.hpp
│       ├── logger.hpp
│       ├── request.hpp
│       ├── resource.hpp
│       ├── response.hpp
│       ├── rule.hpp
│       ├── service.hpp
│       ├── session.hpp
│       ├── session_manager.hpp
│       ├── settings.hpp
│       ├── ssl_settings.hpp
│       ├── status_code.hpp
│       ├── string.hpp
│       ├── uri.hpp
│       ├── web_socket.hpp
│       └── web_socket_message.hpp
└── restbed
library
├── librestbed.a
├── librestbed.so -> librestbed.so.4
├── librestbed.so.4 -> librestbed.so.4.7
└── librestbed.so.4.7

Here is my latest effort at writing a CMakeList file:

cmake_minimum_required(VERSION 2.6)
project(ktest1 CXX)
set (CMAKE_CXX_STANDARD 11)

set(LIB_PATH /usr/local/library)
set(RESTBED_INCLUDE /usr/local/include/corvusoft/restbed) 
set(HEADER_FILES "${RESTBED_INCLUDE}/session.hpp")
message(${HEADER_FILES})

add_library(restbed ${LIB_PATH})
add_executable(ktest1 main.cpp)

target_include_directories(restbed PUBLIC /usr/local/include ${RESTBED_INCLUDE})
set_target_properties(restbed PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(ktest1 PUBLIC restbed)

install(TARGETS ktest1 RUNTIME DESTINATION bin)

CMake created the build files, make runs to 100%, but then my main program still can't find the library, so I'm doing something incredibly wrong.

ben@VMDebian2:~/Workspaces/test/build$ cmake ..
-- The CXX compiler identification is GNU 6.3.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
/usr/local/include/corvusoft/restbed/session.hpp
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ben/Workspaces/test/build
ben@VMDebian2:~/Workspaces/test/build$ ll
total 28K
-rw-r--r-- 1 ben ben  11K Mar  3 17:38 CMakeCache.txt
drwxr-xr-x 6 ben ben 4.0K Mar  3 17:38 CMakeFiles
-rw-r--r-- 1 ben ben 2.1K Mar  3 17:38 cmake_install.cmake
-rw-r--r-- 1 ben ben 6.7K Mar  3 17:38 Makefile
ben@VMDebian2:~/Workspaces/test/build$ make
Scanning dependencies of target restbed
[ 33%] Linking CXX static library librestbed.a
[ 33%] Built target restbed
Scanning dependencies of target ktest1
[ 66%] Building CXX object CMakeFiles/ktest1.dir/main.cpp.o
[100%] Linking CXX executable ktest1
CMakeFiles/ktest1.dir/main.cpp.o: In function `post_method_handler(std::shared_ptr<restbed::Session>)::{lambda(std::shared_ptr<restbed::Session>, std::vector<unsigned char, std::allocator<unsigned char> > const&)#1}::operator()(std::shared_ptr<restbed::Session>, std::vector<unsigned char, std::allocator<unsigned char> > const&) const':
main.cpp:(.text+0x25a): undefined reference to `restbed::Session::close(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::multimap<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&)'
CMakeFiles/ktest1.dir/main.cpp.o: In function `post_method_handler(std::shared_ptr<restbed::Session>)':
main.cpp:(.text+0x3b5): undefined reference to `restbed::Session::get_request() const'
main.cpp:(.text+0x46a): undefined reference to `restbed::Session::fetch(unsigned long, std::function<void (std::shared_ptr<restbed::Session>, std::vector<unsigned char, std::allocator<unsigned char> > const&)> const&)'
CMakeFiles/ktest1.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x571): undefined reference to `restbed::Resource::set_path(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
main.cpp:(.text+0x5ea): undefined reference to `restbed::Resource::set_method_handler(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::function<void (std::shared_ptr<restbed::Session>)> const&)'
main.cpp:(.text+0x61d): undefined reference to `restbed::Service::Service()'
main.cpp:(.text+0x649): undefined reference to `restbed::Service::publish(std::shared_ptr<restbed::Resource const> const&)'
main.cpp:(.text+0x67c): undefined reference to `restbed::Service::start(std::shared_ptr<restbed::Settings const> const&)'
main.cpp:(.text+0x69c): undefined reference to `restbed::Service::~Service()'
main.cpp:(.text+0x737): undefined reference to `restbed::Service::~Service()'
CMakeFiles/ktest1.dir/main.cpp.o: In function `int restbed::Request::get_header<int, 0>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int) const':
main.cpp:(.text._ZNK7restbed7Request10get_headerIiLi0EEET_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES2_[_ZNK7restbed7Request10get_headerIiLi0EEET_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES2_]+0x4b): undefined reference to `restbed::Request::get_header(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&) const'
CMakeFiles/ktest1.dir/main.cpp.o: In function `void __gnu_cxx::new_allocator<restbed::Resource>::construct<restbed::Resource>(restbed::Resource*)':
main.cpp:(.text._ZN9__gnu_cxx13new_allocatorIN7restbed8ResourceEE9constructIS2_JEEEvPT_DpOT0_[_ZN9__gnu_cxx13new_allocatorIN7restbed8ResourceEE9constructIS2_JEEEvPT_DpOT0_]+0x32): undefined reference to `restbed::Resource::Resource()'
collect2: error: ld returned 1 exit status
CMakeFiles/ktest1.dir/build.make:95: recipe for target 'ktest1' failed
make[2]: *** [ktest1] Error 1
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/ktest1.dir/all' failed
make[1]: *** [CMakeFiles/ktest1.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2

The main clue that I'm doing something wildly incorrect is that the library's librestbed.a appears in the /build file in which I run make, but I'm not generating my program's binary file.

I've experimented with find_package(), but I haven't had any luck with it finding the package. I point to to the librestbed.a file.

Any ideas on the best way to link to an external binary library? I'm using CMake 3.7.2 on Debian 9.8. Here is my main.cpp

#include <memory>
#include <cstdlib>
#include <restbed>

using namespace std;
using namespace restbed;

void post_method_handler( const shared_ptr< Session > session )
{
    const auto request = session->get_request( );

    size_t content_length = request->get_header( "Content-Length", 0 );

    session->fetch( content_length, [ request ]( const shared_ptr< Session > session, const Bytes & body )
    {
        fprintf( stdout, "%.*s\n", ( int ) body.size( ), body.data( ) );
        session->close( OK, "Hello, World!", { { "Content-Length", "13" }, { "Connection", "close" } } );
    } );
}

int main( const int, const char** )
{
    auto resource = make_shared< Resource >( );
    resource->set_path( "/resource" );
    resource->set_method_handler( "POST", post_method_handler );

    Service service;
    service.publish( resource );
    service.start( );

    return EXIT_SUCCESS;
}

I'm able to compile and run this program with clang with the root user like so:

export LD_LIBRARY_PATH=/usr/local/library
clang++ -L/usr/local/library -std=c++11 -o test main.cpp -l restbed 

Thanks

BenW
  • 737
  • 10
  • 41
  • `add_library` doesn't look right. I'm assuming you don't want to build the library, just link against it so `target_link_libraries` should be enough. Possibly together with `target_link_directories`. – super Mar 04 '19 at 00:24
  • I've tried this. I get the error "Cannot specify include directories for target "restbed" which is not built by this project." It looks like my version of CMake doesn't have target_link_directories. I've tried link_directories(${LIB_PATH}) and find_library(${LIB_PATH}). I get the same error either way. – BenW Mar 04 '19 at 02:00
  • "Modern" way uses *IMPORTED* keyword when declare external library. See following answer for more details: https://stackoverflow.com/a/10550334/3440745. Note, that `IMPORTED_LOCATION` property for *IMPORTED* library should contain absolute path to the **library file**, not a *directory*. – Tsyvarev Mar 04 '19 at 08:09
  • @horseatingweeds You don't need to specify include directories for `restbed` since it's already built. You just need to tell CMake where it is so you can link against it. You do however need to add the `redtbed` includes to `ktest1`. – super Mar 04 '19 at 16:46

1 Answers1

0

First, you don't need add_library(restbed ${LIB_PATH}) at all. It is used to create your own library during the build process, like add_executable() is for executables.

Second, you have to call target_include_directories() on ktest1 target.

Finally, pass absolute full path to the library in target_link_libraries() call.

arrowd
  • 33,231
  • 8
  • 79
  • 110