0

I have a custom static library called "libcurlwrapper", which just wrapps the execution of libcurl functions, and a main app which calls the library's function.

Compiling the library works, but when it comes to linking the main app I get these errors:

/usr/bin/ld: .//libcurlwrapper.a(curl_wrapper.o): in function `http3_demo()':
curl_wrapper.cpp:(.text+0xd): undefined reference to `curl_easy_init'
/usr/bin/ld: curl_wrapper.cpp:(.text+0x39): undefined reference to `curl_easy_setopt'
/usr/bin/ld: curl_wrapper.cpp:(.text+0x54): undefined reference to `curl_easy_setopt'
/usr/bin/ld: curl_wrapper.cpp:(.text+0x60): undefined reference to `curl_easy_perform'
/usr/bin/ld: curl_wrapper.cpp:(.text+0x73): undefined reference to `curl_easy_strerror'
/usr/bin/ld: curl_wrapper.cpp:(.text+0x9d): undefined reference to `curl_easy_cleanup'
collect2: error: ld returned 1 exit status
make: *** [Makefile:5: all] Error 1

What exactly causes these linker errors?

Ideas how to get rid of them?

Any Help is appreciated!

Thanks

libcurlwrapper > curl_wrapper.h:

#pragma once

#include <curl/curl.h>

void http3_demo();

libcurlwrapper > curl_wrapper.cpp:

#include "curl_wrapper.h"

void http3_demo() {

    // Source taken from here: https://curl.se/libcurl/c/http3.html

    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();

    if(curl) {

        curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
        curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_3);

        // 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);

    }

    printf("http3_demo: endmarker");

}

libcurlwrapper > Makefile:

CXX = g++
CXXFLAGS = -lcurl

all:
    @$(CXX) -c ./curl_wrapper.cpp $(CXXFLAGS)
    @ar -rc libcurlwrapper.a ./curl_wrapper.o

main_app > main.cpp:

#include "curl_wrapper.h"

int main() {

    http3_demo();

    return 0;

}

main_app > Makefile:

CXX = g++
CXXFLAGS = -L ./ -lcurlwrapper

all:
    @$(CXX) main.cpp $(CXXFLAGS) -o main_app
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Stephen Newell Jan 11 '22 at 20:36
  • Thanks for the link. I already stumbled upon that link when researching the dilemma, but it only describes the common (causes for) linker errors. Unfortunately I haven't found one specific to this problem yet. – user17906461 Jan 11 '22 at 20:52
  • You're not linking against whatever provides the `curl_easy*` functions (I assume part of `libcurl`). – Stephen Newell Jan 11 '22 at 21:08

1 Answers1

1

A static library is compiled into the final executable. External functions used by a static library are just references. The main executable that uses the static library will need to resolve the references to all of the external libraries that the static library refers to. In this case, that means the main executable project needs to link to the libcurl library.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks for the answer. The code in the question is a very simplified version of a project currently being developed, in which i already tried what you suggested by adding -lcurl to the CXXFLAGS, but it didn't help anything for some reason. I tried doing so with the code above and it worked like a charm / all ld errors were gone! – user17906461 Jan 12 '22 at 08:50
  • Do you also know how to avoid having to link the main executable project to libcurl? Is there some way to already do so in the libcurlwrapper library? (For encapsulation reasons) – user17906461 Jan 12 '22 at 08:55
  • Compile the wrapper into a dynamic library instead of a static library, so it can be self-contained. – Remy Lebeau Jan 12 '22 at 14:24
  • Any way to make a static lib self-contained? – user17906461 Jan 12 '22 at 18:13
  • @user17906461 not really, no. That is simply the nature of how static libraries work. Like in object files, external references in static libraries aren't resolved until the linker stage. – Remy Lebeau Jan 13 '22 at 09:34