Author Topic: how to add header files  (Read 50484 times)

Offline iprogram

  • Single posting newcomer
  • *
  • Posts: 1
how to add header files
« on: May 19, 2011, 05:43:56 am »
Hey everyone.

I started using code:block just a while back,because i started to learn c, so i was wondering if anyone here know how to include header files.
This is what i do:
#include <FILE_NAME.h>
but it doesn't work it says it can't find that file and i have included in the folder i am writing the program in.
Can any one help.

Thanks in Advance.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 4990
    • My Best Post
Re: how to add header files
« Reply #1 on: May 19, 2011, 03:32:57 pm »

From the directions under "For your project"

- Right click on the project then select Build options
- Select the directories tab
- Add the required paths for compiler and linker.
- Add your specific libraries in the linker tab.
- Pay attention to project settings and target settings.

My re-write of the directions; just for the header file portion
Project -> "Build Options"
Make sure the correct target is highlighted on the left side; if you do not know select the project, top one.
Select Tab "Search Directories"
Select Sub-Tab "Compiler"
"Add" the path to the folder that contains the header. Single Folder per line.

Tim S.

C Programmer working to learn more about C++.
On Win7, compiling CB Trunk against wxWidgets WX_3_0_BRANCH branch.
On Debian Jessie, compiling CB Trunk against wxWidgets 2.8.12.
When in doubt, read the CB WiKi FAQ.

Offline fox2

  • Single posting newcomer
  • *
  • Posts: 2
Re: how to add header files
« Reply #2 on: January 20, 2012, 09:56:39 pm »

I'm fairly new to C programming, but I have some experience with Python. I'm facing a problem that's quite difficult for me to solve. I have actually read these boards all day, and tried to Google this issue that many seemed to had, but I still couldn't figure it out.

The problem is that I can't get additional function to work that is in separate file using the header include code.

I have read that you must add the path link to complier, linker and resource complier from search directories tab, from the project Built options, which I have done, but the error remains the same. That is, "undefined reference to "doubleUp"".

I have also tried to change the #include tag from "#include "test.h"" to "#include "C:\thePathToDir\test.h"" with no effect either.

The only thing that actually makes it work is to change the file extension to .c (as #include "test.h" -> #include "test.c") but I realize that is not the correct way to do it.

Here's a shorter code of my actual problem:

Code: [Select]

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

int main()
    int number = 4;
    int multiply = doubleUp(number);

    printf ("%d times 2 equals %d.\n", number, multiply);
    return 0;

Code: [Select]
#include "test.h"

int doubleUp(int number)
return number*2;

Code: [Select]
int doubleUp(int number);

Here's a screenshot of the Code blocks project view. Is there anything wrong there? Shouldn't the .h and .c files (test.h and test.c) be under the project three?


Any help would be deeply appreciated.
« Last Edit: January 20, 2012, 09:59:47 pm by fox2 »

Offline jens

  • Administrator
  • Lives here!
  • *****
  • Posts: 6899
    • Jens' unofficial debian-repository for the Code::Blocks - IDE
Re: how to add header files
« Reply #3 on: January 20, 2012, 10:20:36 pm »
Right click your project and chose "Add files" to add both files, or "Add files recursively" to add a subdirectory and all including files.

Jens'  Debian - nightlies (and release) : Fedora [21 - 24]- and CentOS/RedHat [5, 6 & 7] - nightlies :

Offline fox2

  • Single posting newcomer
  • *
  • Posts: 2
Re: how to add header files
« Reply #4 on: January 20, 2012, 10:29:48 pm »
Thanks for the quick reply, works like a charm now