【问题标题】:Cannot compile curl and libgcc as a static library in MinGW/CLion (c++)无法在 MinGW/CLion (c++) 中将 curl 和 libgcc 编译为静态库
【发布时间】:2019-10-12 18:51:06
【问题描述】:

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

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

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

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

我希望我的程序在不包含任何额外 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' 的引用




【问题讨论】:

  • 文件.dll.a动态库的导入文件,这不是静态库。你需要链接libcurl.a左右,没有.dll部分。
  • 嗨,我试过了,我在上面编辑了我的结果。使用libcurl.a 时出现很多错误。我不得不从上面的文本中删除大部分错误,因为它们有很多。我猜我错过了ssl 和其他类似的库。
  • 是的,当与 static 库链接时,您需要手动 链接给定库所依赖的所有库。与 dynamic 库不同,static 库不存储所需库的列表。

标签: c++ curl cmake mingw libcurl


【解决方案1】:

第 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:\Users\John\Downloads\openssl-1.1.1b\openssl-1.1.1b"
    

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

    • crypto\dso\dso_win32.c
    • crypto\init.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 构建 curl

  1. 下载并解压 curl

  2. 更改目录:

    cd "C:\Users\John\Downloads\curl\curl-7.65.0"
    
  3. 编辑lib\curl_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
  • lib\Makefile.m32
  • src\Makefile.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")

【讨论】:

    猜你喜欢
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-15
    • 2015-07-05
    • 1970-01-01
    • 2015-03-06
    • 1970-01-01
    相关资源
    最近更新 更多