3

i'm new in CLion and i want to improve my skills in libcurl. I have installed libcurl4-openssl-dev on Ubuntu 15.04. My CMakeLists.txt:

cmake_minimum_required(VERSION 3.3)
project(test)


set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lcurl")
set(SOURCE_FILES main.cpp)
add_executable(test ${SOURCE_FILES}) 

But when i try this:

#include <iostream>
#include <curl/curl.h>
#include <string>

using namespace std;

int main() 
{
    CURL *curl;
    CURLcode result;
    curl = curl_easy_init();
    return 0;
}

He shows me such error:

/home/me/ClionProjects/test/main.cpp:10: undefined reference to `curl_easy_init'

Help me please, Thanks

2 Answers2

2

This usually means that your program is not able to link to the
right .a or .so file to resolve the function curl_easy_init.

.a is used in case of static linking . a stands for archive.
.so is used in the case of dynamic linking. so stands for shared object.

Though you might have libcurl.a or libcurl.so available, it
might be that linker is not able to find those at the time of execution.

Specify directories in which the linker will look for libraries like below.

To find the location of libcurl.a you could run the below command as root :

find / -name libcurl.a 

Suppose the above command gives :

/libcurl/folder/libcurl.a 

as output, you could add the below line in your CMakeLists.txt file.

link_directories(/libcurl/folder /any/additional/path)

The target can be linked to the libraries then by using.

target_link_libraries(test libcurl)

If you're doing dynamic linking you should do the same for libcurl.so

sjsam
  • 21,411
  • 5
  • 55
  • 102
  • I added this line: "link_directories(usr/local/lib)", but there is still same mistake. Command line gave me 2 folders, but both didn't solve the problem – Andrew Kusachev Nov 22 '15 at 15:51
  • How about `target_link_libraries` ? I am not sure `-lcurl` will suffice. – sjsam Nov 22 '15 at 15:54
  • Well, it says: `Cannot specify link libraries for target "libcurl" which is not built by this project.` – Andrew Kusachev Nov 22 '15 at 16:14
  • @AndrewKusachev : see my edit. Your executable that is test should come first ie `target_link_libraries(test libcurl)`. I was wrong with the syntax the first time. – sjsam Nov 22 '15 at 16:18
  • This [SO Question](http://stackoverflow.com/questions/19478088/cmake-link-library-target-link-error) might also help you. – sjsam Nov 22 '15 at 16:23
  • I added test: `target_link_libraries(test libcurl)` and there is new error: `Error:Cannot specify link libraries for target "test" which is not built by this project.` – Andrew Kusachev Nov 22 '15 at 16:25
  • Looks strange, make sure that add_executable statement is before target_link_libraries – sjsam Nov 22 '15 at 16:28
  • it wasn't, but new error: `Error:The target name "test" is reserved or not valid for certain CMake features, such as generator expressions, and may result in undefined behavior.` plus previous one, this is really strange – Andrew Kusachev Nov 22 '15 at 16:34
  • Try some other name for your executable please..say example – sjsam Nov 22 '15 at 16:56
  • likely that the last error you got is just a warning and shouldnt affect the linkage.. – sjsam Nov 22 '15 at 17:05
  • 1
    Fine, it does matter, but: `/usr/bin/ld: cannot find -llibcurl collect2: error: ld returned 1 exit status` this is error. I am also trying curlpp and it doesn't work either – Andrew Kusachev Nov 22 '15 at 17:10
  • 1
    it is automatically appending lib ? nop? change libcurl to curl in cmake. just a try. – sjsam Nov 22 '15 at 17:13
  • often, working with CMake is simply pain. That make you go for the simplest method that is `command line` to achieve your results. But the brighter side of cmake comes when you're dealing with large projects. having a CMake project with you gives you the flexibility of working with nmake, make and even Microsoft Visual C++ Compiler. That is why it is CMAKE ie Cross-Platform Make. I personally has benefited a lot from CMake projects in Version Control Systems like github. Glad things work for you. :) – sjsam Nov 22 '15 at 17:41
2

Thanks to @sjsam. The solution is(CMakeLists.txt):

cmake_minimum_required(VERSION 3.3)
project(example)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lcurl")

set(SOURCE_FILES main.cpp)
add_executable(example ${SOURCE_FILES})
target_link_libraries(example curl)