简体   繁体   中英

c++14 - How to enable HTTPs using standalone libcurl library in windows

Problem: Libcurl works as a standalone library on windows, but doesn't support https protocol, how do I enable it? When I'm testing, or using my CurlClient, it works fine. When I try sending a request to a https site, I get the curl response code of 1, which means https is unsupported protocol.

My cmake logs for when I build curl:

-- curl version=[7.69.0-DEV]
CMake Warning at lib/curl/CMake/Macros.cmake:86 (message):
  Found no *nroff program
Call Stack (most recent call first):
  lib/curl/CMakeLists.txt:215 (curl_nroff_check)

-- Could NOT find LibSSH2 (missing: LIBSSH2_LIBRARY) (found version "1.8.2")
-- Enabled features: IPv6 AsynchDNS NTLM
-- Enabled protocols: DICT FILE FTP GOPHER HTTP IMAP LDAP POP3 RTSP SMB SMTP TELNET TFTP
-- Enabled SSL backends:

My project structure:

CMakeLists.txt
/lib
 /curl
 /googletest
 CMakeLists.txt
/src
 /include
    CurlClient.h
 CurlClient.cc
 CMakeLists.txt
/test
 CurlClientTest.cc
 main.cc
 CMakeLists.txt

src/CMakeLists.txt:

cmake_minimum_required(VERSION 3.10.2)

project(simple_curl_cpp)

set(BUILD_SHARED_LIBS OFF)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

add_subdirectory(src)
add_subdirectory(lib)
add_subdirectory(test)

lib/CMakeLists.txt:

cmake_minimum_required(VERSION 3.10.2)

message(STATUS "Not using system Curl, using built-in curl project instead.")
option(BUILD_TESTING "Set to ON to build cURL tests." OFF)
option(BUILD_CURL_EXE "Set to ON to build cURL executable." OFF)
add_subdirectory(curl)

set(gtest_force_shared_crt TRUE CACHE INTERNAL "")
set(BUILD_GMOCK TRUE CACHE INTERNAL "")
set(INSTALL_GTEST FALSE)
set(gmock_build_tests FALSE CACHE INTERNAL "")
add_subdirectory(googletest)

test/CMakeLists.txt:

cmake_minimum_required(VERSION 3.10.2)

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

set(SOURCES
    CurlClientTest.cc
    ../src/CurlClient.cc   
)

set(HEADERS
    ../src/include/CurlClient.h
)

# googletest
enable_testing()

add_executable(simple_curl_cpp_test "main.cc" ${SOURCES} ${HEADERS})
target_link_libraries(simple_curl_cpp_test libcurl gtest)

add_test(
    NAME simple_curl_cpp_test
    COMMAND simple_curl_cpp_test
)

ANSWER as given by Strager:

in lib/CMakeLists.txt type this before add_subdirectory(curl) :

option(CMAKE_USE_WINSSL "Set to ON to use WINSSL for windows." ON)

Try setting the CMAKE_USE_WINSSL CMake variable when you configure:

PS> "C:\Program Files\CMake\bin\cmake.exe" -DCMAKE_USE_WINSSL=ON ..
-- Building for: Visual Studio 16 2019
[snip]
-- Enabled features: SSL IPv6 AsynchDNS SSPI SPNEGO Kerberos NTLM
-- Enabled protocols: DICT FILE FTP FTPS GOPHER HTTP HTTPS IMAP IMAPS LDAP POP3 POP3S RTSP SMB SMBS SMTP SMTPS TELNET TFTP
-- Enabled SSL backends: WinSSL
[snip]
-- Configuring done
[snip]

Thanks @stranger, your answer was very helpfull for me, but unfortunatelly it isn't working, because in new curl version you need to set CURL_USE_SCHANNEL variable now. Add following line before adding curl:
option(CURL_USE_SCHANNEL "Use windows default SSL libraries" ON)

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