Running libcurl code in C++ on Linux?

I am trying to run a code in C++ that will automatically save a specified web page from the internet. I am using the Linux Mint Operating System, and my code is being written through CodeBlocks. The below is the code I am attempting to use:

1 #include <stdio.h>
2 #include <curl/curl.h>
3
4 int main();
5
6 {
7 CURL *curl;
8 CURLcode res;
9
10 curl = curl_easy_init();
11 if(curl) {
12 curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
13 res = curl_easy_perform(curl);
14
15 /* always cleanup */
16 curl_easy_cleanup(curl);
17 }
18 return 0;
19 }
20 g++ simple.c -l curl

However, upon running this code I am receiving an error message for line 6: "error: expected unqualified-id before '{' token.

I would be grateful for help on getting the code to function properly. Thank you.
Line 4: int main();
Get rid of the semi colon :)
Thank you. However, when I did this, I now get the following error messages:

Line 8: Warning: variable 'res' set but not used [-Wunused-but-set-variable]
Line 20: error: 'g' does not name a type


1 #include <stdio.h>
2 #include <curl/curl.h>
3
4 int main()
5
6 {
7 CURL *curl;
8 CURLcode res;
9
10 curl = curl_easy_init();
11 if(curl) {
12 curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
13 res = curl_easy_perform(curl);
14
15 /* always cleanup */
16 curl_easy_cleanup(curl);
17 }
18 return 0;
19 }
20 g++ simple.c -lcurl
Wait is line 20 part of your code?
Yes, I am running a Linux system and I read somewhere that this line must be inserted if it is Linux for the code to run. This may not be the case though.

Also note that when I removed it and ran the code again, I got the following additional errors:

In function ‘int main()’:
In function `main':
undefined reference to `curl_easy_init'
undefined reference to `curl_easy_setopt'
undefined reference to `curl_easy_perform'
undefined reference to `curl_easy_cleanup'
Last edited on
closed account (Dy7SLyTq)
no. that has to be run to compile the code. it shouldnt be put in the code, unless its quoted or in a comment, or it will make an error. if you are writing a shell script or something like that then you need something like that
How are you compiling the code?
closed account (Dy7SLyTq)
@fafner: look at the last line of his code
Your issue is not specific to curl: the order of arguments to gcc is important: compiler options, then source files, then object files, then libraries (from high-level to low-level); so try to compile with

gcc -Wall -g prog.c -lcurl -o binprog
or (for a C++ program)

g++ -Wall -g otherprog.cc -lcurl -o binotherprog
Of course you need the development package e.g. libcurl-dev or libcurl3-gnutls-dev or libcurl4-gnutls-dev (packaged in Ubuntu); on your CentOS distribution it might be called libcurl-devel or something else.
closed account (Dy7SLyTq)
thats not true... i have compiled any number of different ways. it doesnt set the options as it goes throught out the program. it does it all at one time
Last edited on
But the order in which the libraries are listed is important, as I have recenty learnt. The GCC linker will only check libraries, as it processes them left to right, for the dependencies that it already knows about.

Andy

PS I usually use Visual C++, whose linker puts a lot more effort into finding dependencies...
The order of static libraries is important
I recently learned that the Java compiler and is very picky with order... Took me quite awhile to figure out why it wasn't setting my classpath -_-
closed account (Dy7SLyTq)
sorry yes library order can be important, like when compiling with sfml but i have compiled as $g++ foo.cpp -o bar and with $g++ -o bar -Wall -Wextra foo.cpp -o3 (<-- is that the optimization one? i havent had to use it in a while)
DTSCode wrote:
is that the optimization one? i havent had to use it in a while
Optimization level is -O.
closed account (Dy7SLyTq)
ah yes thats right. thank you. wasnt too important. i was just illustrating the relative order of most arguments isnt important
nobody reads the manual
You can mix options and other arguments. For the most part, the order you use doesn't matter. Order does matter when you use several options of the same kind; for example, if you specify -L more than once, the directories are searched in the order specified. Also, the placement of the -l option is significant.
Topic archived. No new replies allowed.