I am trying to use a C ++ program to use libcurl and cannot figure it out. When developing in C ++, I usually use visual studio, but this project uses a vi ssh session for a centos computer using VI and g ++. I ran yum install curl, yum install libcurl, yuminstall curl-devel and yum installed libcurl-devel and still cannot compile the program.
The API documentation is pretty good, and I can find information on how to use libcurl once it is installed correctly, but installing it will turn out to be painful, but.
The code:
#include<iostream>
#include<string>
#include<curl/curl.h>
using namespace std;
string data;
size_t writeCallback(char* buf, size_t size, size_t nmemb, void* up)
{
for (int c = 0; c<size*nmemb; c++)
{
data.push_back(buf[c]);
}
return size*nmemb;
}
int main(void) {
CURL* curl;
curl_global_init(CURL_GLOBAL_ALL);
curl=curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "https://domain.com");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &writeCallback);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_USERPWD, "username:password");
curl_easy_perform(curl);
cout << endl << data << endl;
cin.get();
curl_easy_cleanup(curl);
curl_global_cleanup();
return 0;
}
When trying to compile the following error:
/tmp/ccfeybih.o: In function `main':
helloworld.cpp:(.text+0x72): undefined reference to `curl_global_init'
helloworld.cpp:(.text+0x77): undefined reference to `curl_easy_init'
helloworld.cpp:(.text+0x96): undefined reference to `curl_easy_setopt'
helloworld.cpp:(.text+0xb1): undefined reference to `curl_easy_setopt'
helloworld.cpp:(.text+0xcc): undefined reference to `curl_easy_setopt'
helloworld.cpp:(.text+0xe7): undefined reference to `curl_easy_setopt'
helloworld.cpp:(.text+0xf3): undefined reference to `curl_easy_perform'
helloworld.cpp:(.text+0x132): undefined reference to `curl_easy_cleanup'
helloworld.cpp:(.text+0x137): undefined reference to `curl_global_cleanup'
collect2: ld returned 1 exit status
I can’t find where to go from here.
source
share