Results 1 to 5 of 5

Thread: Make your own .h file

  1. #1
    sql_lall
    Guest

    Question Make your own .h file

    I know this has probably been asked many times, but here we go again anyway:

    I have a function i created that takes in two variables, prints stuff out, then returns another integer:

    int printit(int* days, int first)
    {
    ....
    return 0;
    }

    and i was wondering if i could make my own .h file, so that i could use the function without having to write it out for each source.

    i.e. up the top of some code have:
    #include <iostream.h>
    #include <myownh.h>

    Also, can i have a constant array defined as well??
    If so, could someone post some example code?? Thankyou!

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Creating a header is easy. Write a text file and save it with a .h extension.
    A header only contains function prototypes, so in your case:

    int printit(int* days, int first);

    add this file to every project where you want to use it and write
    #include "myheader.h"
    You also must add the source file or object file where the function code is inside to the project.

    What do you mean with "constant array"?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    sql_lall
    Guest

    Red face Thanks

    Thanks for your help.
    So you're saying i can't actually have a function in a .h file that i don't have to include the source to each time a make a new program??? See, was i was wanting to do was make a compilation of my most-used functions, store them in a .h file so i didn't have to write them out each time.

    Also, constant array in this case means an array that won't be edited. (Just has 31 values that stay the same.)

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    const type name[size];


    You can write one seperate source file and a fitting header file. Then include both files in every project you need them.

    To save compilation time you can create an object file:
    Right-click on the source file and choose compile. A name.cpp file is then compiled to the file name.obj. Simply include this file instead of the source file in the projects you need your functions, then they don't need to be compiled over and over.

    To reduce the size of your executable, create a Win32 Static Library project and choose your source file as the only file of the project. Then build the project and you'll get a .lib file: a static library. When you need the functions, simply add the file name to the list in the linker settings (the file must be in a known directory). The linker will only include the functions you actually use.

    All versions require a header file.

    The last method would be a dll, but I won't explain it because it's more complicated and in your case (a small collection fo some often-used functions) the contras are bigger than the pros.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    sql_lall
    Guest

    Exclamation Don't worry

    I've figured it out. All i have to do is make a class, and have one of the member functions the function i want to use.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width