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

iprogram

  • Guest
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: 7588
    • My Best Post
Re: how to add header files
« Reply #1 on: May 19, 2011, 03:32:57 pm »
http://wiki.codeblocks.org/index.php?title=FAQ#Q:_I_would_like_to_compile_a_project_using_some_non-standard_libraries._How_can_I_indicate_to_CodeBlocks_that_these_libraries_and_include_files_exist.3F

From the directions under "For your project"

Quote
- 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++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline fox2

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

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
main.c
---------

#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
test.c
-------
#include "test.h"

int doubleUp(int number)
{
return number*2;
}

Code
test.h
-------
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?

[IMG=http://img862.imageshack.us/img862/1034/guia.png][/IMG]

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

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
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.

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