C ++将静态库添加到CMake项目会导致未定义的引用

时间:2017-03-08 04:33:38

标签: c++ curl cmake libcurl

我已经设置了一个简单的C ++项目,我尝试使用CMake进行编译,并且我试图将libcurl静态链接到它。在我的main.cpp我已经获得了标准的curl_easy_init示例代码,而我正在尝试发送一个简单的请求以确保库正常运行。但是,我在每次cURL函数调用时都会收到未定义的引用错误。在我的CMakeLists.txt中,我检查当前平台,然后从该特定if语句链接正确的库。我的错误日志,main.cpp和CMakeLists.txt如下所示。 我唯一的问题是如何让我的libcurl正确链接,因为我现在不认为它,因为我的二进制文件只有72KB,因为我实际上可以让它编译删除cURL特定代码但是离开头文件。除了显而易见的事情之外,我尝试使用的是--enable-stdcall-fixup链接器标志,它根本不起作用。

错误日志:

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()

2 个答案:

答案 0 :(得分:1)

您似乎正在尝试将从Visual Studio构建的库链接到使用MinGW构建的应用程序。 This is normally not possible。您应该尝试使用MinGW从源代码构建libcurl。

<小时/> 原始答案:

尝试将2个库版本分成2个不同的目录/ thirdparty / curl_win32和/ thirdparty / curl_2,然后将代码更改为:

if(WIN32)
    add_definitions(-DCURL_STATICLIB)
    link_directories("${CMAKE_SOURCE_DIR}/thirdparty/openssl/" "${CMAKE_SOURCE_DIR}/thirdparty/"curl_win32)
else()
    link_directories("${CMAKE_SOURCE_DIR}/thirdparty/curl2")
endif()

add_executable(${PROJECT_NAME} ${SOURCE_FILES})

if(WIN32)
    target_link_libraries(${PROJECT_NAME} openssl curl)
else()
    target_link_libraries(${PROJECT_NAME} curl)
endif()

请注意,显然link_directories命令必须在add_executable命令之前。请参阅here

答案 1 :(得分:0)

个人而言,我总是使用命令&#34; set&#34;对于librairies,我传递相对路径,然后将该变量放在target_link_libraries中。