Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include Pistache in C++ project

Tags:

c++

cmake

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!

like image 438
wmash Avatar asked Sep 23 '18 17:09

wmash


Video Answer


1 Answers

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})
like image 74
perezmlal Avatar answered Sep 27 '22 19:09

perezmlal