2

I'd like to use libcurl (a lib that handles HTTP connections) by linking it statically to a program I am writing. I am new to building programs from their source, so I have no clue on how to do it (i'd like to avoid installing libcurl, so that not to mess with my system).

So here it is. I downloaded libcurl sources, and then I built them with ./configure && make && make test. Well I think I have built them, because the tests pass. And I actually downloaded the source for the whole curl software, so I guess I built more than the mere libcurl library.

Now, I am writing mysource.c :

#include <stdio.h>
#include <curl/curl.h>

int main(void) {
  CURL *curl;
  curl = curl_easy_init();
  if(curl) {
    // do something
  }
  return 0;
}

So, I brought all the include folder from the curl source near mysource.c, so that the preprocessor resolves the #include <curl/curl.h>. I suppose I should also bring the binary, built lib (like libcurl.o? libcurl.so? libcurl.a? something else?). But I can't find any file with such names in the curl folder/sub-folder. Where is it located?
Even if I find it and bring it along my own sources, would using gcc -static -I... -L... -lcurl mysource.c work? How would gcc be able to understand the -lcurl flag then?

More generally (and the following question should work with all sources, not just curl/libcurl), when building from a source, where do the compiled and linked binaries go? I know I should have my answer by reading the makefile, but it is thousands of lines long, and I really don't see the output dir for the built binaries anywhere.
I know that, if I run make install as well, I should find them somewhere like /usr/local/bin, /usr/local/lib (and the corresponding .h files, if any, in /usr/local/include). But what happens if I don't wish to install the software I have just built? Can't I just grab the compiled files to use them?

Thanks a lot

  • There shouldn't be any need to compile your own version of the library: both the libcurl4-gnutls-dev and libcurl4-nss-dev packages already provide libcurl.a static libraries, so it should just be a matter of specifying static linkage (gcc -static ...) when you build your program AFAIK. – steeldriver Dec 23 '14 at 22:59
  • Thanks! I'm usually lazy, and I am actually used to grab all my binaries from compiled packages. But this time, I'd like to really understand how to build from a tarball source, so I'd like to avoid this resorting to packages. It gives me an idea though, I think I'll get the package without installing it, and I'll check the names of the files it includes. I think I'll have the same ones in my own build. – Daladim Dec 24 '14 at 8:02
  • In that case the easiest solution is probably to configure the library package to install itself into your home directory (./configure --prefix=$HOME), and then specify that as a library search path when you build your program (something like -L $HOME/lib -lcurl). – steeldriver Dec 24 '14 at 13:20
  • OK, thanks! I finally managed to use my own libcurl.so (for dynamic linking) and libcurl.a (for static linking). After digging a bit (and for future reference to the readers), these built binaries were originally located in the hidden dir /path/to/the/downloaded/sources/lib/.libs/. But it does not look a very straightforward place for me, is it a general behaviour? – Daladim Jan 2 '15 at 11:42
1

The idea is the compiler turns .c file into .o, so-called object file. Next one uses ar to create a library, .a file that is supposed to contain several, hm, compiled C functions that one might reuse. Finally ld performs linkage, creating .so or ELF from specified .o and .a files. Because of this needless complexity most compilers are also front-ends for ld and ar bintools.

To override the path where compiler/linker searches for libraries, one uses --library-path=searchdir option. Though I am just answering the question, you dont need .a file really for statically linked ELF.

So what module (.o file) you need to link with to use particular functions? Generally you should search what .c file has it, by default xyz.c file gets compiled into xyz.o so you'll need this.

  • Thanks , so that is why I found a ton of libcurl-whatever.o files, but no plain libcurl.so file then... Right, I'll try to locate the right .o that suits my needs and use it. If I understand you correctly, the linker only needs a --library-path flag (or -L) and it will automatically find the right .o in the given folder? That means I don't need the -lcurl flag then? Then, in what case should one use this flag? Only if the lib has been completely installed in my system (say, in /usr/local/lib)? – Daladim Dec 24 '14 at 7:53
  • I'll try that as soon as I can get to my computer (might be tomorrow or so). Thanks for your advice anyway – Daladim Dec 24 '14 at 7:57
  • I finally found my .o and the .a that was made from them (it was in lib/.libs/libcurl.a... strange location...). I did not try to build a minimal .a with only the requires .o however, as you sugested. But now it works as I expected. Thanks a lot! – Daladim Jan 2 '15 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.