C - Cmake compiling program with libcurl

▼魔方 西西 提交于 2021-01-03 06:24:42

问题


I'm trying to use some curl code to test the lib but I can't compile it :(

I'm using Clion (Cmake + gcc) and I've got a libcurl.a, a libcurl.dll and a libcurl.dlla What am I suppose to do with those 3 files ?

This is my CmakeLists.txt :

cmake_minimum_required(VERSION 3.10)
project(curl_test2 C)

set(CMAKE_C_STANDARD 99)

ADD_DEFINITIONS( -DCURL_STATICLIB )
include_directories( "include" )

set(SRCS"
srcs/main.c")
set(HEADERS
    "include/curl/curl.h"
     "include/curl/easy.h")

link_directories("lib")

add_executable(curl_test2 ${SRCS} ${HEADERS})

target_link_libraries(curl_test2 "curl")

this is my project:

include
    --curl
        --curl.h
srcs
    --main.c
lib
    --libcurl.a
    (--dlls
        --libcurl.dll
        --libcurl.dlla) <- i'm not using them for now

this is my main.c (just a libcurl example, not really important - i'm just trying to compile):

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

int main(void)
{
    CURL *curl;
    CURLcode res;

    curl_global_init(CURL_GLOBAL_ALL);

    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://postit.example.com/moo.cgi");
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=daniel&project=curl");

        res = curl_easy_perform(curl);
        if(res != CURLE_OK)
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    curl_easy_strerror(res));

        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();
    return 0;
}

When I'm trying to build the compiler drop this error : "undefined reference to `_imp__curl_global_init'" (and all other curl function calls)

Can you help me ? I'm a bit lost - I did not really use Cmake before..

Thank you !


回答1:


You link with static curl library (libcurl.a) which required CURL_STATICLIB definition. Add this in your CMakeLists.txt between "project" and "add_executable" commands:

add_definitions( -DCURL_STATICLIB )

Moreover, your file .dlla is not .dll.a ? In that case, .dll.a file is (I think) generated by MinGW toolchain and is required only if you link dynamically with curl library (as .dll file).

I downloaded the sources of cURL library here and compiled it (with Visual Compiler 2015). It produces shared libraries (libcurl.dll and libcurl_imp.lib) that I move in the lib directory (I should rename libcurl_imp.lib to curl.lib). I move also the include directory.

Then, I used this CMakeLists.txt :

cmake_minimum_required(VERSION 3.10)
project(curl_test2 C)

set(CMAKE_C_STANDARD 99)

set(SRCS "srcs/main.c")

set( CURL_LIBRARY ${CMAKE_SOURCE_DIR}/lib )
set( CURL_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include )
find_package( CURL )

include_directories( ${CURL_INCLUDE_DIRS} )
link_directories( ${CURL_LIBRARIES} )

add_executable(curl_test2 ${SRCS})
target_link_libraries(curl_test2 curl)

With it, your project compiles and the execution works.

I also tried with cURL static library (for that, if you use CMake to compile cURL, add -DCURL_STATICLIB=ON in cmake command line). And I moved the produced library libcurl.lib to lib\curl.lib.

I used this CMakeLists.txt :

cmake_minimum_required(VERSION 3.10)
project(curl_test2 C)

set(CMAKE_C_STANDARD 99)

set(SRCS "srcs/main.c")
add_definitions( -DCURL_STATICLIB )

set( CURL_LIBRARY ${CMAKE_SOURCE_DIR}/lib )
set( CURL_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include )
find_package( CURL )

include_directories( ${CURL_INCLUDE_DIRS} )
link_directories( ${CURL_LIBRARIES} )

add_executable(curl_test2 ${SRCS})
target_link_libraries(curl_test2 curl wldap32 ws2_32)

And it works too.



来源:https://stackoverflow.com/questions/49778607/c-cmake-compiling-program-with-libcurl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!