Thread: questions about djgpp and header-files

  1. #1
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140

    questions about djgpp and header-files

    hi!

    i use djgpp to compile my plain c-code.

    normally i compile with the following option:

    Code:
    gcc file.c -o file.exe -lm
    how do i have to change the command if i want to include my own written header-file (*.h)?

    that was question one - here is question two (you guessed it, it's about own header-files ):

    for example: my header-file should only include this two functions:

    Code:
    int my_newline()
    {
     printf( "\n" );
    
     return 0;
    }
    
    int my_lines(int x)
    {
     int y;
     for(y=1; y<=x; y=y+1)
     {
      my_newline();
     }
    
     return 0;
    }
    what else do i have to include in my header-file?
    explain it for a newbie please

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Header files should contain protypes, not definitions, unless they are inline. This is more common in C++. You headers should look like this:
    called: myheader.h
    Code:
    #ifndef MYHEADER_H
    #define MYHEADER_H
    
    int my_newline();
    int my_lines(int);
    
    #endif
    That's it. Than define those functions in a cpp file and include the header, for example:

    mysource.cpp
    Code:
    #include "myheader.h"
    #include<stdio.h>
    
    int my_newline()
    {
     printf( "\n" );
    
     return 0;
    }
    
    int my_lines(int x)
    {
     int y;
     for(y=1; y<=x; y=y+1)
     {
      my_newline();
     }
    
     return 0;
    }
    Than in your main program:
    Code:
    #include "myheader.h"
    #include<stdio.h>
    
    int main()
    {
       printf("Hello");
       my_newline();
       my_lines(30);
       my_newline();
       return o;
    }
    And as far as DJGPP goes. I have no clue, but my guess would be:
    gcc file.c mysource.c myheader.h -o file.exe -lm

    Ofcourse this command could be very wrong. No clue.

  3. #3
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    well, currently my program is structured like this:

    1) function-prototypes

    2) functions

    3) main program

    so i should save 1) as header, 2) as an extra .c and 3) is the only thing which will remain in the main-program.c ?

    you said something about cpp, but i am learning c at the moment and don't want to use any cpp-code...

    does this work in plain c, too?

    don't i have to include the extra.c (the functions) in the main program with an #include ? or is the header file there enough?

  4. #4
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    about compiling with djgpp (from the manual):

    compile and link a single file c program:

    gcc myfile.c -o myfile.exe -lm

    that's what i do.

    compile a c source file into an object file:

    gcc -c -wall myfile.c

    => produces object file 'myfile.o'

    and filnally: link several c opject files into an exe:

    gcc -o myprog.exe mymain.o mysub1.o mysub2.o

    => produces exe 'myprog.exe'

    but i can also combine the compilaton and link steps:

    gcc -wall -o myprog.exe mymain.c mysub1.c mysub2.c

    i think this is the syntax i am searching for, but: can i just include a myheader.h in this code syntax? and: what are object files for?

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    I use a C/C++ compiler. Yes, you can just use the .c extension. You do not include a .c file at any time. They don't work like that. Yes you seem to have it mostly right. At least it sounds like it.

    1) function-prototypes

    2) functions

    3) main program
    I'd only slightly revise this to:
    1) function prototypes
    2)function definitions
    3)main program
    1)prototypes declare the existance of the functions but do not allocate memory. Use the #ifndef, #define, #endif preprocessor directives so that your header file is not included more than once in your source files (.c).

    2)Function definitions. Requires prototype list and body of function. Include header files that contain the function protypes and standard library #includes, such as #include<stdio.h>, etc.

    3)Main function. Include header files once again. Include standard library files again. You can call your own functions now from main that were defined in the implimentation .c file, step 2.

  6. #6
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    i think this is the syntax i am searching for, but: can i just include a myheader.h in this code syntax? and: what are object files for?
    The object files are an intermediate step before the files must be linked. Actually just try to do it individually. I think the header must become an object too than linked. I use Microsoft Visual Studio.NET so I do not have to use the command line to build my programs. It is a GUI. I really don't like the command line.

  7. #7
    Registered User dune911's Avatar
    Join Date
    Sep 2001
    Posts
    140
    yeah - no more problems!

    i just copied my three files in ms vc++ 5.0, tried to compile and it worked fine! no more trouble including header-files in the command-line syntax from djgpp(gcc)!

    what exactly is visual studio.net? i have a beta 2 version here, but don't installed it, yet... i think i don't really need it as a newbie, am i right?

    thanks for your help writing the header!

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Visual Studios.NET is basicly Visual Studios 7 (with a new name). New versions of previous products, focused more on the .NET idea from Microsoft.

    Quzah.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > gcc -wall -o myprog.exe mymain.c mysub1.c mysub2.c
    For small projects this is the way to go.

    > about compiling with djgpp (from the manual):
    The rest of what you read is more applicable to large projects containing 10's or 100's of source files.
    When you have 100 source files, and you're only editing one of them, you can save yourself 99 compilations if you've done this first
    &nbsp; gcc -c -wall myfile.c
    The link step
    &nbsp; gcc -o myprog.exe mymain.o mysub1.o mysub2.o
    Is a much simpler (less time consuming operation) than compiling.

    When you are ready, read about the 'make' tool which allows you to automate this process.

    > but: can i just include a myheader.h in this code syntax?
    You don't compile header files - header files describe an interface (function prototypes) to functions in other modules. The things you compile are .c files.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing a directory and all the files within it
    By misplaced in forum C++ Programming
    Replies: 2
    Last Post: 12-15-2004, 02:08 PM
  2. Djgpp error, possibly with the headings i'm using
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 07-24-2002, 11:02 PM
  3. djgpp long header file name problem or something
    By v0id in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-08-2002, 07:25 PM
  4. DJGPP project problems
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-08-2002, 07:16 PM