链接项目与静态生成卷曲 [英] Linking project with statically build curl
问题描述
< blockquote>
未定义引用`_imp__curl_easy_init'
未定义引用`_imp__curl_easy_setopt'
未定义引用`_imp__curl_easy_perform'
未定义引用到`_imp__curl_easy_getinfo'
未定义的引用`_imp__curl_easy_strerror'
我有cURL源代码在dep / curl和我的源代码在src文件夹。这是我的项目的CMakeLists.txt的样子:
项目(ShutdownServer)
cmake_minimum_required(VERSION 2.8)
set(CMAKE_BUILD_TYPERelease)
set(CURL_STATICLIB true)
set(BUILD_CURL_EXE false)
add_subdirectory(dep / curl)
set(TARGET_NAME ShutdownServer)
set(LIBCURL_DIR $ {PROJECT_SOURCE_DIR} / dep / curl)
aux_source_directory(src SRC_LIST)
add_executable($ {TARGET_NAME} $ {SRC_LIST})
find_path(LIBCURL_INCLUDE_DIR curl / curl.h HINTS $ {LIBCURL_DIR} / include)
include_directories($ {LIBCURL_INCLUDE_DIR})
add_dependencies($ {TARGET_NAME} libcurl)
(LIBS $ {LIBS} ws2_32)
set(LIBS $ {LIBS} iphlpapi)
set(LIBS $ {LIBS} libcurl)
target_link_libraries ($ {TARGET_NAME} $ {LIBS})
set(CMAKE_EXE_LINKER_FLAGS-static -static-libgcc -static-libstdc ++)
当我将CURL_STATICLIB选项切换为false,并动态构建libcurl时,项目构建正常,但我宁愿将所有构建到一个二进制文件。任何人都可以告诉我这是什么问题?
您需要定义 CURL_STATICLIB
当包括curl.h时,否则编译器会认为你正试图链接到libcurl库的动态版本。
如果你检查curl。 h,您可以看到以下行:
/ *
*装饰用于Win32 DLL链接的可导出函数。
*这避免使用.def文件来构建libcurl.dll。
* /
#if(defined(WIN32)|| defined(_WIN32))&& !define(CURL_STATICLIB)
#if defined(BUILDING_LIBCURL)
#define CURL_EXTERN __declspec(dllexport)
#else
#define CURL_EXTERN __declspec(dllimport)
#endif
#else
你可以看到,默认情况下,每个函数都声明为 __ declspec(dllimport)
约定,其添加符号名称上的 _imp __
前缀。
因此,您需要在CMakeLists.txt中定义CURL_STATICLIB,如下所示:
add_definitions(-DCURL_STATICLIB)
I'm doing small project in C++ with CMake and MinGW32, which requires libcurl library, but when i try to link statically build libcurl.a I get undefined reference errors
undefined reference to `_imp__curl_easy_init' undefined reference to `_imp__curl_easy_setopt' undefined reference to `_imp__curl_easy_perform' undefined reference to `_imp__curl_easy_getinfo' undefined reference to `_imp__curl_easy_strerror'
I have cURL source code in dep/curl and my source code in src folder. And this is how my project's CMakeLists.txt looks like:
project(ShutdownServer)
cmake_minimum_required(VERSION 2.8)
set(CMAKE_BUILD_TYPE "Release")
set(CURL_STATICLIB true)
set(BUILD_CURL_EXE false)
add_subdirectory(dep/curl)
set(TARGET_NAME "ShutdownServer")
set(LIBCURL_DIR ${PROJECT_SOURCE_DIR}/dep/curl)
aux_source_directory(src SRC_LIST)
add_executable(${TARGET_NAME} ${SRC_LIST})
find_path(LIBCURL_INCLUDE_DIR curl/curl.h HINTS ${LIBCURL_DIR}/include)
include_directories(${LIBCURL_INCLUDE_DIR})
add_dependencies(${TARGET_NAME} libcurl)
set(LIBS ${LIBS} ws2_32)
set(LIBS ${LIBS} iphlpapi)
set(LIBS ${LIBS} libcurl)
target_link_libraries(${TARGET_NAME} ${LIBS})
set(CMAKE_EXE_LINKER_FLAGS "-static -static-libgcc -static-libstdc++")
When I switch CURL_STATICLIB option to false, and build libcurl dynamically, project is build fine, but I'd rather had build all into one binary file. Can anybody please tell me what is wrong with this?
You need to define CURL_STATICLIB
when including curl.h, otherwise the compiler will think that you are trying to link against the dynamic version of the libcurl library.
If you examine curl.h, you can see the following lines:
/*
* Decorate exportable functions for Win32 DLL linking.
* This avoids using a .def file for building libcurl.dll.
*/
#if (defined(WIN32) || defined(_WIN32)) && !defined(CURL_STATICLIB)
#if defined(BUILDING_LIBCURL)
#define CURL_EXTERN __declspec(dllexport)
#else
#define CURL_EXTERN __declspec(dllimport)
#endif
#else
You can see that by default, every function is declared with the __declspec(dllimport)
convention, which add those _imp__
prefixes on symbol names.
So you need to define CURL_STATICLIB in your CMakeLists.txt, like this:
add_definitions(-DCURL_STATICLIB)
这篇关于链接项目与静态生成卷曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!