visual studio19 libcurl 빌드

libcurl

서버에 접속할 일이 생겨서 visual studio 19에서 libcurl을 사용해야됬습니다.
visual studio19에서 c++ 로 libcurl을 사용할 일이 없는지 오래된 글 밖에 없었고 버전 호환성 때문에 빌드에 실패했습니다.
결국 성공했는데 다른 분들은 고생하지 마시라고 21년 11월 기준 빌드를 씁니다.

libcurl을 빌드하기 전에 3개 프로그램이 필요합니다.
1. Perl
2. nasm
3. openssl

perl

perl이 있어야 Openssl을 빌드할 수 있습니다.
perl은 https://strawberryperl.com/ 링크에서 받는다.

64 비트 버전을 받으면 된다.

nasm

NASM은 암호화 성능을 개선하려고 부분적으로 어셈블리어를 사용한다고 합니다.
https://www.nasm.us/
다운 받고 nasm을 환경변수에 추가합니다.

openssl

openssl은 안정된 버전인 1.1.1l을 github에서 clone 합니다.

openssl을 빌드하려면 visual studio command prompt를 관리자 권한으로 실행한다.

x86은 32비트이고 x64는 64비트이니까 우리는 64비트로 엽니다.

clone한 디렉토리에 들어가서 정적 빌드 configure를 해줍니다.
1. x86 static library
perl Configure VC-WIN32 --openssldir=C:\OpenSSL-x86 no-shared no-asm threads no-idea no-mdc2 no-rc5
2. x86 static debug library
perl Configure debug-VC-WIN32 --openssldir=C:\OpenSSL-x86-debug no-shared no-asm threads no-idea no-mdc2 no-rc5

  1. x64 static library
    perl Configure VC-WIN64A --openssldir=C:\OpenSSL-x64 no-shared no-asm threads no-idea no-mdc2 no-rc5

  2. x64 static debug library
    perl Configure debug-VC-WIN64A --openssldir=C:\OpenSSL-x64-debug no-shared no-asm threads no-idea no-mdc2 no-rc5

※ 참고로 --openssldir=C:\OpenSSL 과 같이 입력하면 "C:\OpenSSL" 디렉토리에 라이브러리가 설치됨
※ no-idea no-mdc2 no-rc5 는 암호화 알고리즘 라이센스 문제로 인하여 빌드에서 제외

perl Configure VC-WIN64A --openssldir=C:\OpenSSL-x64 no-shared no-asm threads no-idea no-mdc2 no-rc5

입력 후 ENTER . 완료되면 nmake 로 빌드

빌드가 완료되면 nmake test 명령어로 테스트 수행 후 nmake install 명령어로 설치합니다.

설치 경로는 x64일 경우 C:\Program Files\OpenSSL 에 설치됩니다.

libcurl

libcurl github 에서 7.78을 clone합니다.

https://github.com/curl/curl/tree/curl-7_78_0

openssl을 지원하는 libcurl을 만들어주기 위해서 아래와 같은 디렉토리 위치에 deps , lib, include, bin 을 만듭니다.

If you wish to support zlib, openssl, c-ares, ssh2, you will have to download them separately and copy them to the deps directory as shown below:

somedirectory\
 |_curl-src
 | |_winbuild
 |
 |_deps
   |_ lib
   |_ include
   |_ bin

그리고 C:\Program Files\OpenSSL 위치에 있는 bin, include, lib의 내용을 모두 이름에 맞게 옮겨줍니다.

visiual studio command Prompt를 관리자 권한으로 실행해준다.( 64 비트 )

디렉토리 위치를 curl/winbuild 로 이동한다.

  • 빌드 명령어 + 추가 옵션
nmake /f Makefile.vc mode=<static or dll> <option>
  • 추가 옵션
where <options> is one or many of:

VC=<num> - VC version. 6 or later.
WITH_DEVEL=<path> - Paths for the development files (SSL, zlib, etc.) Defaults to sibbling directory deps: ../deps Libraries can be fetched at https://windows.php.net/downloads/php-sdk/deps/ Uncompress them into the deps folder.
WITH_SSL=<dll/static> - Enable OpenSSL support, DLL or static
WITH_NGHTTP2=<dll/static> - Enable HTTP/2 support, DLL or static
WITH_MBEDTLS=<dll/static> - Enable mbedTLS support, DLL or static
WITH_CARES=<dll/static> - Enable c-ares support, DLL or static
WITH_ZLIB=<dll/static> - Enable zlib support, DLL or static
WITH_SSH2=<dll/static> - Enable libSSH2 support, DLL or static
WITH_PREFIX=<dir> - Where to install the build
ENABLE_SSPI=<yes/no> - Enable SSPI support, defaults to yes
ENABLE_IPV6=<yes/no> - Enable IPv6, defaults to yes
ENABLE_IDN=<yes or no> - Enable use of Windows IDN APIs, defaults to yes Requires Windows Vista or later
ENABLE_SCHANNEL=<yes/no> - Enable native Windows SSL support, defaults to yes if SSPI and no other SSL library
ENABLE_OPENSSL_AUTO_LOAD_CONFIG=<yes/no> - Enable loading OpenSSL configuration automatically, defaults to yes
ENABLE_UNICODE=<yes/no> - Enable UNICODE support, defaults to no
GEN_PDB=<yes/no> - Generate Program Database (debug symbols for release build)
DEBUG=<yes/no> - Debug builds
MACHINE=<x86/x64> - Target architecture (default is x86)
CARES_PATH=<path> - Custom path for c-ares
MBEDTLS_PATH=<path> - Custom path for mbedTLS
NGHTTP2_PATH=<path> - Custom path for nghttp2
SSH2_PATH=<path> - Custom path for libSSH2
SSL_PATH=<path> - Custom path for OpenSSL
ZLIB_PATH=<path> - Custom path for zlib

nmake /f Makefile.vc mode=static VC=17 WITH_DEVEL=../deps MACHINE=x64 WITH_PREFIX=builds

위 명령어는 정적 라이브러리, VC 17 버전, deps 디렉토리에 openssl 지원, 64 비트 머신, 저장 위치 winbuild/builds 이다.

ENTER 하면 winbuild/builds에 bin , lib, include 디렉토리가 생성된다. 이걸 프로젝트에 적용하면된다.

Visual studio 19 켜준다.

프로젝트를 하나 만들고 다음과 같은 코드 작성

#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
    CURL* curl;
    CURLcode res;

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "https://google.com");
        /* example.com is redirected, so we tell libcurl to follow redirection */
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);
        /* Check for errors */
        if (res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(res));

        /* always cleanup */
        curl_easy_cleanup(curl);
    }
    return 0;
}

프로젝트 속성에 들어간다.

  1. Debug 플랫폼 x64 변경

  2. 구성 Debug

  3. VC++ 디렉토리 들어가서 포함 디렉터리에 winbuild/builds/include 추가 , 라이브러리 디렉토리에 winbuild/builds/lib 추가

  4. c / c++ 들어가서 전처리기에 전처리기 정의에 CULR_STATICLIB 추가

  5. 링커 들어가서 일반에서 추가 라이브러리 디렉토리 winbuild/builds/lib 추가

  6. 링커 입력에서 추가 종속성에

    libcurl_a.lib
    libssl.lib
    ws2_32.lib
    crypt32.lib
    Wldap32.lib
    Normaliz.lib

    추가

  7. 구성 release에도 위의 방식을 반복한다.

빌드 성공

좋은 웹페이지 즐겨찾기