【问题标题】:C, <curl/curl.h> not linkingC, <curl/curl.h> 没有链接
【发布时间】:2021-05-06 07:32:48
【问题描述】:

我有一个如下所示的 1 文件 C 程序,它正在尝试进行简单的 CURL 调用。还有一个简单的 Make 文件。

看起来我的 curl/curl.h 没有被链接,导致所有对 *curl 的引用都是错误的。

我使用自制软件安装 Curl。 我需要指定链接器的确切位置吗?

代码

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

int main(void) {
    Curl *curl = curl_easy_init();

    if(!curl) {
        printf("curl init failed");
        return 1;
    }

    curl_easy_setopt(curl, CURLOPT_URL, "https://api.coinbase.com/v2/prices/BTC-USD/buy");

    CURLcode result = curl_easy_perform(curl);

    if(result != CURLE_OK) {
        printf("curl peform fail");
    }

    curl_easy_cleanup(curl); 
 
  return 0;
}

错误

name@name-MacBook-Pro c % make

gcc -o main main.c -lcurl 

main.c:10:5: error: use of undeclared identifier 'Curl'

Curl *curl = curl_easy_init();
^ main.c:10:11: error: use of undeclared identifier 'curl'

Curl *curl = curl_easy_init();
      ^ main.c:12:9: error: use of undeclared identifier 'curl'

if(!curl) {
    ^ main.c:17:22: error: use of undeclared identifier 'curl'

curl_easy_setopt(curl, CURLOPT_URL, "https://api.coinbase.com/v2/prices/BTC-USD/buy");
                 ^ main.c:19:41: error: use of undeclared identifier 'curl'

CURLcode result = curl_easy_perform(curl);
                                    ^ main.c:25:23: error: use of undeclared identifier 'curl'

curl_easy_cleanup(curl); 
                  ^ 6 errors generated. make: *** [all] Error 1

如果安装了 CURL 检查

name@name-MacBook-Pro c % curl --version curl 7.64.1 (x86_64-apple-darwin19.0) libcurl/7.64.1 (SecureTransport) LibreSSL/2.8.3 zlib/1.2.11 nghttp2/1.39.2 发布日期:2019-03-27 协议: dict 文件 ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp 特点:AsynchDNS GSS-API HTTP2 HTTPS 代理 IPv6 Kerberos 大文件 libz MultiSSL NTLM NTLM_WB SPNEGO SSL UnixSockets

【问题讨论】:

  • 首先,Curl-> CURL
  • 解决了,哇。谢谢

标签: c curl gcc linker


【解决方案1】:

你应该使用 CURL,而不是 Curl。


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

int main(void) {
    CURL *curl = curl_easy_init();

    if(!curl) {
        printf("curl init failed");
        return 1;
    }

    curl_easy_setopt(curl, CURLOPT_URL, "https://api.coinbase.com/v2/prices/BTC-USD/buy");

    CURLcode result = curl_easy_perform(curl);

    if(result != CURLE_OK) {
        printf("curl peform fail");
    }

    curl_easy_cleanup(curl); 

    return 0;
}

【讨论】:

    猜你喜欢
    • 2021-07-27
    • 2014-07-17
    • 1970-01-01
    • 2021-05-28
    • 2011-05-30
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    相关资源
    最近更新 更多