Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made the library installable #28

Merged
merged 11 commits into from
Oct 29, 2017
Merged

Made the library installable #28

merged 11 commits into from
Oct 29, 2017

Conversation

01e9
Copy link
Contributor

@01e9 01e9 commented Oct 28, 2017

Now the library can be installed as a cmake external project or directly in os.

Also it creates a cmake package which can be used like this:

find_package(restc-cpp REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE restc-cpp)

Test

Create a project with these 2 files:

CMakeLists.txt

cmake_minimum_required(VERSION 3.8)
project(restc_cpp_test)

set(EXTERNAL_PROJECTS_PREFIX ${CMAKE_BINARY_DIR}/external-projects)
set(EXTERNAL_PROJECTS_INSTALL_PREFIX ${EXTERNAL_PROJECTS_PREFIX}/installed)
include(ExternalProject)
ExternalProject_Add(externalRestcCpp
    PREFIX "${EXTERNAL_PROJECTS_PREFIX}"
    GIT_REPOSITORY "https://github.com/arteniioleg/restc-cpp.git"
    GIT_TAG "master"
    CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_PROJECTS_INSTALL_PREFIX}
    )
if(NOT EXISTS "${EXTERNAL_PROJECTS_INSTALL_PREFIX}/include")
    # Happens on first CMake run.
    # Can't continue because the below `find_package(REQUIRED)` will fail.
    # Build all external project targets then rerun CMake and build the project target.
    message(AUTHOR_WARNING "Build all external projects then reload cmake.")
    return()
endif()

add_executable(${PROJECT_NAME} main.cpp)
set_target_properties(${PROJECT_NAME} PROPERTIES
    CXX_STANDARD 14
    CXX_STANDARD_REQUIRED YES
    CXX_EXTENSIONS NO
    )

add_dependencies(${PROJECT_NAME} externalRestcCpp)
find_package(restc-cpp REQUIRED HINTS ${EXTERNAL_PROJECTS_INSTALL_PREFIX})
target_link_libraries(${PROJECT_NAME} PRIVATE restc-cpp)

main.cpp

#include <iostream>
#include <restc-cpp/restc-cpp.h>

int main()
{
    auto rest_client = restc_cpp::RestClient::Create();
    std::string body;

    rest_client->Process([&body](restc_cpp::Context& ctx){
        auto reply = ctx.Get("https://github.com/jgaa/restc-cpp/raw/master/.gitignore");
        body = reply->GetBodyAsString();
    });
    rest_client->CloseWhenReady(true);

    std::cout << "TEST OK" << std::endl << body << std::endl;

    return 0;
}

Build

  1. mkdir build
  2. cd build
  3. cmake ..
  4. cmake --build . (build all external projects)
  5. cmake .. (this will pass return() and will add_executable())
  6. cmake --build . --target restc_cpp_test
  7. ./restc_cpp_test

@01e9 01e9 mentioned this pull request Oct 28, 2017
@jgaa
Copy link
Owner

jgaa commented Oct 28, 2017

Thanks for the patch.
I will test it tomorrow on all the supported platforms, and update the documentation before I merge it to master.

Jarle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants