Monday, February 5, 2024

[SOLVED] GCC won't use self-built libcurl?

Issue

I'm trying to build libcurl to link latest version statically to my program. Output folder contains the build files.

However when I build and run this code:

// g++ gontest.cpp -o gontest -Ilibs/curl/gonout/include/curl -Llibs/curl/gonout/lib -lcurl
#include <curl/curl.h>
#include <iostream>

int main(int argc, char** argv)
{
  auto data = curl_version_info(CURLVERSION_NOW);
  std::cout << "curlversion: " << data->version << std::endl;
  return 0;
}

...I'm getting the system version of curl library (7.6.8), not the built one (8.8.0).

It's the same output as if I'd built it with this:

g++ gontest.cpp -o gontest -lcurl

I've tried several versions of the command, including:

# none works
~$ g++ gontest.cpp -o gontest -Ilibs/curl/gonout/include/curl -Llibs/curl/gonout/lib -l:libs/curl/gonout/lib/libcurl.a
/usr/bin/ld: cannot find -l:libs/curl/gonout/lib/libcurl.a
collect2: error: ld returned 1 exit status

~$ g++ gontest.cpp -o gontest -Ilibs/curl/gonout/include/curl -Llibs/curl/gonout/lib -l:/libs/curl/gonout/lib/libcurl.a
~$ g++ gontest.cpp -o gontest -Ilibs/curl/gonout/include/curl -Llibs/curl/gonout/lib -l:./libs/curl/gonout/lib/libcurl.a
~$ g++ gontest.cpp -o gontest -Ilibs/curl/gonout/include/curl -Llibs/curl/gonout/lib -l:./libs/curl/gonout/lib/libcurl.so
~$ g++ gontest.cpp -o gontest -Ilibs/curl/gonout/include/curl -Llibs/curl/gonout/lib -l:/libs/curl/gonout/lib/libcurl.so
~$ g++ gontest.cpp -o gontest -Ilibs/curl/gonout/include/curl -Llibs/curl/gonout/lib -l:libs/curl/gonout/lib/libcurl.so
# note that it exists
~$ ls ./libs/curl/gonout/lib/libcurl.a
./libs/curl/gonout/lib/libcurl.a

Why is my custom-built library getting ignored?


Solution

After building .so, it either needs to be linked to the executable by modifying LD_LIBRARY_PATH, or, it needs to be built with a custom include path defined with flags -Wl,-rpath,<path to lib directory>

code

In this example we are using -Wl,-rpath,. where . adds the current directory to the list of usable library locations.

Please note that this way isn't a standard/proper way on Linux and normally, I'd simply upgrade curl on a given machine. It just isn't an option right now. Please, don't use it unless you have to.



Answered By - Gonzi
Answer Checked By - Willingham (WPSolving Volunteer)