无法将 curl 和 libgcc 编译为 MinGW/CLion (c++) 中的静态库
Cannot compile curl and libgcc as a static library in MinGW/CLion (c++)
我正在尝试将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 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 部分。设置 openssl v1.1.1b
下载openssl v1.1.1b软件包(不是安装程序)
下载并安装MSYS2
打开
msys.exe
(不是mingw32.exe
或mingw.exe
)运行以下命令以安装 perl 和其他软件包:
pacman -S make perl msys2-devel libcrypt-devel perl-CPAN mingw-w64-i686-toolchain
现在关闭
msys.exe
并打开mingw32.exe
将目录更改为您提取openssl软件包的位置,在我的情况下是这样的:
cd "C:UsersJohnDownloadsopenssl-1.1.1bopenssl-1.1.1b"
在继续之前,您必须将
#include <windows.h>
添加到目录下的这些文件:cryptodsodso_win32.c
cryptoinit.c
运行(这将openssl编译为静态库):
./configure --prefix=c:/OpenSSLx86 --openssldir=c:/OpenSSLx86 no-threads no-idea no-shared mingw
运行
make clean
运行
make depend
运行
make
运行
make install
第 2 部分。使用 openssl v1.1.1b 构建卷曲
下载并解压缩卷曲包
更改目录:
cd "C:UsersJohnDownloadscurlcurl-7.65.0"
编辑
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
- 在以下文件中设置
OPENSSL_PATH = C:OpenSSLx86
:
libMakefile.m32
srcMakefile.m32
运行
make mingw32-ssl
现在
libcurl.a
从lib
复制到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")
- 无法在 CLion 中构建 C++ 项目
- C++:如何在CLion IDE中安装Boost
- 如何使用CLion在Mac上创建一个新的.txt文件
- IDE (CLion) 无法解析C++模板类型
- Clion显示错误,但可以使用Cmake成功构建代码
- 如何将 CLion C++ 项目导入 Visual Studio?
- Boost.TEST with CLion: "Test framework quit unexpectedly"
- clion 无法解析 std 类型
- 在装有 CLion 的 Mac 上使用 C++ 连接到 MySQL 数据库时出现问题
- 如何让 CLion 显示异常?
- CLion 无法在 macOS 上找到 Eigen3
- 如何将 OpenMP 和 MPI 导入到大型 CLion CMake 项目中?
- 当我在 CLion 中读取数组中的 txt 文件时C++编码问题
- CLion - 无法解决类中对构造函数的未定义引用
- Linking SDL2 - CLion - Ubuntu 16.04 - G++
- fdeclspec not enabled -- cLion Mac
- CLion、MinGW 和 SDL2:进程已完成,退出代码为 -1073741515 (0xC0000135)
- 无法在 CLion 中编译 OpenCV 头文件
- 如何使用CLion和MSVC编译器工具链编译wxWidgets Hello World应用程序而没有错误?
- 程序在CLion IDE中工作,但exe不起作用