Libcurl 问题 - "initializer element is not constant"
Libcurl Issue - "initializer element is not constant"
我正在接触 C curl.h
库,第一个示例遇到编译问题。基于 example given here,我正在尝试编译此代码:
#include <curl/curl.h>
CURL *curl = curl_easy_init();
if(curl) {
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
使用此 gcc
命令:
gcc curltest.c -lcurl
我收到此错误:
curltest.c:3:14: error: initializer element is not constant
CURL *curl = curl_easy_init();
^
curltest.c:4:1: error: expected identifier or ‘(’ before ‘if’
if(curl) {
^
知道出了什么问题吗?
你写的内容:
#include <curl/curl.h>
CURL *curl = curl_easy_init();
让我相信您拥有全局级别的所有这些代码(因此不在函数内部)。在全局级别上,您不能使用函数调用来初始化变量,也不能有诸如 if
之类的可执行语句。您需要在函数内部调用该函数,例如:
#include <curl/curl.h>
CURL *curl;
int main(void)
{
curl = curl_easy_init();
if (curl) {
//...
}
我正在接触 C curl.h
库,第一个示例遇到编译问题。基于 example given here,我正在尝试编译此代码:
#include <curl/curl.h>
CURL *curl = curl_easy_init();
if(curl) {
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
使用此 gcc
命令:
gcc curltest.c -lcurl
我收到此错误:
curltest.c:3:14: error: initializer element is not constant
CURL *curl = curl_easy_init();
^
curltest.c:4:1: error: expected identifier or ‘(’ before ‘if’
if(curl) {
^
知道出了什么问题吗?
你写的内容:
#include <curl/curl.h>
CURL *curl = curl_easy_init();
让我相信您拥有全局级别的所有这些代码(因此不在函数内部)。在全局级别上,您不能使用函数调用来初始化变量,也不能有诸如 if
之类的可执行语句。您需要在函数内部调用该函数,例如:
#include <curl/curl.h>
CURL *curl;
int main(void)
{
curl = curl_easy_init();
if (curl) {
//...
}