Dany
Dany

Reputation: 2800

How to get rid of libcurl linking error?

I am using libcurl version 7.19 in my cpp application. I am trying to use the example given by kukuruku.co. When I try to build the application I am getting the following errror,

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'

here is the code snippet I am using ,

#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;
}

My Makefile looks like this,

  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

I use module.mk file for the target,

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

Could someone help me on this?

Upvotes: 2

Views: 6318

Answers (1)

Ruslan Osmanov
Ruslan Osmanov

Reputation: 21522

You should provide the linker options, e.g.

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

Or use pkg-config. In a Linux shell it may look like the following:

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

UPDATE: static linking

As @drew010 suggested, you can generate proper linker options with curl-config:

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

UPDATE: added Makefile and CMake examples

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

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

Upvotes: 6

Related Questions