简体   繁体   中英

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. This is the code which uses curl library in C.

#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 :

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

And if I run the run.exe as such:

./run

According to the above mentioned link the output must be fetched webpage of https://www.google.com but it does nothing, absolutely nothing. 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:

#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? 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):

$ 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.

$ 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM