10

Apologies for the fact this is a very noob question but I am fairly new to C++.

I'm building a RESTful service with pistache. I have checked it out and got the examples running within the project itself but am now trying to import/include the framework to use in my own project.

My folder structure is as follows:

rest_api
   |
   +--- build
   +--- include
          |
          +--- pistache
   +--- src
          |
          +--- main.cpp
   +--- tests

The pistache directory holds all of the pistache source code compiled. (I am unsure here if I need the entirety of the project or just the header files)

I have attempted to follow the example and the quickstart guide but have had no look.

My CMakeLists.txt is barebones currently looking like this:

cmake_minimum_required(VERSION 3.5.1)
project(rest_api)

set(CMAKE_CXX_STANDARD 14)
set(PISTACHE_DIR "./include/pistache")

include_directories (${PISTACHE_DIR}/include)

add_executable(${PROJECT_NAME} src/main.cpp)

My main.cpp is a direct copy of their example hello_server.cc.

When I try and make my project, I am returned with the exceptions (a snapshot of):

main.cpp:(.text+0x143): undefined reference to `Pistache::Port::Port(unsigned short)'
main.cpp:(.text+0x148): undefined reference to `Pistache::Ipv4::any()'
main.cpp:(.text+0x162): undefined reference to `Pistache::Address::Address(Pistache::Ipv4, Pistache::Port)'
main.cpp:(.text+0x171): undefined reference to `Pistache::Http::Endpoint::options()'
main.cpp:(.text+0x185): undefined reference to `Pistache::Http::Endpoint::Options::threads(int)'
main.cpp:(.text+0x1c9): undefined reference to `Pistache::Http::Endpoint::Endpoint(Pistache::Address const&)'
main.cpp:(.text+0x1e2): undefined reference to `Pistache::Http::Endpoint::init(Pistache::Http::Endpoint::Options const&)'
main.cpp:(.text+0x223): undefined reference to `Pistache::Http::Endpoint::setHandler(std::shared_ptr<Pistache::Http::Handler> const&)'

I have looked at questions such as this but does not help me.

My questions are:

  1. Do I need the entirety of the Pistache source code or just the headers?
  2. What is wrong in my CMakeLists.txt that causes these errors?

Apologies if this is seen as a duplicate but have not been able to find the right answers I need.

Thank you!

Community
  • 1
  • 1
wmash
  • 4,032
  • 3
  • 31
  • 69
  • 1
    You need the headers and either the source or a library built from the source code. Not sure which since I'm not familiar with pistache, but normally in C++ you need the header files and a library, and you need to add the library to the CMakeLists.txt file – john Sep 23 '18 at 17:32
  • 1
    As usual with *libraries*, for use it you need to **include directories**, containing the library's headers, and **link** with the **library** file. You have done only the first part (include directories), that is why you got "undefined reference" error. You may look into file [examples/CMakeLists.txt](https://github.com/oktal/pistache/blob/master/examples/CMakeLists.txt), which builds examples: the function `pistache_example`, called for every example, links the executable. (Note, that this `CMakeLists.txt` isn't *standalone* - it is intended to be called from the upper scripts). – Tsyvarev Sep 23 '18 at 17:59
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Tsyvarev Sep 23 '18 at 18:01
  • @Tsyvarev thank you for explaining that to me and providing a link to the example. I just had to add `target_link_libraries` into my CMake and it now works. If you post an answer, I will accept – wmash Sep 23 '18 at 20:56
  • I think you are missing `target_link_libraries`. Possible duplicate of [How to properly link libraries with cmake?](https://stackoverflow.com/q/39598323/608639) and [How to link a static library to an executable using CMake](https://stackoverflow.com/q/28536435/608639). – jww Sep 24 '18 at 00:40

1 Answers1

5

This my CMakeLists.txt file. Works fine :D

cmake_minimum_required(VERSION 3.12)
project(PistacheExample)

set(CMAKE_CXX_STANDARD 11)

############################
##      SOURCE FILES      ##
############################
add_executable(${PROJECT_NAME} src/main.cpp)

#####################################
##      HEADERS SEARCH PATHS       ##
##################################### 
set(PROJECT_INCLUDE_DIR "src/include")
set(PISTACHE_INCLUDE_DIR "libs/pistache/include")

set(HEADER_SEARCH_PATHS ${PROJECT_INCLUDE_DIR} ${PISTACHE_INCLUDE_DIR})

#####################################
##      LIBRARY SEARCH PATHS       ##
#####################################
set(PISTACHE_LIBRARY "${PROJECT_SOURCE_DIR}/libs/pistache/lib/libpistache.a")
set(EXTRA_LIBRARY "-pthread -lssl")

set(LIBRARIES_SEARCH_PATHS ${PISTACHE_LIBRARY} ${EXTRA_LIBRARY})

#######################################
##      ADDING HEADERS LIBRARY       ##
#######################################
include_directories(${HEADER_SEARCH_PATHS})
target_link_libraries(${PROJECT_NAME} ${LIBRARIES_SEARCH_PATHS})
perezmlal
  • 360
  • 5
  • 12