how can I create a header file ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scienceman88
    New Member
    • Aug 2009
    • 85

    how can I create a header file ?

    I've found thinks on this in other places, But even when I used premade headers from my compiler ( Dev C++) which should be called Devil C++ in my opinion, and replaced all the code from one and altered it to be in C ( as to my knowledge) I keep on getting an error or 2 ( I've seen others talk about this error though). can you guys walk me through making my own headers( if i learn how to do non standard based C headers I might try and make a mini program that I think could work as an OS, And yes I've tried assembly language before ( I think I can ask my uncle as one of them has done robotics). the errors I get are

    [Linker error] undefined reference to `WinMain@16'
    ld returned 1 exit status
    C:\Users\roddy\ Documents\RAD Studio\7.0\Demo s\CPP\Apps\Imag eProc\Makefile. win [Build Error] [Project1.exe] Error 1

    my code is:
    random.c
    Code:
    #include <math.h>
    #include "random.h"
    double xrt( unsigned long long i, unsigned long long y){
    double x = 0.0;
    x=pow(y,1/i);
    return x;
    }
    double log_x( unsigned long long c, unsigned long long p){
    double x = 0.0;
    x=log(c)/log(p);
    return x;
    }
    and

    Code:
    //---------------------------------------------------------------------------
    
    #ifndef randomH
    #define randomH
    //---------------------------------------------------------------------------
    #include <math.h>
    
    #endif
    
    double xrt( unsigned long long i, unsigned long long y);
    double log_x(unsigned long long c,unsigned long long p);
    the original header and source I took some of this from was ABOUT.h,About.c I believe.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    There is nothing special about header files. They are just text files. They are only called header files because usually they are included at the beginning (or head) of the implementation file.

    The .h extension is not required but it customary in C. C++ standard header files have no extension.
    You do need to be careful of this:

    Code:
    #include "myheader.h"
    This tells the preprocesspr to first look for the header file in the same directory as the source file. If not there, then look in the "standard places".

    Whereas this:

    Code:
    #include <myheader.h>
    tells the preprocesssor to look only in the "standard places" for the header file.

    A "standard place" is a predefined path. Consult your compiler documentation for instructions on how to specify a standard place.


    Next, header files need to be able to include themselves without causing re-definition errors. You need to so this:

    Code:
    #ifndef AUNIQUETAG
    #define AUNIQUETAG
     
    ...contents of the header file go here...
    
    #endif
    This is called an inclusion guard. It only comes into placy if the header file is included more than once in the same source file.

    And that's all there is to it.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      On looking at your post. I see a missing reference to WinMain16.

      That tells me: a) you are using Visual Studio and b) you have incorrectly set up your project.

      What you have is a Windows project to produce a Windows program. Probably you wanted a console application so you could use main().

      If so do this:

      1) Create a Win32 project.
      2) When the wizard appears DO NOT CLICK FINISH.
      3) Instead click Application Settings
      4) Select Console Application and Empty Project
      5) Now click Finish.

      TheWinMain16 error will go away.

      Comment

      • scienceman88
        New Member
        • Aug 2009
        • 85

        #4
        I obviously am missing something things I've tried are:

        using compilers headers as a template,
        copying a code said to be accurate,

        both of these gave me the same result then i took something out that I thought was just for the example and I got more errors put it back in exactly as I remembered it and got even more errors I think.

        Comment

        • scienceman88
          New Member
          • Aug 2009
          • 85

          #5
          a is completely wrong and b is likely as I've never done this before.

          Comment

          • scienceman88
            New Member
            • Aug 2009
            • 85

            #6
            not to be mean but it would be nice if you read my first post it clearly makes fun of the fact I'm using Dev C++

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              So there is no need to use the standard headers as temlates, just include them as required and create you own headers with your own declarations (and definitions where appropriate).

              weaknessforcats is just overlooking the fact you can actually make Windows programs in a variety of IDEs and assumed you have used the most common one. However the point is that you told the toolset you where building a Windows program so it expected your code to contain a WinMain and it didn't. The normally cause for this is that the user meant to set up a console application project but set up a Windows application project so their code contains main not WinMain. However I see no evidence of your program containing either.

              Talking of Dev-C++ that IDE is getting rather long in the tooth now, you should consider moving onto a more up to date IDE with a more up to date compiler, ones you can get for free are Code:Blocks or Visual Studio 2008 Express Edition.

              Comment

              • scienceman88
                New Member
                • Aug 2009
                • 85

                #8
                I still don't know how to make the project type to make the headers why care about a compiler change if I can't figure out what project type i need for the job in dev c++ I don't see anything close to anything that could be a header setup I know I need both the header and a source code I don't see how to do that.

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  That's easy, if you learn what you need to do in Dev-C++ then change IDE you will need to learn it again because how projects get set-up is IDE dependent they all have their own slightly different ways. Change first and then learn and you only have to do the job once.

                  There is no header set-up, a few IDEs (Eclipse does for example) have a create header file option but most don't. It is just a matter of creating a new file and giving it an appropriate extension.

                  The project is about setting the appropriate options to the compiler and linker, nothing to do with the source code, all the source files, header files (h) and code files (c or cpp) are just simple text files that should easily transfer between different IDEs.

                  You appear to be trying to treat 2 jobs as one, the 2 jobs you have are

                  1. Creating a project with the right settings
                  2. Creating you header file (and other source code files)

                  You seem to be looking for a create a project that creates a header file option but that just doesn't exist because it doesn't make sense, most projects contain multiple header files, I regularly use 1 header file for every code file plus a few extra header files.

                  Comment

                  • scienceman88
                    New Member
                    • Aug 2009
                    • 85

                    #10
                    I'm less than 20 minute away from a trial of visual studio professional so how do I do it in that one ? so I use source file creation ?

                    Comment

                    • Banfa
                      Recognized Expert Expert
                      • Feb 2006
                      • 9067

                      #11
                      Trying to remember, its something like

                      File -> New -> Project

                      Then in the box that appears select console application, it will give you the option of an empty project or a "Hello World" project. Which you choose is your choice the "Hello World" creates a few files and puts a "Hello World" program in them, the empty project doesn't. Or see the instructions in post #3.

                      Having got the project you can create new files by right clicking on the project in the Solution Explorer (the bit with the file list) and selecting Add -> New Item. If you have existing source files you can add the to the project by right clicking on the project and selecting Add -> Existing Item.

                      Comment

                      • scienceman88
                        New Member
                        • Aug 2009
                        • 85

                        #12
                        sounds like the same thing I'm doing already

                        Comment

                        • Banfa
                          Recognized Expert Expert
                          • Feb 2006
                          • 9067

                          #13
                          Very possibly, I didn't say you would be doing anything much different only that you shouldn't use an out of date IDE (and toolset). There are only so many ways to add a header file to a project.

                          Comment

                          • scienceman88
                            New Member
                            • Aug 2009
                            • 85

                            #14
                            I did everything I can think of to fix errors here's my new list:

                            Warning 1 warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data c:\users\roddy\ documents\visua l studio 2008\projects\r oddy\roddy\rodd y.c 6 roddy

                            Error 2 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStar tup MSVCRTD.lib roddy

                            Error 3 fatal error LNK1120: 1 unresolved externals C:\Users\roddy\ Documents\Visua l Studio 2008\Projects\r oddy\Debug\rodd y.exe roddy

                            Comment

                            • weaknessforcats
                              Recognized Expert Expert
                              • Mar 2007
                              • 9214

                              #15
                              The first error is converting a double (8 bytes) to a float (4 bytes) and that may mean a loss of accuracy. The most common cause is:

                              Code:
                              float data = 1.0;
                              because 1.0 is a double. You need:

                              Code:
                              float data = 1.0f;
                              You see this also in calling a function with a float argument using a symbolic constant like 1.0.

                              The second error is lack of a main() function in your program.

                              Comment

                              Working...