【问题标题】:how do I add curl(libcurl) to my C project in CLION?如何在 CLION 中将 curl(libcurl) 添加到我的 C 项目中?
【发布时间】:2020-05-01 13:50:24
【问题描述】:

我必须制作一个 Linux 应用程序,该应用程序将从队列中打开网站并下载它们并将它们作为文件存储在我的 PC 上,同时使用线程。 我们有一个 web_request.c 和 web_request.h 类,它们使用 OPENSSL 和 CURL 来完成这项工作,我们可以使用 web_request.c 中的函数来下载我们排队的站点。 到目前为止,我一直在尝试在 curl 之后进行编译,但肯定可以使用一些帮助。

我的 CMakeLists.txt:

project(BS06 C)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_FLAGS -lm -lssl -lcrypto -pthread -lcurl)

add_executable(BS06 main.c)

我如何在 main.c 中包含 web_request.h 以使用它们的功能:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <values.h>
#include <string.h>
#include "include/web_request.h"

我如何在 web_Request.c 中包含 curl

#include "web_request.h"
#include <assert.h>
#include <curl/curl.h>
#include <getopt.h>
#include <openssl/opensslv.h> // OPENSSL_VERSION_NUMBER
#include <stdio.h>
#include <string.h> // strncmp

CURL --版本:

curl 7.58.0 (x86_64-pc-linux-gnu) libcurl/7.58.0 OpenSSL/1.1.1 zlib/1.2.11 libidn2/2.0.4 libpsl/0.19.1 (+libidn2/2.0.4) nghttp2/1.30.0 librtmp/2.3
Release-Date: 2018-01-24 

curl-config --libs:

-lcurl

错误:

 [50%] Building C object CMakeFiles/BS06.dir/main.c.o
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
/bin/sh: 1: -lssl: not found
/bin/sh: 1: -lcrypto: not found
/bin/sh: 1: -pthread: not found
/bin/sh: 1: -lcurl: not found
CMakeFiles/BS06.dir/build.make:62: recipe for target 'CMakeFiles/BS06.dir/main.c.o' failed
make[3]: *** [CMakeFiles/BS06.dir/main.c.o] Error 127
CMakeFiles/Makefile2:75: recipe for target 'CMakeFiles/BS06.dir/all' failed
make[2]: *** [CMakeFiles/BS06.dir/all] Error 2
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/BS06.dir/rule' failed
make[1]: *** [CMakeFiles/BS06.dir/rule] Error 2
Makefile:118: recipe for target 'BS06' failed
make: *** [BS06] Error 2

【问题讨论】:

  • 可能相关? >> (.text+0x20): 对 `main' 的未定义引用

标签: c unix curl libcurl clion


【解决方案1】:

您似乎需要将 curl 库与可执行文件链接。如果您重写您的 CMakeList.txt 是否有帮助:

project(BS06 C)

set(CMAKE_C_STANDARD 11)
find_package(CURL REQUIRED) 

include_directories(${CURL_INCLUDE_DIR})
add_executable(BS06 main.c)
target_link_libraries(BS06 ${CURL_LIBRARIES})

【讨论】:

    猜你喜欢
    • 2017-08-26
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-09
    • 1970-01-01
    相关资源
    最近更新 更多