Linking errors

Hello Everyone!

Now this is unheard of to me, I am surprised to hear about this just now, after 2 years working with c++:

#include <curl\curl.h>

I get this error later in the code

obj\Debug\main.o||In function `main':|
AUB\dload\main.cpp|10|undefined reference to `__imp__curl_easy_init'|
AUB\

...

Shouldn't this include all the functions and classes in the curl\curl.h header? If so, why do you I need to configure the compiler to link some lib files (I do not whether they are .lib, .a, or .so!)?

I having trouble finding an answer on the internet...

Thanks!
Last edited on
Shouldn't this include all the functions and classes in the curl\curl.h header?


No, it should not. It can do, but there is no requirement that it should. It should provide prototypes for all functions, and definitions of classes, but does not have to contain the actual definitions of functions or member functions of classes. It is actually bad practice to have the full definitions in the header file.

The curl.h header does not contain the actual function definition of __imp__curl_easy_init for very good reasons.

Commonly, a header file will include the prototypes of a function. This is enough for your compiler to work. The compiler effectively leaves a note to the linker, stating that the linker needs to find the actual function somewhere in the compiled code and libraries. The linker then goes looking through all the compiled code and the libraries for that function.

If you do not provide some compiled code or a library with that function to the linker, the linker will come back with undefined reference. This is what's happened in your case, and is completely normal.

Sometimes, a header file will include the actual function definition and there will be no need to link to a library. Alternatively, maybe you have the header file containing the prototype and a corresponding cpp file containing the actual definition, and again there will be ne need to link to a library because you are providing the definition yourself. However, if a header file includes the full definition, as soon as you make a programme in which two different cpp files include the header, you'll have problems that can be really, really annoying to fix (you will have two separate compiled files with identical functions in - this is the multiply defined symbol linker error). This is (one reason) why it's uncommon to have the definition of a function in the header.

It is very, very, very common to provide a header file and a library that must be linked to. It's the standard way - it's what libraries are for.
Last edited on
Topic archived. No new replies allowed.