5

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 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Hello, thank you for your answer ! Cmake drop me this : Unknown CMake command "add_denifition". My file is .dll.a, my bad Also, I don't really get why, but i added the include_directories command and my .c won't compile with the new relative path..
Sorry, I forgot the ending "s" in the command "add_definitions" I corrected it. For include directories, do you delete your CMake cache before ?
I don't know how to delete the Cmake cache. Delete the cmakecache.txt file ? I still have the "undefined reference to `curl_global_init'" .. :(
If you execute CMake elsewhere than the source directory (and I hope you do), like a "build" directory for exemple, remove the cache : * with CMake GUI : File > Delete Cache * with command line : rm -rf build/*
Ok the include works, thanks. But I still have the "undefined reference to `curl_global_init'" .. :(
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.