John Byron
John Byron

Reputation: 49

Cannot compile curl and libgcc as a static library in MinGW/CLion (c++)

I am trying to compile curl and libgcc as a static library so I would not have to include dll files with my program. I am having problems with libcurl.dll not found when I run the executable through command prompt. Also, there is another error libgcc_s_dw2-1.dll not found.

It works when I put the dll files in the same folder as executable but that is not what I am really fond of. I have put -DCURL_STATICLIB in CMAKE_CXX_FLAGS as suggested here:

https://curl.haxx.se/docs/install.html

Here is my CMakeLists.txt

project(ham)

set(CMAKE_CXX_STANDARD 14)

set(CMAKE_C_FLAGS -m32 -DCURL_STATICLIB)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32 -std=c++14 -LC:/curlx86 -lcurl")

set(CMAKE_CXX_STANDARD_LIBRARIES "${CMAKE_CXX_STANDARD_LIBRARIES} -static-libgcc -static-libstdc++ -lwsock32 -lws2_32 -lwinmm")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Bstatic,--whole-archive -Wl,--no-whole-archive")


project(untitled3)
add_executable(ham Ham/ham.cpp)

target_link_libraries(ham "C:/curlx86/libcurl.dll.a")

I want my program to run without including any extra dll files. What should I do ?

Edit When I use target_link_libraries(ham "C:/curlx86/libcurl.dll.a")

I get these errors.

c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(easy.o):(.text+0x8f): undefined reference to libssh2_init' c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(easy.o):(.text+0x16b): undefined reference tolibssh2_exit' c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0xcd): undefined reference to nghttp2_version' c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0x2ca): undefined reference tonghttp2_submit_rst_stream' c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0x2dc): undefined reference to nghttp2_session_send' c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0x31d): undefined reference tonghttp2_session_set_stream_user_data' c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0x3c0): undefined reference to nghttp2_pack_settings_payload' c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0x4bd): undefined reference tonghttp2_session_resume_data' c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0x4fe): undefined reference to nghttp2_session_mem_recv' c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0x51c): undefined reference tonghttp2_strerror' c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0x5c3): undefined reference to nghttp2_priority_spec_init' c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0x614): undefined reference tonghttp2_submit_priority' c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0x621): undefined reference to `nghttp2_session_send'




Upvotes: 1

Views: 1709

Answers (1)

John Byron
John Byron

Reputation: 49

Part 1. Setting up openssl v1.1.1b

  1. Download the openssl v1.1.1b package (not the installer)

  2. Download and install MSYS2

  3. Open msys.exe (not mingw32.exe or mingw.exe)

  4. Run this command to install perl and other packages:

    pacman -S make perl msys2-devel libcrypt-devel perl-CPAN mingw-w64-i686-toolchain
    
  5. Now close msys.exe and open mingw32.exe

  6. Change directory to wherever you extracted the openssl package, in my case it's this:

    cd "C:\Users\John\Downloads\openssl-1.1.1b\openssl-1.1.1b"
    

    Before continuing any further you have to add #include <windows.h> to these files underneath the directory:

    • crypto\dso\dso_win32.c
    • crypto\init.c
  7. Run (this compiles openssl as a static library):

    ./configure --prefix=c:/OpenSSLx86 --openssldir=c:/OpenSSLx86 no-threads no-idea no-shared mingw
    
  8. Run make clean

  9. Run make depend

  10. Run make

  11. Run make install

Part 2. Building curl with openssl v1.1.1b

  1. Download and extract the curl package

  2. Change directory:

    cd "C:\Users\John\Downloads\curl\curl-7.65.0"
    
  3. Edit lib\curl_setup.h and add the following lines to reduce the size of the compiled library by choosing only the options you will use. I will be using only the SMTP protocol, so I am disabling the others.

#define CURL_DISABLE_TFTP
#define CURL_DISABLE_FTP
#define CURL_DISABLE_LDAP
#define CURL_DISABLE_TELNET
#define CURL_DISABLE_DICT
#define CURL_DISABLE_FILE
#define CURL_DISABLE_RTSP
#define CURL_DISABLE_POP3
#define CURL_DISABLE_IMAP
//#define CURL_DISABLE_SMTP
#define CURL_DISABLE_GOPHER
#define CURL_DISABLE_SMB
  1. Set OPENSSL_PATH = C:\OpenSSLx86 in these files:
  • lib\Makefile.m32
  • src\Makefile.m32
  1. Run make mingw32-ssl

  2. Now copy libcurl.a from lib to C:\curlx86 and you are good to go.

Here is my final CMakeLists.txt:

cmake_minimum_required(VERSION 3.3)     #curl
project(ham)

set(CMAKE_CXX_STANDARD 14)

add_compile_definitions(
        CURL_STATICLIB
        WITH_SSL=STATIC
)
#CURL_DISABLE_SMTP

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32 -LC:/curlx86 -lcurl -LC:/OpenSSLx86/lib -lssl -LC:/OpenSSLx86/lib -lcrypto")

set(CMAKE_CXX_STANDARD_LIBRARIES "${CMAKE_CXX_STANDARD_LIBRARIES} -static-libgcc -static-libstdc++ -lwsock32 -lws2_32 -lwinmm -lcrypt32")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive") 


project(untitled3)
add_executable(ham Ham/ham.cpp)

target_link_libraries(ham "C:/curlx86/libcurl.a"  "C:/OpenSSLx86/lib/libssl.a" "C:/OpenSSLx86/lib/libcrypto.a")

Upvotes: 2

Related Questions