Heiko Herlich
Heiko Herlich

Reputation: 333

C; headers and code

I'm beginner in C programming, so I have question about basic stuff.
When I work with non-standard packages and include their headers into my projects, I'm always getting "undefined reference to function" errors. I see that header files don't contain internal code of functions, and I'm guessing that I need to link headers with the code somehow. So my question is, should I search for some libraries like dlls, which contain the functions, or should I look for C source files, and in any case, how I'm gonna link them with headers and put them all together to work in my project? I'm using CodeBlocks + MinGW.

Upvotes: 0

Views: 101

Answers (2)

am6sigma
am6sigma

Reputation: 184

When ever you have a header file in C you will have the header file Example:

//func.h
int myfunc(int x);

then you will have a source file

Blockquote

//func.c
#include "func.h"
int myfunc(int x)
{
    return x;
}

Then your source file that has main()

//main.c
#include "func.h"
int main(){
    int x = 2;
    x = myfunc(x);
    return x;
}

in your ide you include main.c and func.c in your source files. And include func.h in your header files.

I don't use code block, but basically any ide would work this way.

Upvotes: 0

abhi
abhi

Reputation: 3576

You need to build (actually link with the library) your executable against the external library you are using which you can specify using the -L path to lib gcc flag.

e.g

gcc -L path_to_lib -llib prog.c -o executable

you can use locate lib_name to know the path of the library.

Upvotes: 1

Related Questions