[英]How do I include a needed C library using GCC in Visual Studio Code on Windows?
I am trying to compile a simple code in C with reference to this example.我正在尝试参考这个例子在 C 中编译一个简单的代码。 This is the code which uses curl library in 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;
}
Using the following command to compile this code to produce run.exe
:使用以下命令编译此代码以生成
run.exe
:
gcc curl1.c -o run.exe -IC:/CURL/include -LC:/CURL/lib -l curl
gcc curl1.c -o run.exe -IC:/CURL/include -LC:/CURL/lib -l curl
And if I run the run.exe
as such:如果我这样运行
run.exe
:
./run
。/跑步
According to the above mentioned link the output must be fetched webpage of https://www.google.com
but it does nothing, absolutely nothing.根据上面提到的链接,输出必须是从
https://www.google.com
获取的网页,但它什么都不做,绝对什么都不做。 Not a single line of webpage, not a single line of error.不是一行网页,不是一行错误。
What exactly is the problem?究竟是什么问题? I am not able to figure out.
我无法弄清楚。 The
problems
section in vs code is saying: 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"
I can not recognize what the problem it is talking while it lets to compile and run the program without giving any errors.当它允许编译和运行程序而不给出任何错误时,我无法识别它在说什么问题。 How do I resolve this?
我该如何解决这个问题? Is there some extra setting that I have to set in Vs Code for this?
为此,我必须在 Vs Code 中设置一些额外的设置吗? If yes then please tell me how to do this.
如果是,请告诉我该怎么做。
The program you post runs complete, from beginning to end.您发布的程序从头到尾运行完整。 Next is the output from it, running on an Ubuntu desktop computer (I have deleted the
system("cls");
call, as it is windows specific, and I don't have proper substitute in Ubuntu):接下来是它的输出,在 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.
I recommend you to redirect program output to a file, as shown next, to save the normal program output into a file you can inspect.我建议您将程序输出重定向到一个文件,如下所示,以将正常的程序输出保存到一个您可以检查的文件中。 I don't know the reason you cannot get any output, but, at least in Linux, it runs as you told.
我不知道您无法获得任何输出的原因,但至少在 Linux 中,它会按照您所说的那样运行。
$ run >file.out
$ ls -l file.out
-rw-rw-r-- 1 lcu lcu 49962 Dec 19 14:55 file.out
$ _
I recommend also to erase the system()
call at the beginning, so it doesn't interfere in the program output (it shouldn't, as it is executed before the whole stuff, but try it, and see what happens)我还建议在开始时删除
system()
调用,这样它就不会干扰程序输出(它不应该,因为它在整个过程之前执行,但试试看,看看会发生什么)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.