首先我想说我看到了类似的帖子,然后我发布了这个帖子但是被删除了......所以所有内容都在标题中。我知道它可能是链接器步骤的问题,但我不知道为什么。实际上,我将所有(* .a)文件链接到我的Code :: Blocks projet上,并在链接器选项和搜索目录中添加了curl库的不同路径。
我也尝试手动编译和链接,但我没有成功。
g++ -L ..\lib\curl\lib64 -I ..\lib\curl\include -lcurl main.cpp
另一次尝试
g++ main.cpp -L ..\lib\curl\lib64 -I ..\lib\curl\include -lcurl -o test.exe
这是错误:“未定义引用'curl_easy_init'”
这是我的代码,可能是问题的根源在这里,但我不这么认为(尝试过静态的lib)
#include <iostream>
#define CURL_STATICLIB
#include <curl/curl.h>
#include <curl/easy.h>
#include <string>
#include <fstream>
#include <unistd.h>
#include <windows.h>
#include <Lmcons.h>
using namespace std;
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;}
int main(void)
{
/*ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();*/
CURL *curl;
FILE *fp;
CURLcode res;
char *url = "http://stackoverflow.com";
char outfilename[FILENAME_MAX] = "page.html";
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
fclose(fp);
}
return 0;
}