简体   繁体   English

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

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

相关问题 如何使用 GCC 包含所需的 C 库? - How can I include a needed C library using GCC? 如何在代码块(Windows)的C项目中包含curl库? - How do I include curl library in my C project in Code Blocks (Windows)? C代码可在Windows(使用Visual Studio编译)上运行,但不能在Linux(使用gcc)上编译 - C code works on Windows (compiled with Visual Studio) but won't compile on Linux (using gcc) 为什么我从在 gcc 中运行的 C 代码和运行它的 Visual Studio 得到不同的结果 - Why do I get different results from C code running in gcc and Visual Studio running it 在Windows上使用Visual Studio Code调试C - Debugging C using Visual Studio Code on Windows 如何在Windows 7上使用Visual Studio的编译器使用Vim的&#39;make&#39;命令编译C程序? - How do I compile C programs using Vim's 'make' command with Visual Studio's compiler on Windows 7? 如何使用“静态链接”和“动态链接”与gcc和Visual Studio构建C / C ++程序? - How can I build a C/C++ program using `static linking` & `Dynamic linking` with gcc & Visual studio? 如何在 Visual Studio 代码中调试从 f2py 接口调用共享库中的 c 函数的 python 程序 - How do I debug in visual studio code a python program that calls c functions in a shared library from a f2py interface 如何使用GCC(在Windows上)正确包含libcurl? - How can I include libcurl correctly using GCC (on Windows)? 每个文件都需要 Visual Studio #include “file.c” - Visual studio #include “file.c” needed in every file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM