无法将 curl 和 libgcc 编译为 MinGW/CLion (c++) 中的静态库

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

本文关键字:c++ CLion 静态 MinGW curl libgcc 编译      更新时间:2023-10-16

我正在尝试将curl和libgcc编译为静态库,这样我就不必在我的程序中包含dll文件。我在通过命令提示符运行可执行文件时找不到libcurl.dll问题。此外,还有另一个错误libgcc_s_dw2-1.dll未找到。

当我将dll文件放在与可执行文件相同的文件夹中时,它可以工作,但这不是我真正喜欢的。我按照此处的建议-DCURL_STATICLIB放入CMAKE_CXX_FLAGS

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

这是我的CMakeList.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")

我希望我的程序在不包含任何额外 dll 文件的情况下运行。我该怎么办?

Edit当我使用target_link_libraries(ham "C:/curlx86/libcurl.dll.a")

我收到这些错误。

c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:/curlx86/libcurl.a(easy.o):(.text+0x8f): 对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): 对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): 未定义对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):未定义对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): 对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): 对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):对"nghttp2_session_send"的未定义引用



1 部分。设置 openssl v1.1.1b

  1. 下载openssl v1.1.1b软件包(不是安装程序)

  2. 下载并安装MSYS2

  3. 打开msys.exe(不是mingw32.exemingw.exe)

  4. 运行以下命令以安装 perl 和其他软件包:

    pacman -S make perl msys2-devel libcrypt-devel perl-CPAN mingw-w64-i686-toolchain
    
  5. 现在关闭msys.exe并打开mingw32.exe

  6. 将目录更改为您提取openssl软件包的位置,在我的情况下是这样的:

    cd "C:UsersJohnDownloadsopenssl-1.1.1bopenssl-1.1.1b"
    

    在继续之前,您必须将#include <windows.h>添加到目录下的这些文件:

    • cryptodsodso_win32.c
    • cryptoinit.c
  7. 运行(这将openssl编译为静态库):

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

  9. 运行make depend

  10. 运行make

  11. 运行make install

第 2 部分。使用 openssl v1.1.1b 构建卷曲

  1. 下载并解压缩卷

  2. 更改目录:

    cd "C:UsersJohnDownloadscurlcurl-7.65.0"
    
  3. 编辑libcurl_setup.h并添加以下行,以通过仅选择要使用的选项来减小已编译库的大小。我将仅使用 SMTP 协议,因此我禁用了其他协议。

#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. 在以下文件中设置OPENSSL_PATH = C:OpenSSLx86
  • libMakefile.m32
  • srcMakefile.m32
  1. 运行make mingw32-ssl

  2. 现在libcurl.alib复制到C:curlx86,您就可以开始了。

这是我最后的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")