如何摆脱libcurl链接错误?

时间:2016-04-20 01:09:19

标签: c++ curl libcurl linkage

我在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'

这是我正在使用的代码段,

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

有人可以帮我这个吗?

1 个答案:

答案 0 :(得分:2)

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

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