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

时间:2019-05-27 07:58:20

标签: c++ curl cmake mingw libcurl

我正在尝试将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 to libssh2_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 to nghttp2_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 to nghttp2_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 to nghttp2_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 to nghttp2_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 to nghttp2_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 个答案:

答案 0 :(得分:0)

花了一整天的时间弄清楚了,但是我终于能够使用openssl v1.1.1b作为静态库来编译curl。来找的任何人都可以按照以下步骤操作。

第1部分(设置openssl v1.1.1b)

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

2)下载并安装Msys2

3)打开msys.exe(不是mingw32.exe或mingw.exe)

4)运行以下命令以安装perl和其他软件包(不带引号):     "pacman -S make perl msys2-devel libcrypt-devel perl-CPAN mingw-w64-i686-toolchain"

5)现在关闭msys.exe并打开mingw32.exe

6)在解压缩openssl包的任何地方更改目录(在我的情况下为this):     cd "C:\Users\John\Downloads\openssl-1.1.1b\openssl-1.1.1b"

Before continuing further you have to open the files below with any text editor like Notepad++
and add "#include <windows.h>" to each of them, without this you will run into errors:
"C:\Users\John\Downloads\openssl-1.1.1b\openssl-1.1.1b\crypto\dso\dso_win32.c"
"C:\Users\John\Downloads\openssl-1.1.1b\openssl-1.1.1b\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)转到此位置:"C:\Users\John\Downloads\curl\curl-7.65.0\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

3)转到这两个文件,并将OPENSSL_PATH变量编辑为"OPENSSL_PATH = C:\OpenSSLx86"

"C:\Users\John\Downloads\curl\curl-7.65.0\lib\Makefile.m32"

"C:\Users\John\Downloads\curl\curl-7.65.0\src\Makefile.m32"

4)运行:cd "C:\Users\John\Downloads\curl\curl-7.65.0"

5)运行:make mingw32-ssl

6)现在,将libcurl.a中的C:\Users\John\Downloads\curl\curl-7.65.0\lib复制到C:\curlx86,一切顺利。

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")