cpprestsdk icon indicating copy to clipboard operation
cpprestsdk copied to clipboard

cannot compile sample on OSX

Open dcp12345678 opened this issue 9 years ago • 9 comments

I'm on OSX El Capitan, I did an install using the instructions located here.

$ brew install cpprestsdk

I copied the sample code from here and tried to compile it, but get this error:

usr/local/include/boost/asio/ssl/detail/openssl_types.hpp:20:10: fatal error: 'openssl/conf.h' file not found
#include <openssl/conf.h>

I tried adding -I and -L flags to the g++ arguments like this: g++ -std=c++11 test.cpp -I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib

But then I get a bunch more errors: (the first few are shown below)

Undefined symbols for architecture x86_64:
  "_CONF_modules_unload", referenced from:
      boost::asio::ssl::detail::openssl_init_base::do_init::~do_init() in test-72b3e4.o
  "_CRYPTO_cleanup_all_ex_data", referenced from:
      boost::asio::ssl::detail::openssl_init_base::do_init::~do_init() in test-72b3e4.o
  "_CRYPTO_num_locks", referenced from:
      boost::asio::ssl::detail::openssl_init_base::do_init::do_init() in test-72b3e4.o

I'm not sure how to proceed from here. Thanks for any help.

dcp12345678 avatar May 31 '17 13:05 dcp12345678

I met the same problem. What I did is to explicitly link to the openssl libs to solve this. Actually, in order to make the sample code work, I have to link to a bunch of libs from boost as well. I deem this is problem of cpprest. For sure, itself has dependencies on openssl and boost, but the sample code clearly does not rely on them directly. Why including a head file of cpprest would require me to link to openssl and boost explicitly?

bubbercn avatar Jun 13 '17 10:06 bubbercn

I you use VisualStudio Code for macOS this configuration worked for me:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "0.1.0",
    "command": "g++",
    "isShellCommand": true,
    "args": ["-o test","-Wall","-std=gnu++0x", "-stdlib=libc++", 
            "-I/usr/local/Cellar/cpprestsdk/2.9.1/include/",
            "-I/usr/local/Cellar/boost/1.64.0_1/include/",
            "-I/usr/local/Cellar/openssl/1.0.2l/include/", 
            "-L/usr/local/Cellar/openssl/1.0.2l/lib/",
            "-L/usr/local/Cellar/cpprestsdk/2.9.1/lib/",
            "-L/usr/local/Cellar/boost/1.64.0_1/lib/",
            "-lssl","-lcrypto", "-lcpprest", "-lboost_system", "-lboost_thread-mt", "-lboost_chrono-mt",
            "-g","casablanca_client.cpp"],
    "showOutput": "always",
    "echoCommand": true
}

to run the HTTP_Client example

ThorstenBux avatar Jun 14 '17 06:06 ThorstenBux

My apology to reactivate this rather old issue, the build is failing again with the macOS Catalina (Version 10.15). @BillyONeal

Here is what I did:

brew install cpprestsdk
mkdir ./build
cd ./build
cmake -DOPENSSL_ROOT_DIR=/usr/local/opt/openssl -DOPENSSL_LIBRARIES=/usr/local/Cellar/openssl/1.0.2s/lib ..
make

Here is my CMakeLists.txt

cmake_minimum_required(VERSION 3.7)
project(main)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

find_package(cpprestsdk REQUIRED)

add_executable(main main.cpp)
target_link_libraries(main PRIVATE cpprestsdk::cpprest)

It appears that

and here is my main.cpp

#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(int argc, char* argv[])
{
    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());
    }

    return 0;
}

And I have got this weirdo errors:

[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
make[2]: *** No rule to make target `/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/CoreFoundation.framework', needed by `main'.  Stop.
make[1]: *** [CMakeFiles/main.dir/all] Error 2
make: *** [all] Error 2

cshung avatar Oct 16 '19 06:10 cshung

I just received this today after a MacOS update to Catalina. I have the same error as cshung.

EdSwArchitect avatar Oct 28 '19 15:10 EdSwArchitect

This is probably the problem:

https://gitlab.kitware.com/cmake/cmake/issues/19824

EdSwArchitect avatar Oct 28 '19 15:10 EdSwArchitect

Has this been fixed ?

Clovel avatar Apr 05 '20 12:04 Clovel

Not as far as I know.

BillyONeal avatar Apr 06 '20 21:04 BillyONeal

I moved on, so I can't answer.

EdSwArchitect avatar Apr 06 '20 21:04 EdSwArchitect

I did this and that worked

brew install openssl
cd /usr/local/include 
ln -s ../opt/openssl/include/openssl .

taken from https://www.anintegratedworld.com/mac-osx-fatal-error-opensslsha-h-file-not-found/

domingguss avatar Jul 15 '20 22:07 domingguss