【问题标题】:How to get rid of libcurl linking error?如何摆脱 libcurl 链接错误?
【发布时间】:2016-04-20 01:09:18
【问题描述】:

我在我的 cpp 应用程序中使用 libcurl 7.19 版。我正在尝试使用kukuruku.co 给出的示例。当我尝试构建应用程序时,出现以下错误,

lib/libcurl.a(libcurl_la-http_negotiate.o): In function `cleanup':
    http_negotiate.c:(.text+0x1b): undefined reference to `gss_delete_sec_context'
    http_negotiate.c:(.text+0x30): undefined reference to `gss_release_buffer'
    http_negotiate.c:(.text+0x45): undefined reference to `gss_release_name'

lib/libcurl.a(libcurl_la-http_negotiate.o): In function `Curl_output_negotiate':
    http_negotiate.c:(.text+0x123): undefined reference to `gss_release_buffer'
    http_negotiate.c:(.text+0x1c0): undefined reference to `gss_release_buffer'

lib/libcurl.a(libcurl_la-http_negotiate.o): In function `Curl_input_negotiate':
    http_negotiate.c:(.text+0x324): undefined reference to `GSS_C_NT_HOSTBASED_SERVICE'
    http_negotiate.c:(.text+0x33a): undefined reference to `gss_import_name'
    http_negotiate.c:(.text+0x48e): undefined reference to `gss_release_buffer'
    http_negotiate.c:(.text+0x4cc): undefined reference to `gss_release_buffer'

这是我正在使用的代码 sn-p,

#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
    /* example.com is redirected, so we tell libcurl to follow redirection */ 
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

    /* Perform the request, res will get the return code */ 
    res = curl_easy_perform(curl);
    /* Check for errors */ 
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    /* always cleanup */ 
    curl_easy_cleanup(curl);
  }
  return 0;
}

我的 Makefile 看起来像这样,

  CCFLAGS = -g -Wall -pthread -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -DUSE_APACHE2 -fno-strict-aliasing -fmessage-length=0 -fPIC -    O0 -ftemplate-depth-32 -fno-operator-names -D_GNU_SOURCE -D_GSS_C_NT_HOSTBASED_SERVICE

  CFLAGS += `pkg-config --cflags libcurl`
  LDFLAGS += `pkg-config --libs libcurl`
  #DCMAKE_BUILD_TYPE=Release
  #LIBS += -libcurl

我使用 module.mk 文件作为目标,

APP_OBJECTS = \
        app-client/app-client-cmd.o \
    $(NULL)

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: c++ curl libcurl linkage


    【解决方案1】:

    您应该提供链接器选项,例如

    gcc -Wall -O2 -g -lcurl -o test main.cpp
    

    或使用pkg-config。在 Linux shell 中,它可能如下所示:

    gcc -Wall -O2 -g $(pkg-config --libs --cflags libcurl) -o test main.cpp
    

    更新:静态链接

    正如 @drew010 所建议的,您可以使用 curl-config:

    CC=$(curl-config --cc)
    $CC -Wall -g -O2 main.cpp $(curl-config --static-libs) -o mytest
    

    更新:添加了 Makefile 和 CMake 示例

    使用 Makefile

    CC = gcc
    CFLAGS = -Wall -g -O2
    LDFLAGS =
    
    EXECUTABLE = mytest
    SOURCES = main.cpp
    
    CFLAGS += `pkg-config --cflags libcurl`
    LDFLAGS += `pkg-config --libs libcurl`
    
    
    all: $(EXECUTABLE)
    
    
    $(EXECUTABLE): $(SOURCES)
      $(CC) $(SOURCES) -o $@ $(LDFLAGS)
    
    
    .cpp.o:
      $(CC) $(CFLAGS) $< -o $@
    
    
    clean:
      rm -f $(OBJECTS) $(EXECUTABLE)
    

    使用 CMake

    cmake_minimum_required (VERSION 2.6)
    project (MyProject CXX)
    set(CMAKE_BUILD_TYPE "Release")
    
    include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
    
    include(FindCURL)
    find_package(CURL REQUIRED)
    
    if (NOT CURL_FOUND)
      message (FATAL_ERROR "Curl is not supported")
    endif (NOT CURL_FOUND)
    
    include_directories(CURL_INCLUDE_DIRS)
    
    set(CMAKE_REQUIRED_LIBRARIES "${CURL_LIBRARIES}")
    list(APPEND LIBS "${CURL_LIBRARIES}")
    
    set(TARGET "mytest")
    add_executable(${TARGET} main.cpp)
    target_link_libraries(${TARGET} ${LIBS})
    
    install(TARGETS ${TARGET} DESTINATION "bin")
    

    【讨论】:

    • 感谢您的帮助。我在一个大型应用程序中使用 Makefile。如何在 Makefile 中使用它。我需要使用 CFLAGS 吗?
    • @Dany 这取决于您的Makefile,但如果使用隐式规则编译,请使用 LDLIBS。
    • 根据发布的错误,他可能还需要添加-lgssapi 作为链接器标志。
    • @Dany 似乎与 curl 链接的 gssapi 库也可能丢失。尝试curl-config --static-libs 查看它所链接的所有内容。然后找到 libcurl 并运行ldd /path/to/libcurl.so 看看是否缺少任何东西。您可能需要安装 libgssapi-krb5-2 或其他 libgssapi 库。
    • @RuslanOsmanov - 我在这里添加了我的 makefile 内容。但根据我添加的内容,它不起作用:(。你们帮了很多忙。可能是我不太了解。我是相当新的 C/C++,我来自 Java 背景。
    猜你喜欢
    • 1970-01-01
    • 2022-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    • 2011-08-07
    • 1970-01-01
    相关资源
    最近更新 更多