16

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?

Kaiser
  • 171
  • 1
  • 1
  • 4
  • Sounds like you didn't link the C file. – James M Mar 12 '13 at 17:22
  • You're getting a linker error, since the linker is unable to find the definition of few of the symbols within `MyC`. Have you built a library for all the files within your `MyC` directory ? Also looks like you're using some IDE to compile. Is that the case ? Are you open to using the command line `gcc` directly ? – Tuxdude Mar 12 '13 at 17:24
  • I am not familiar with Code Blocks, but generally you need to set up your include path and add your C file to your project from wherever it is (unless you want to make a library - but that may be too complex). You need to read your IDE documentation, paying special attention to "include files" and "adding external code" or some similar topics. –  Mar 12 '13 at 17:26
  • Yeah I thought i read about linking the .c file but i don't know how to do that. *edit* yeah, if i use the command line, and compile "testMain.c" it works only if i have all files in the same folder, but thats what i dont want – Kaiser Mar 12 '13 at 17:36
  • You should add the file `test.c` to your project. Right-click your project > Add files ... > then select it. When you compile your project, it will be compiled and linked with it. – Karim ElDeeb Mar 12 '13 at 18:12
  • So, I got it to work like this: On the global settings, included the path to where my header is, and then on the Project's Build Options, on the Linker settings included the path to the .o file..... Now a related question... I used absolute paths because i do projects in different folders. Should I use relative paths instead? – Kaiser Mar 12 '13 at 21:38
  • If you want to refer to a file, you can use either relative or absolute [path](http://en.wikipedia.org/wiki/Path_%28computing%29). A relative path is the one relative to the _working directory_, so if you are in `MyC` (the current working directory) and you are referring to a file inside it you simply type `tect.c`. An absolute path is the one pointing to the same file _regardless_ of the working directory, so to refer to the same file you would type `C:\C\MyC\test.c` assuming `C:` drive is where you keep the `C` folder. – Karim ElDeeb Mar 13 '13 at 09:40
  • yeah i know the difference, I was just asking which one would be preferred to use for the linker settings and search directories. My guess is the absolute. – Kaiser Mar 15 '13 at 13:53

4 Answers4

9

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!

pampeho
  • 704
  • 6
  • 12
  • Strongly recommend against using relative pathnames in header names that include `..` — see [What are the benefits of a relative path such as `"../include/header.h"` for a header?](https://stackoverflow.com/questions/597318/) – Jonathan Leffler May 27 '19 at 16:07
3

When you compile you need to include the -I option. For example:

gcc -I<path-to-headers> <path-to>/test.c testMain.c -o test_driver
Mosby
  • 1,291
  • 1
  • 10
  • 14
  • 1
    Although this answer tells how to specify the include path, it does not answer the OP's error `undefined reference to something` - which is a linker error. – Tuxdude Mar 12 '13 at 17:25
  • Fixed, should be more complete – Mosby Mar 12 '13 at 17:27
3

You've added the header folder, so that's fine. You can #include as normal. You'd be getting a 'file not found' error if it couldn't find your header.

But you will also need to link against the object code in this directory, and you'll need to specify which object files to search for your functions. There should be a setting in your IDE to add compiler options, e.g. ../MyC/test.o. If you haven't already compiled the code in these functions, you'll need to specify the .c file instead.

teppic
  • 8,039
  • 2
  • 24
  • 37
  • Thanks this helped. I got it to work like this: On the global settings, included the path to where my header is, and then on the Project settings, on the Linker settings included the path to the .o file – Kaiser Mar 12 '13 at 21:31
0

Considering that "folder_to_code" contains test.h and test.c,

you can add an include folder using the gcc command:

gcc -I folder_to_code folder_to_code/test.c testMain.c -Wall -o program.exe

This way you can add #include "test.h" to the code without problems.

Community
  • 1
  • 1
Felipe Lavratti
  • 2,887
  • 16
  • 34