Proper way to use an external binary library in your project, using cmake

I'm experimenting with this C++ library, restbed. https://github.com/corvusoft/restbed

Following the readme, I checked it out from github and ran the build commands, which created the following files. I'm using Debian.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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


I can compile and run a test like so:
clang++ -L/usr/local/library -std=c++11 -o test test.cpp -l restbed

I'm trying to figure out how to compile a project that includes restbed using cmake. So what is the best way to do this? Should I reference the executables in the /usr/local/ area? Should I pull it into the project itself and somehow keep it pre-compiled?

I'm a big confused by now. I'm also not very experienced with deploying C++ projects or using cmake. Below is my last try at using CMakeList to look at the files in /usr/local, but it's not working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cmake_minimum_required(VERSION 2.6)
project(ktest1)


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

add_library(librestbed ${HEADER_FILES})
add_executable(restbed ${HEADER_FILES})
target_include_directories( restbed PRIVATE ${RESTBED_INCLUDE} )
target_include_directories( restbed PRIVATE ${LIB_PATH})

add_executable(ktest1 main.cpp)
#add_library(restbed STATIS /usr/local/library/librestbed.a)


install(TARGETS ktest1 RUNTIME DESTINATION bin)


The errors I get include..

Cannot dtermine link language for target "librestbed".
CMake can not determine linker language for target: librestbed
CMake can not determine linker language for target: restbed
Cannot determine linke language for target "restbed"

I've tried adding "CXX" to the project(ktest1 CXX), but that doesn't change the error.

Any good advice about best approach to including these kinds of libraries? I've been looking for how to "link and external binary library". Maybe I'm reading the wrong things.

Thanks
You're using CMake wrong.

1. add_library() doesn't link in an external library, it tells CMake that you want your project to be linked into a library.
2. Likewise for add_executable().
3. To link in an external library you use target_link_libraries(). Remember to use link_directories() and include_directories() to tell CMake to tell the compiler where to look for files.

If you google these functions the CMake documentation is usually the first result.
Topic archived. No new replies allowed.