我如何在Windows上使用Visual Studio代码中的GCC包含所需的C库?



我试图在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 Curln");
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 Webpagen[+] Error : %sn", curl_easy_strerror(res));
exit(-2);
}
curl_easy_cleanup(curl);
return 0;
}

使用以下命令编译此代码以生成run.exe:

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

如果我这样运行run.exe:

。/运行

根据上面提到的链接,输出的应该是https://www.google.com的网页,但是它什么也没做,绝对没有。没有一行网页,没有一行错误。

到底是什么问题?我想不出来。vscode中的problems部分说:

#include errors detected. Please update your includePath. IntelliSense features for this translation unit (C:UsersCPNDesktopLIBRARIESlibcurlcurl1.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()调用,因此它不会干扰程序输出(它不应该,因为它在整个东西之前执行,但尝试一下,看看会发生什么)

最新更新