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
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.