简体   繁体   中英

Convert compiler command line to CMakeLists.txt for Casablanca Rest SDK

I'm having a hard time converting a compiler command line call to a CMakeLists.txt, the goal is to use the Microsoft Rest SDK casablanca. Because I'm new to this, I have no idea what's wrong in my CMakeLists.txt

I successfully build using this (on macOS):

$ clang++ request.cpp -o request -std=c++11  -Wall   -stdlib=libc++ -I/usr/local/Cellar/cpprestsdk/2.9.1/include/ -I/usr/local/Cellar/openssl/1.0.2l/include/      -L/usr/local/Cellar/openssl/1.0.2l/lib/     -lssl  -lcrypto   -lcpprest   -lboost_system   -lboost_thread-mt   -lboost_chrono-mt 

BUT running cmake and make I get:

clang: warning: -lssl: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lcrypto: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lcpprest: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lboost_system: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lboost_thread-mt: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: -lboost_chrono-mt: 'linker' input unused [-Wunused-command-line-argument]
clang: warning: argument unused during compilation: '-L/usr/local/Cellar/openssl/1.0.2l/lib/' [-Wunused-command-line-argument]
/Users/mtobal/Documents/exercises/sandbox/request.cpp:2:10: fatal error: 'cpprest/http_client.h' file not found
#include <cpprest/http_client.h>
         ^~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/request.dir/request.cpp.o] Error 1
make[1]: *** [CMakeFiles/request.dir/all] Error 2
make: *** [all] Error 2

I know the issue is not finding the library, as I managed to solve the problem with the command line call but on CMakeLists.txt I didn't. Any thoughts?

CMakeLists.txt:

cmake_minimum_required (VERSION 2.8.11)
project (REST)

set(CMAKE_CXX_STANDARD 11)

set(CMAKE_CXX_COMPILER "clang++")

SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS} -stdlib=libc++ -I/usr/local/Cellar/openssl/1.0.2l/include/  -L/usr/local/Cellar/openssl/1.0.2l/lib/  -lssl  -lcrypto   -lcpprest   -lboost_system   -lboost_thread-mt   -lboost_chrono-mt ")

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++ -I/usr/local/Cellar/openssl/1.0.2l/include/  -L/usr/local/Cellar/openssl/1.0.2l/lib/  -lssl  -lcrypto   -lcpprest   -lboost_system   -lboost_thread-mt   -lboost_chrono-mt ")

include_directories("/usr/local/Cellar/cpprestsdk/2.9.1/include")
include_directories("/usr/local/Cellar/boost/1.64.0_1/include")
include_directories("/usr/local/Cellar/openssl/1.0.2l/include")

link_directories("/usr/local/Cellar/openssl/1.0.2l/lib/")

link_libraries(ssl crypto cpprest boost_system boost_thread-mt boost_chrono-mt)

#find_library(ssl crypto cpprest boost_system boost_thread-mt boost_chrono-mt)

set(SOURCE_FILES request.cpp)

add_executable (request ${SOURCE_FILES})

C++ code:

#include <iostream>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>

using namespace utility;                    // Common utilities like string conversions
using namespace web;                        // Common features like URIs.
using namespace web::http;                  // Common HTTP functionality
using namespace web::http::client;          // HTTP client features
using namespace concurrency::streams;       // Asynchronous streams

int main(){
    // std::cout << "Hello again!" << std::endl;

    auto fileStream = std::make_shared<ostream>();

    // Open stream to output file.
    pplx::task<void> requestTask = fstream::open_ostream(U("results.html"))
    .then([=](ostream outFile)
    {
        *fileStream = outFile;

        // Create http_client to send the request.
        http_client client(U("http://www.bing.com/"));

        // Build request URI and start the request.
        uri_builder builder(U("/search"));
        builder.append_query(U("q"), U("cpprestsdk github"));
        return client.request(methods::GET, builder.to_string());
    })
    // Handle response headers arriving.
    .then([=](http_response response)
    {
        printf("Received response status code:%u\n", response.status_code());

        // Write response body into the file.
        return response.body().read_to_end(fileStream->streambuf());
    })
    // Close the file stream.
    .then([=](size_t)
    {
        return fileStream->close();
    });

    // Wait for all the outstanding I/O to complete and handle any exceptions
    try
    {
        requestTask.wait();
    }
    catch (const std::exception &e)
    {
        printf("Error exception:%s\n", e.what());
    }
}

-- UPDATE -- Now trying with this (following Tsyvarev suggestion) some paths versions were wrong:

cmake_minimum_required (VERSION 2.8.11)
project (rest)

set(CMAKE_CXX_STANDARD 11)

set(CMAKE_CXX_COMPILER "g++")

include_directories("/usr/local/Cellar/cpprestsdk/2.10.2/include")
include_directories("/usr/local/Cellar/boost/1.66.0/include")
include_directories("/usr/local/Cellar/openssl/1.0.2l/include")

link_directories("/usr/local/Cellar/openssl/1.0.2l/lib/")
link_directories("/usr/local/Cellar/cpprestsdk/2.10.2/include")
link_directories("/usr/local/Cellar/boost/1.66.0/include")

link_libraries(ssl crypto cpprest boost_system boost_thread-mt boost_chrono-mt)

set(SOURCE_FILES request.cpp)

add_executable (request ${SOURCE_FILES})

Now I get:

[ 50%] Linking CXX executable request
ld: library not found for -lcpprest
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [request] Error 1
make[1]: *** [CMakeFiles/request.dir/all] Error 2
make: *** [all] Error 2

Alright guys so thanks to Tsyvarev, it builds now. As mentioned, it should point to lib for the link_directories. Also it's important to check the version of the software (libraries) installed, otherwise it doesn't find any library.

My final CMakeLists.txt to build on MacOS with clang is:

cmake_minimum_required (VERSION 2.8.11)
project (rest)

set(CMAKE_CXX_STANDARD 11)

set(CMAKE_CXX_COMPILER "clang++")

include_directories("/usr/local/Cellar/cpprestsdk/2.10.2/include")
include_directories("/usr/local/Cellar/boost/1.66.0/include")
include_directories("/usr/local/Cellar/openssl/1.0.2l/include")

link_directories("/usr/local/Cellar/openssl/1.0.2l/lib/")
link_directories("/usr/local/Cellar/cpprestsdk/2.10.2/lib")
link_directories("/usr/local/Cellar/boost/1.66.0/lib")

link_libraries(ssl crypto boost_system boost_thread-mt boost_chrono-mt cpprest)

set(SOURCE_FILES request.cpp)

add_executable (request ${SOURCE_FILES})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM