How to use libcurl as a static library with cmake?

2019-08-13 03:41发布

问题:

I have googled around and can not figure this out. I am trying to learn c++. But my project requires libcurl to work.

Now I have not been able after hours of googling for this issue to figure out how I can statically link the libcurl library with my project using cmake. I am developing on Windows 10 64 bit.

I believe I am missing quite a few ideas of how exactly everything works with libraries in c++. I could find a few answers partially answering my question but nothing that I could piece together. If there are a few answers that can answer my question I would be more than happy to get those provided to me.

Down below is my code, cmakelists & the error that I am currently having.

#include <iostream>
#include <sstream>
#include <curl.h>

int main() {

std::string username;
std::string password;

std::cout << "Please enter the username of the account you want to use to snipe" << std::endl;
std::cin >> username;

std::cout << "Please enter the password of the account you want to use to snipe" << std::endl;
std::cin >> password;

curl_global_init(CURL_GLOBAL_ALL);

}

CMakeLists:

cmake_minimum_required(VERSION 3.3)
project(Learning)

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

include_directories(curl/include/curl)
link_directories(curl/bin)

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

The error:

"C:\Program Files (x86)\JetBrains\CLion 1.2.2\bin\cmake\bin\cmake.exe" --build C:\Users\Czarek\.CLion12\system\cmake\generated\c124b936\c124b936\Debug --target Learning -- -j 8
Scanning dependencies of target Learning
[ 50%] Building CXX object CMakeFiles/Learning.dir/main.cpp.obj
[100%] Linking CXX executable Learning.exe
CMakeFiles\Learning.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/Czarek/ClionProjects/Learning/main.cpp:16: undefined reference to `curl_global_init'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [Learning.exe] Error 1
CMakeFiles\Learning.dir\build.make:96: recipe for target 'Learning.exe' failed
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Learning.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/Learning.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/Learning.dir/rule] Error 2
mingw32-make.exe: *** [Learning] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Learning.dir/rule' failed
Makefile:117: recipe for target 'Learning' failed