[英]How to include libssh2 to c project in visual studio code
我正在嘗試運行一個使用 libssh2 庫的簡單 c 程序。 對於安裝(構建+安裝),我使用了 cmake-gui(我使用的是 Windows 10 x64),openSSL 選項打開,zlib 關閉。
我正在嘗試不同的解決方案來包含具有正確依賴項的庫,但每次都會出現不同的錯誤消息。 喜歡:
當我只包含 ssh2 庫路徑時-I "C:\\\\Program Files (x86)\\\\libssh2\\\\include" -L "C:\\\\Program Files (x86)\\\\libssh2\\\\lib" -lssh2
錯誤信息:
C:\\Program Files (x86)\\libssh2\\lib/libssh2.a(openssl.c.obj):openssl.c:(.text+0x38): 對“BN_bn2bin”的未定義引用
C:\\Program Files (x86)\\libssh2\\lib/libssh2.a(openssl.c.obj):openssl.c:(.rdata$.refptr.PEM_read_bio_ECPrivateKey[.refptr.PEM_read_bio_ECPrivateKey]+0x0):未定義對'的引用PEM_read_bio_ECPrivateKey'
C:\\Program Files (x86)\\libssh2\\lib/libssh2.a(pem.c.obj):pem.c:(.text+0x6dd): 對“EVP_DigestUpdate”的未定義引用
C:\\Program Files (x86)\\libssh2\\lib/libssh2.a(bcrypt_pbkdf.c.obj):bcrypt_pbkdf.c:(.text+0x3bb):對“EVP_DigestUpdate”的未定義引用
C:\\Program Files (x86)\\libssh2\\lib/libssh2.a(crypt.c.obj):crypt.c:(.text+0x156): 對“EVP_CIPHER_CTX_free”的未定義引用
我沒有發布所有錯誤消息,但我注意到“()”中的那些庫像 crypt 和 openssl 一樣混亂。 所以我試圖通過將 openSSL 安裝目錄的路徑添加到編譯器參數中來包含它們: -I 'C:\\Program Files (x86)\\libssh2\\include' -IC:\\OpenSSL-Win64\\include -L 'C:\\Program Files (x86)\\libssh2\\lib' -LC:\\OpenSSL-Win64\\lib -lssh2 -lws2_32 -lcrypto -lssl
但它沒有用,因為他找不到這些庫:
找不到 -lcrypto
找不到 -lssl openSSL 安裝文件夾包含帶有 theesheaders 文件的包含文件夾和帶有 .lib 文件的 lib 文件夾(我不確定 100% 是否 mingw64 可以使用這些擴展名,或者它應該是 .a)。
也許之前有人問過這個問題,但經過幾天的搜索,我找不到適合我的解決方案。 很抱歉重復的帖子。
這是我的任務文件:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "Build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-6.2.0-win32-seh-rt_v5-rev1\\mingw64\\bin\\gcc.exe",
"args": [
"-Wall",
"-Wextra",
"-g",
"${file}",
"-I",
"C:\\Program Files (x86)\\libssh2\\include",
"-I",
"C:\\OpenSSL-Win64\\include",
"-L",
"C:\\Program Files (x86)\\libssh2\\lib",
"-L",
"C:\\OpenSSL-Win64\\lib",
"-lssh2",
"-lws2_32",
"-lcrypto",
"-lssl",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files\\mingw-w64\\x86_64-6.2.0-win32-seh-rt_v5-rev1\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"type": "shell",
"label": "Run active file",
"command":".\\${fileBasenameNoExtension}.exe"
}
]
}
我不確定您是否需要 c_cpp_properites 文件:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.10586.0",
"compilerPath": "\"C:/Program Files/mingw-w64/x86_64-6.2.0-win32-seh-rt_v5-rev1/mingw64/bin/gcc.exe\"",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x86"
}
],
"version": 4
}
您始終需要鏈接到您的應用程序或共享庫的依賴項(您使用-lssh2
),但是如果您正在進行靜態構建,您還需要包含依賴項的依賴項。
在 Linux 上或在 Windows 上使用 MSYS2 時,您可以使用pkg-config.exe
獲取實際鏈接參數,如下所示:
pkg-config.exe --static --libs libssh2
在我的情況下返回:
-LD:/Prog/winlibs64-9.2.0-7.0.0/custombuilt/lib -lssh2 -lws2_32 -lssl -lcrypto -lws2_32 -lgdi32 -lcrypt32 -lz
我找到了解決辦法。 我缺少 openSSL 庫的 .dll 文件。 dll 文件位於根文件夾(openSSL 文件夾)中,我必須將它們復制並發布到 lib 文件夾(openSSL/lib)中,並將名稱從 libcrypto-1_1-x64 更改為 libcrypto-1_1-x64,對於 ssl 也是如此,我雖然需要 openSSL 的 .a 文件,但因為我使用的是 Windows,所以 openSSL 安裝了 .lib 文件而不是 .a 並且 lib 文件需要 dll,這是我的理解。 就是這樣,我發布的任務文件是正確的。 您閱讀@Brecht Sanders 的答案以獲取更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.