[英]Cannot properly use C++ static library in C program (Mingw64 with CLion)
[英]Cannot compile curl and libgcc as a static library in MinGW/CLion (c++)
我正在嘗試將 curl 和 libgcc 編譯為靜態庫,因此我不必在我的程序中包含 dll 文件。 當我通過命令提示符運行可執行文件時,我遇到了找不到libcurl.dll
問題。 此外,還有另一個錯誤libgcc_s_dw2-1.dll
not found。
當我將 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 的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
。 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_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/ 的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
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_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 的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
/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_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/ 的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
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/../../../ 的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
。 ./mingw32/b in/ld.exe: C:/curlx86/libcurl.a(http2.o):(.text+0x621): 對`nghttp2_session_send'的未定義引用
下載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:\\Users\\John\\Downloads\\openssl-1.1.1b\\openssl-1.1.1b"
在繼續之前,您必須將#include <windows.h>
添加到目錄下的這些文件中:
crypto\\dso\\dso_win32.c
crypto\\init.c
運行(這會將 openssl 編譯為靜態庫):
./configure --prefix=c:/OpenSSLx86 --openssldir=c:/OpenSSLx86 no-threads no-idea no-shared mingw
運行make clean
運行make depend
運行make
運行make install
下載並解壓curl包
更改目錄:
cd "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
OPENSSL_PATH = C:\\OpenSSLx86
:lib\\Makefile.m32
src\\Makefile.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")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.