[英]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.