簡體   English   中英

如何在 Windows 的 Visual Studio Code 中使用 GCC 包含所需的 C 庫?

[英]How do I include a needed C library using GCC in Visual Studio Code on Windows?

我正在嘗試參考這個例子在 C 中編譯一個簡單的代碼。 這是在 C 中使用 curl 庫的代碼。

#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>

int main(void) {
     system("cls");
     CURL* curl = curl_easy_init();

     if (!curl) {
          fprintf(stderr, "[-] Failed Initializing Curl\n");
          exit(-1);
     }

     CURLcode res;
     curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com");
     res = curl_easy_perform(curl);

     if (res != CURLE_OK) {
          fprintf(stderr, "[-] Could Not Fetch Webpage\n[+] Error : %s\n", curl_easy_strerror(res));
          exit(-2);
     }

     curl_easy_cleanup(curl);
     return 0;
}

使用以下命令編譯此代碼以生成run.exe

gcc curl1.c -o run.exe -IC:/CURL/include -LC:/CURL/lib -l curl

如果我這樣運行run.exe

。/跑步

根據上面提到的鏈接,輸出必須是從https://www.google.com獲取的網頁,但它什么都不做,絕對什么都不做。 不是一行網頁,不是一行錯誤。

究竟是什么問題? 我無法弄清楚。 vs 代碼中的problems部分說:

#include errors detected. Please update your includePath. IntelliSense features for this translation unit (C:\Users\CPN\Desktop\LIBRARIES\libcurl\curl1.c) will be provided by the Tag Parser.
cannot open source file "curl/curl.h"

當它允許編譯和運行程序而不給出任何錯誤時,我無法識別它在說什么問題。 我該如何解決這個問題? 為此,我必須在 Vs Code 中設置一些額外的設置嗎? 如果是,請告訴我該怎么做。

您發布的程序從頭到尾運行完整。 接下來是它的輸出,在 Ubuntu 台式計算機上運行(我刪除了system("cls");調用,因為它是特定於 Windows 的,我在 Ubuntu 中沒有合適的替代品):

$ run
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="fi"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/logos/doodles/2022/seasonal-holidays-2022-6753651837109831.4-law.gif" itemprop="image"><meta content="Vuoden 2022 juhlapyh�t" property="twitter:title"><meta content=" " 

... <a lot of hidden output, several pages long> ... 

        </body></html>$ _    <<--- this is where shell prompt and cursor ends, as no final newline is included in the body of the HTML output.

我建議您將程序輸出重定向到一個文件,如下所示,以將正常的程序輸出保存到一個您可以檢查的文件中。 我不知道您無法獲得任何輸出的原因,但至少在 Linux 中,它會按照您所說的那樣運行。

$ run >file.out
$ ls -l file.out
-rw-rw-r-- 1 lcu lcu 49962 Dec 19 14:55 file.out
$ _

我還建議在開始時刪除system()調用,這樣它就不會干擾程序輸出(它不應該,因為它在整個過程之前執行,但試試看,看看會發生什么)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM