I've got a simple C++ project set up that I'm trying to compile with CMake, and I'm trying to statically link libcurl to it. In my main.cpp
I've got the standard curl_easy_init
example code, and I'm trying just to send a simple request to make sure the library is working. However, I'm getting undefined reference errors on every cURL function call. In my CMakeLists.txt I'm checking for the current platform, then linking the right libraries from that specific if statement. My error log, main.cpp, and CMakeLists.txt are below. My only question is how do I get my libcurl to link properly, as I don't think it is now, because my binary is only 72KB when I can actually get it to compile removing the cURL specific code but leaving the header file. Other than the obvious, one thing I've tried is using is the --enable-stdcall-fixup
linker flag, which did not work at all.
Error Log:
C:\Users\bfsco\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\163.13906.4\bin\cmake\bin\cmake.exe --build D:\Programming\CPP\cppackage\cmake-build-debug --target all -- -j 4
[ 50%] Linking CXX executable cppackage.exe
CMakeFiles\cppackage.dir/objects.a(main.cpp.obj): In function `main':
D:/Programming/CPP/cppackage/main.cpp:6: undefined reference to `curl_easy_init'
D:/Programming/CPP/cppackage/main.cpp:10: undefined reference to `curl_easy_setopt'
D:/Programming/CPP/cppackage/main.cpp:11: undefined reference to `curl_easy_perform'
D:/Programming/CPP/cppackage/main.cpp:13: undefined reference to `curl_easy_cleanup'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[2]: *** [cppackage.exe] Error 1
CMakeFiles\cppackage.dir\build.make:98: recipe for target 'cppackage.exe' failed
mingw32-make.exe[1]: *** [CMakeFiles/cppackage.dir/all] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/cppackage.dir/all' failed
mingw32-make.exe: *** [all] Error 2
Makefile:82: recipe for target 'all' failed
main.cpp:
#include <iostream>
#include "include/curl/curl.h"
int main() {
CURL *curl = curl_easy_init();
if(curl) {
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
res = curl_easy_perform(curl);
std::cout << res << std::endl;
curl_easy_cleanup(curl);
}
std::cout << "Hello, World!" << std::endl;
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.6)
project(cppackage)
set(CMAKE_CXX_STANDARD 14)
set(SOURCE_FILES main.cpp)
include_directories(${CMAKE_SOURCE_DIR}/include/curl)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
if(WIN32)
add_definitions(-DCURL_STATICLIB)
target_link_libraries(cppackage ${CMAKE_SOURCE_DIR}/thirdparty/openssl/openssl.lib ${CMAKE_SOURCE_DIR}/thirdparty/curl/libcurl.lib)
else()
target_link_libraries(cppackage ${CMAKE_SOURCE_DIR}/thirdparty/curl/libcurl.a)
endif()