Using cmake for a project with sub/external projects/dependencies

孤者浪人 提交于 2021-01-28 14:02:16

问题


I am actually imroving my c++ skills with a project that uses PocoProject as a framework for supplying a simple rest webserver and json parsing as well as a mongodb driver.

I am compiling my project with cmake/make

my directory structure is:

root
 - pocoproject
 - helloworld.cpp
 - CMakeLists.txt

To test cmake, I have a simple cpp file (taken from the poco examples) that starts a http-server. the includes are the following:

#include "Poco/Net/HTTPServer.h"
#include "Poco/Net/HTTPRequestHandler.h"
...
#include "Poco/Util/HelpFormatter.h"
#include <iostream>

My CMakeLists.txt look like:

cmake_minimum_required (VERSION 2.6)
project (Hello)
add_executable(Hello helloworld.cpp)

Question:

How do I add the needed libraries from pocoproject to to my executable? How do I reference the headers correctly?


回答1:


You need let your CMake project know where to look for the dependencies, such as Poco. You need to set the variable CMAKE_PREFIX_PATH. It can be done with cmake-gui or on the command line:

cmake ... -DCMAKE_PREFIX_PATH=<path> ...

The <path> is where you copied or installed the Poco library, so the Poco headers will have paths like <path>/include/Poco/AbstractCache.h, etc..., libraries like <path>/lib/libPocoFoundation.* and most importantly, the config-module must be found at <path>/lib/cmake/Poco/PocoConfig.cmake.

In your CMakeLists.txt you need to find the Poco package with

find_package(Poco REQUIRED Foundation Util Net)

and link to it with

target_link_libraries(Hello Poco::Foundation Poco::Util Poco::Net)


来源:https://stackoverflow.com/questions/36122833/using-cmake-for-a-project-with-sub-external-projects-dependencies

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!