如何在CLION中将curl(libcurl)添加到我的C项目中



我必须制作一个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配置--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

似乎需要将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})

最新更新