Unable to include HTTPSClientSession - c++

Preparation
I added poco to my project using:
git clone --recurse-submodules https://github.com/pocoproject/poco.git ThirdParty/poco
So my project structure looks like this:
ThirdParty/poco
include/
src/
main.cpp
CMakeLists.txt
My CMakeLists looks something like this :
cmake_minimum_required(VERSION 3.13.4 FATAL_ERROR)
project(sample)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/poco)
add_executable(runservice main.cpp)
target_link_libraries(runservice PUBLIC ${PROJECT_NAME} Poco::Net)
My main looks like this:
#include <iostream>
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPSClientSession.h>
using namespace std;
int main() {
//Code here
return 0;
}
Errors
I am able to include HTTPClientSession as <Poco/Net/HTTPClientSession.h> but when I include <Poco/Net/HTTPSClientSession.h>, it gives me the following error : fatal error: 'Poco/Net/HTTPSClientSession.h' file not found
Am I missing something in CMakeLists? I checked that HTTPSClientSession is not present in 'poco/Net', instead it's under 'poco/NetSSL_OpenSSL'.
So I tried to add Poco::NetSSL_OpenSSL to my target_include_libraries but then cmake is unable to generate build files correctly.
Any help would be really appreciated. Thank you for your time.

You need include_directories.
I'd also suggest to enclose the include paths in double quotes, rather than angle brackets.
But ... you'll be much better off starting with the cmake example, which will automagically take care of everything.
EDIT:
For cmake example:
target_link_libraries(pocoex PUBLIC Poco::Foundation Poco::XML Poco::JSON Poco::Util Poco::Net Poco::Crypto Poco::NetSSL)

Related

Unabled to load lohmann/json.hpp using CMake - getting fatal error: 'nlohmann/json.hpp' file not found

I have the following main.cpp, very simple script, trying to re-produce the problem and isolate to it's most basic.
#include<iostream>
#include<fmt/core.h>
// #include "json/json.hpp"
// #include <json/json.hpp>
// #include <nlohmann/json.hpp>
// #include "json.hpp"
// #include "nlohmann/json.hpp"
int main(){
fmt::print("Hello, world!\n");
return 0;
}
The commented out include statements are all of the paths I have tried to get this into my program.
My CMakeLists.txt file looks like this
cmake_minimum_required(VERSION 3.24)
project(main)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "-std=c++17")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build)
# include(FetchContent)
# FetchContent_Declare(
# json
# GIT_REPOSITORY https://github.com/nlohmann/json
# )
# FetchContent_MakeAvailable(json)
include(FetchContent)
FetchContent_Declare(fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt
GIT_TAG 9.0.0
)
FetchContent_MakeAvailable(fmt)
add_executable(main main.cpp)
# target_link_libraries(main json)
target_link_libraries(main fmt)
I've commented out the json part since it's not working obviously, I can also json-src and json-build in my _deps folder so I know for a fact that it is being downloaded into my local machine.
After running cmake CMakeLists.txt and make and then running the executable I get the error in the title 'nlohmann/json.hpp' file not found
Any help would be appreciated.
Tried to reproduce the example with another popular C++ package which worked fine. Tried changing the include statements to see if maybe I had something wrong since I don't fully understand all the nuance with CMake and am quite new to it.
You can either specifically use the interface target already included in the nlohmann library, which will automatically populate the correct include path for you, with:
target_link_libraries(main nlohmann_json::nlohmann_json)
Or you would need to specifically include the include path yourself:
include_directories(${json_SOURCE_DIR}/include)
With either way, you will be able to use #include <nlohmann/json.hpp> in your source.

Cpp file can find libary header file but header file can't using Cmake

FYI Im very new with cmake.
I have the following Structure in my project:
main.cpp
CMakeLists.txt
writer_pool/
- writer_pool.cpp
- writer_pool.h
- CMakeLists.txt
Both writer_pool.cpp and writer_pool.h have #include "json/json.h" from the JsonCpp package (https://github.com/open-source-parsers/jsoncpp). Except the issue is that the include statement only works in the writer_pool.cpp file and not writer_pool.h (gives json/json.h: No such file or directory). I.e. if I remove #include "json/json.h" from writer_pool.h the project compiles fine.
My CMakeLists.txt is:
cmake_minimum_required (VERSION 3.18)
project(pipeline)
add_subdirectory(writer_pool)
add_executable(${PROJECT_NAME} main.cpp)
find_package(jsoncpp REQUIRED)
target_link_directories(${PROJECT_NAME}
PUBLIC
writer_pool
)
target_link_libraries(${PROJECT_NAME}
PUBLIC
writer_pool
)
target_link_libraries(objects ${JSON_LIBRARIES})
target_include_directories(objects PUBLIC ${JSON_INCLUDE_DIRS})
target_compile_options(objects PUBLIC ${JSON_CFLAGS_OTHER})
and my writer_pool/CMakeLists.txt is:
add_library(objects objects.cpp)
I am completely stuck on how to get #include "json/json.h" in writer_pool.h and to actually compile...any help would be greatly appreciated!

CMake bracket #include grpc

I am trying to integrate grpc with a large project by trying to integrate the code in grpc example helloworld directory: greeter_client.cc
I used the cmake option where assuming grpc is already installed in my system in $MY_INSTALL_DIR as described in grpc's document: cmake -DCMAKE_PREFIX_PATH=$MY_INSTALL_DIR
When I include it in my "large project", I changed the greeter_client.cc into .h and .cc file.
greeter_client.h
#include <grpcpp/grpcpp.h>
#ifdef BAZEL_BUILD
#include "examples/protos/helloworld.grpc.pb.h"
#else
#include "helloworld.grpc.pb.h"
#endif
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using helloworld::HelloRequest;
using helloworld::HelloReply;
using helloworld::Greeter;
class GreeterClient {
public:
GreeterClient(std::shared_ptr<Channel> channel)
: stub_(Greeter::NewStub(channel)) {}
std::string SayHello(const std::string& user);
private:
std::unique_ptr<Greeter::Stub> stub_;
};
void libFun();
greeter_client.cc
#include <iostream>
#include <memory>
#include <string>
#include "greeter_client.h"
std::string GreeterClient::SayHello(const std::string& user) {
// the original implementation unchanged...
}
void libFun() {
GreeterClient client(grpc::CreateChannel(
"localhost:50051", grpc::InsecureChannelCredentials()));
client.SayHello("world");
}
and in the target destination (say the file is main.cc) I added
#include "/path/to/greeter_cient.h"
int main (){
libFun();
return 0;
}
The directory structure is
helloworld
greeter_client.h
greeter_client.cc
CMakeLists (the original one with little tweak to also include greeter_client.h in target greeter_client)
protos (the protos directory under examples in grpc project)
test
main.cc
CmakeList
The CMakeList for main.cc is like the following
cmake_minimum_required(VERSION 3.5.1)
project(test C CXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# This part is copied from the grpc example CMakeFileList
set(protobuf_MODULE_COMPATIBLE TRUE)
find_package(Protobuf CONFIG REQUIRED)
message(STATUS "Using protobuf ${Protobuf_VERSION}")
set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)
# Find gRPC installation
# Looks for gRPCConfig.cmake file installed by gRPC's cmake installation.
find_package(gRPC CONFIG REQUIRED)
message(STATUS "Using gRPC ${gRPC_VERSION}")
set(_GRPC_GRPCPP gRPC::grpc++)
add_subdirectory("../helloworld" "../helloworld")
add_executable(program "main.cc")
# THIS IS THE QUESTION: why adding ${_GRPC_GRPCPP} will work?
target_link_libraries(program greeter_client
${_GRPC_GRPCPP})
target_include_directories(program PRIVATE "../helloworld" )
The question is: initially, I did not add find_project(gRPC) in the main.cc 's CMakeList and I did not add ${_GRPC_GRPCPP} in the target_link_libraries of program target. And I will get error
complaining "/test/../helloworld/greeter_client.h:23:10: fatal error: 'grpcpp/grpcpp.h' file not found
#include <grpcpp/grpcpp.h>"
I read some threads and it seems that bracket include finds the files in system include paths. I am not sure why the grpc example can make it work by adding a dependency in target_link_libraries?
Another question is: ideally, I want main.cc does not care about anything inside greeter_client target. However, since I have to include greeter_client.h which in turn includes <grpcpp/grpcpp.h> I have to add the find_project and link the target in main.cc's CMakeList as well. How to avoid re-deal with this include <grpcpp/grpcpp.h> in main.cc's CMakeList?
Thanks!
If I understand you correctly, the real question here is
it seems that bracket include finds the files in system include paths.
I am not sure why the grpc example can make it work by adding a
dependency in target_link_libraries?
In short, #include <foo.h> will search system include paths first, but if that fails it will retry as if you wrote #include "foo.h".

Installing boost on macos

I am trying to create a simple example using the boost library. I can successfully use CMake for the initial setup and it finds boost.
using the following code in CMakeLists.txt:
cmake_minimum_required(VERSION 3.18)
project(edge_detector)
find_package(Boost REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(edge_detector main.cpp)
target_include_directories(edge_detector PUBLIC ${Boost_INCLUDE_DIRS})
target_link_libraries(edge_detector ${Boost_LIBRARIES})
However when I try to build the project using make or CMake --build . boost is not found and I am met with this error:
I am not sure what I am missing, I would be grateful for any help
Your include directive must include a file not a directory.
Replace
#include <boost/algorithm/string>
with
#include <boost/algorithm/string.hpp>

CMake find eigen incorrect results

I am trying to compile a code snippet with Eigen using CMake, but CMake seems to have trouble finding the correct path, even if the source code is there.
This is my CMakeLists.txt file.
cmake_minimum_required(VERSION 3.14.2)
project(test)
# set default build type
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
find_package(OpenCV 3 REQUIRED)
find_package (Eigen3 3.3 REQUIRED NO_MODULE)
include_directories(
include
${OpenCV_INCLUDE_DIRS}
${EIGEN3_INCLUDE_DIR}
)
add_executable(playground playground.cpp)
target_link_libraries(playground ${OpenCV_LIBS} Eigen3::Eigen)
This is how I import headers in playground.cpp:
#include <iostream>
#include <vector>
#include <map>
#include <math.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <string>
#include <Eigen>
My Eigen is installed under /usr/include/eigen3, here is the directory structure:
yuqiong#yuqiong-G7-7588:/usr/include/eigen3$ ls
Eigen signature_of_eigen3_matrix_library unsupported
So the correct path to Eigen library is /usr/include/eigen3/Eigen.
However, when I run cmake .. and then make for the aforementioned CMakeLists.txt file, with the -LH flag, CMake complains it can't find Eigen. This is the path it thinks where Eigen is:
yuqiong#yuqiong-G7-7588:/media/yuqiong/DATA/vignetting/catkin_ws/src/vig2/src/build$ cmake -LH ..
-- Configuring done
-- Generating done
-- Build files have been written to: /media/yuqiong/DATA/vignetting/catkin_ws/src/vig2/src/build
-- Cache values
// Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel ...
CMAKE_BUILD_TYPE:STRING=
// Install path prefix, prepended onto install directories.
CMAKE_INSTALL_PREFIX:PATH=/usr/local
// The directory containing a CMake configuration file for Eigen3.
Eigen3_DIR:PATH=/usr/local/share/eigen3/cmake
// The directory containing a CMake configuration file for OpenCV.
OpenCV_DIR:PATH=/opt/ros/kinetic/share/OpenCV-3.3.1-dev
This is the structure of the folder where it think Eigen is:
yuqiong#yuqiong-G7-7588:/usr/local/share/eigen3$ tree
.
└── cmake
├── Eigen3Config.cmake
├── Eigen3ConfigVersion.cmake
├── Eigen3Targets.cmake
└── UseEigen3.cmake
I am confused why CMake has such behavior? How can I let it find the correct path of Eigen in this case?
All this environment set-ups are so tricky to get it right...
Thanks for the comments above. I think the issue might be the Eigen team changed their directory structure somehow between different versions, and the old include path does not work anymore.
The solution for me was to change #include <Eigen/Dense> to #include <eigen3/Eigen/Dense>. After that g++ and CMake can find the path.
I did install Eigen following this official doc, which was why I was surprised the installation is still problematic.
This answer solved my problem.
This answer also solves the problem.
On another note, my CMake file was a bit messed up, with both include_directories and target_link_libraries.