Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - how to include own headers kept in separate folder

Tags:

c

header

First of all I am pretty much a beginner, so I am not sure how to explain what I need but I'll give it a try. (I searched but couldn't find a complete answer).

I am learning C on my own and using Code Blocks.

I want to create my own mini-library of custom functions to use in my programs.

I have a folder called "C". Inside "C", I have a folder called "Exercises" where I do all the little projects from a book.

Also inside "C", I wanna have another folder called "MyC" in which I would keep my own header files and the .c files containing the implementations of my custom functions. For example, these .h and .c would be saved in "MyC":

//test.h

#ifndef _TEST_H
#define _TEST_H

int mySum(int, int);

#endif // _TEST_H

//test.c

#include <stdio.h>
#include "test.h"

int mySum(int a, int b)
{
    return a + b;
}

So now, what I'm trying to do is to be able to create a new project in "Exercises" and not having to bring a copy of both test.h and test.c into the project, but instead just #include my test.h and do something like:

//testMain.c

#include <stdio.h>
#include <test.h>

int main(void)
{
    printf("\n2 + 1 = %d", mySum(2, 1));

    return 0;
}

I know that <> are for the standard headers, but quotes are for headers in the current folder, and that's what I don't want.

Is it possible to do this? How?

I've read about going into settings>compiler and on Search Directories add the path where I have the header but didn't work. It gives me an error "undefined reference to 'mySum'" I tried quotes and brackets on the #include.

Can you guys please give a step-by-step for what it needs to be done to be able to do this?

like image 438
Kaiser Avatar asked Mar 12 '13 17:03

Kaiser


People also ask

How do I include a .h file in another folder?

You can find this option under Project Properties->Configuration Properties->C/C++->General->Additional Include Directories. and having it find it even in lib\headers. You can give the absolute or relative path to the header file in the #include statement.

Should header files be in separate folder?

Headers and source files are distinctly different, and are used for different things, so it makes sense to separate them.

Can a header file include itself?

The content of a header file should compile correctly by itself. A header file should explicitly #include or forward declare everything it needs.

Can we create our own header files in C?

So the question arises, is it possible to create your own header file? The answer to the above is yes. header files are simply files in which you can declare your own functions that you can use in your main program or these can be used while writing large C programs.


1 Answers

for headers

If you want to move back one folder, do that: #include "../something.h".

In your case, just do this: #include "../MyC/test.h"

.. simply means go back one directory.

If you dislike doing it like that, or you want to simply #include "test.h", you can do it using -I compiler parameter, like this:

-I'../MyC/'

for c files

You need to do a simmiliar thing in compiler parameter.

gcc testMain.c ../MyC/test.c

Just remember that, .. means go back one directory!

like image 69
pampeho Avatar answered Oct 14 '22 00:10

pampeho