0

Every time I want to use CURL in one of my C++ programs, I have to add the flag -lcurl as a flag onto g++. This can be especially annoying when working with Eclipse. If /usr/include/curl/curl.h exists, what do I need to do to have CURL always be within the include path for g++?

Puzzler3141
  • 1,682
  • 2
  • 14
  • 21

3 Answers3

1

tl;dr: you have to add the flag.


The linker needs libcurl, not the compiler. The compiler needs the header; the linker needs the lib.

To simplify things quite a bit, the header file tells the compiler that the declarations will be defined later. libcurl is what actually defines them.

The linker does not guess-and-check what to link against (doing so would be a horrible idea). You must explicitly tell it what to link against (except for the default libs). In particular, the linker has to know to use libcurl to find the declarations that curl.h laid out. Without libcurl, the linker is missing functions and thus cannot produce a complete binary.

I'm not familiar with Eclipse, but I'm nearly positive that it has an option where you can specify additional libraries. Yes, you'll have to do that once per project, but that shouldn't be a major overhead.

Corbin
  • 33,060
  • 6
  • 68
  • 78
  • Alright, I know how to do that. I guess I was just hoping that there would be a more "permanent" way since I use curl in almost every project. If you don't mind, could you explain or point me to a resource that explains the differences between compilers, linkers, headers, and libraries? – Puzzler3141 Jan 09 '13 at 04:31
  • 1
    @user1580190 I'm not sure off the top of my head, but http://stackoverflow.com/questions/9529571/c-compiler-and-linker-functionality seems to have a few good explanations. – Corbin Jan 09 '13 at 04:35
1

Try adding curl path in

Properties -> C/C++ General -> Paths and Symbols:

m4n07
  • 2,267
  • 12
  • 50
  • 65
1

This is just how linking works in C and C++.

  1. When you compile the program, you include the header file /usr/include/curl/curl.h. The compiler does this part. The header file contains all of the definitions for the library interface.

  2. When you link the program, you link in the library /usr/lib/libcurl.so, or whatever it happens to be named. The linker does this part. The library contains the implementation in either a loadable (for dynamic libraries) or linkable (for static libraries) format.

The C and C++ languages have no way of specifying which libraries should be linked in, so you have to pass -lcurl to the linker. This is just the way it is.

There are some extensions to C and C++ that allow you to encode library dependencies in your source code, e.g., #pragma comment with MSC, but they're not supported by your typical ELF toolchain, as far as I know.

Note: Actually, the -lcurl flag is not for g++, but it is for the linker, ld. When you pass -lcurl to g++, g++ passes it through to the linker.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • So what is actually turning human readable code into executable byte code? The linker or the compiler? Does the compiler run, and then the linker? Or is it more that the compiler is executed, runs the linker, and then returns to finish the job? – Puzzler3141 Jan 09 '13 at 05:02
  • @user1580190: It depends. On the toolchain I'm using now, it depends on which compiler I use and what optimization flags are enabled. I wouldn't concern myself with knowing which one produces the machine code. Just remember that there are two phases: compiling, and linking. – Dietrich Epp Jan 09 '13 at 05:06