0

I am trying to setup a Http client and download some data using the example shown here.

When I try to run the program, it fails saying "conf.h" is not found.

$g++ -std=c++11 BingSearch.cpp
In file included from BingSearch.cpp:1:
In file included from /usr/local/include/cpprest/http_client.h:53:
In file included from /usr/local/include/boost/asio/ssl.hpp:19:
In file included from /usr/local/include/boost/asio/ssl/context.hpp:27:
In file included from /usr/local/include/boost/asio/ssl/context_base.hpp:19:
/usr/local/include/boost/asio/ssl/detail/openssl_types.hpp:20:10: fatal error: 'openssl/conf.h' file not found
#include <openssl/conf.h>
         ^
1 error generated.

I looked at other SO posts and I have set the necessary path variables.

$echo $LDFLAGS
-L/usr/local/opt/openssl/lib
$echo $CPPFLAGS
-I/usr/local/opt/openssl/include
$echo $PKG_CONFIG_PATH
/usr/local/opt/openssl/lib/pkgconfig

Also conf.h is located inside openssl directory

openssl $find /usr/local/opt/openssl/ -name "conf.h"
/usr/local/opt/openssl//include/openssl/conf.h

Is there something else that needs to be done to compile the below file? It is got from here.

#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;
}
Anit
  • 345
  • 1
  • 5
  • 19
  • "Is there something else that needs to be done to compile this file?" : How would we know when you don't show us "this file"? Please post a [mcve]. – Jesper Juhl Jul 05 '17 at 23:30
  • It is the same file that is there at https://github.com/Microsoft/cpprestsdk/wiki/Getting-Started-Tutorial Sorry I missed to add the code as I had added a link which has the complete code. – Anit Jul 05 '17 at 23:35
  • Can you please explain the reason for the downvote? – Anit Jul 05 '17 at 23:35
  • 1
    Please show the command line used to compile your program or library. `CPPFLAGS` is the C preprocessor. You should add `-I/usr/local/opt/openssl/include` to `CFLAGS` (C options) and `CXXFLAGS` (C++ options). Its the compiler driver's job to pass arguments down to the C preprocessor; not vice versa. If it literally is `g++ -std=c++11 BingSearch.cpp`, then use `g++ $CXXFLAGS -std=c++11 BingSearch.cpp` – jww Jul 05 '17 at 23:48
  • Also see [Homebrew refusing to link OpenSSL](http://stackoverflow.com/q/38670295), [Update OpenSSL on OS X with Homebrew](http://stackoverflow.com/q/15185661), [How to install latest version of openssl Mac OS X El Capitan](http://stackoverflow.com/q/35129977), [How to upgrade OpenSSL in OS X?](http://apple.stackexchange.com/q/126830), [Openssl installation using HomeBrew fails](http://superuser.com/q/486389), etc. – jww Jul 05 '17 at 23:48
  • @jww Thanks! I am sure how if I am running the program correctly. The example link does not say how to run the program. So I am running it as any other C++ program "g++ -std=c++11 BingSearch.cpp". Please let me know if there is an alternate way to run it. – Anit Jul 06 '17 at 00:06

1 Answers1

2

I tried @jww's suggestion as below.

g++ -std=c++11 BingSearch.cpp -lssl -lcrypto -L/usr/local/opt/openssl/lib -I/usr/local/opt/openssl/include

This resulted in overcoming 'conf.h' file not found error.

But it still failed with the below error.

Undefined symbols for architecture x86_64:
  "web::uri_builder::append_query(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool)", referenced from:
      web::uri_builder& web::uri_builder::append_query<char [18]>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char const (&) [18], bool) in BingSearch-582644.o

I followed https://github.com/Microsoft/cpprestsdk/issues/264 and it resolved the problem.

I am able to compile the program using the below command.

g++ -std=c++11 BingSearch.cpp -stdlib=libc++ -lcpprest -lssl -lcrypto -lboost_system -lboost_thread-mt -lboost_chrono-mt -L/usr/local/opt/openssl/lib -I/usr/local/opt/openssl/include

My PATH variable is set as follows.

$echo $PATH
/usr/local/opt/openssl/bin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Anit
  • 345
  • 1
  • 5
  • 19