编译时,出现缺少 curl 问题的解决

本文介绍了一个用C语言编写的示例项目,该项目利用libcurl从OANDA API沙箱环境中请求EUR/USD、USD/CAD及USD/JPY的汇率数据,并将结果输出到标准输出设备。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/** Description
 *  Sample project in C using libcurl.
 *  Requests the rate data for EUR/USD, USD/CAD, AND USD/JPY and outputs them to standard output.
 */

#include <stdio.h>
#include <curl/curl.h>

int main() {
    CURL *curl;
    CURLcode res;
    
    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL,
                         "http://api-sandbox.oanda.com/v1/prices?instruments=EUR_USD%2CUSD_CAD%2CUSD_JPY");

        struct curl_slist *chunk = NULL;

        // uncomment to add authorization header: not required for sandbox
        // chunk = curl_slist_append(chunk, "Authorization: Bearer <your access token>");
        
        /* use custom headers */
        res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
        
        /* Perform the request, res will get the return code */
        res = curl_easy_perform(curl);
        
        /* Check for errors */
        if(res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    curl_easy_strerror(res));
        }
        
        /* always cleanup */
        curl_easy_cleanup(curl);

        /* free the custom headers */ 
        curl_slist_free_all(chunk);
    }
    return 0;
}


安装

apt-get install libcurl4-gnutls-dev

编译

gcc APISample.c -lcurl -o rate_fetcher

运行

./rate_fetcher

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值