problem with compilation

Discussion in 'C Programming' started by foodic, Mar 20, 2005.

  1. foodic

    foodic Guest

    Hi all
    I have a little bit of programming experience on windows 98 using C.
    But this is my first attempt to compile a program on
    Linux using gcc.I have two files s1.c and s1.h and while
    compiling i am getting compilation problem,
    here is the content of s1.c

    #include <stdio.h>
    #include <s1.h>
    void hi();
    int main(){
    hi();
    }
    here is the content of s1.h
    void hi(){
    printf ("Hi all\n");
    }

    when i compile this program,

    gcc s1.c
    it gives error like,
    s1.c:2:16: s1.h: No such file or directory


    and also i have tried
    gcc s1.h s1.c
    it gives error like,

    gcc: compilation of header file requested
    s1.c:2:16: s1.h: No such file or directory

    Please somebody explain how to compile it

    thanks
    divya
     
    foodic, Mar 20, 2005
    #1
    1. Advertising

  2. foodic

    Helmut Weber Guest

    The include-statement is wrong. You have to
    use

    #include "s1.h"

    instead of

    #include <s1.h>

    because s1.h is no system include file, but resides in your own
    working directory.

    Compile with: gcc s1.c

    Regards
    Helmut Weber

    "foodic" <> schrieb im Newsbeitrag
    news:...
    > Hi all
    > I have a little bit of programming experience on windows 98 using C.
    > But this is my first attempt to compile a program on
    > Linux using gcc.I have two files s1.c and s1.h and while
    > compiling i am getting compilation problem,
    > here is the content of s1.c
    >
    > #include <stdio.h>
    > #include <s1.h>
    > void hi();
    > int main(){
    > hi();
    > }
    > here is the content of s1.h
    > void hi(){
    > printf ("Hi all\n");
    > }
    >
    > when i compile this program,
    >
    > gcc s1.c
    > it gives error like,
    > s1.c:2:16: s1.h: No such file or directory
    >
    >
    > and also i have tried
    > gcc s1.h s1.c
    > it gives error like,
    >
    > gcc: compilation of header file requested
    > s1.c:2:16: s1.h: No such file or directory
    >
    > Please somebody explain how to compile it
    >
    > thanks
    > divya
     
    Helmut Weber, Mar 20, 2005
    #2
    1. Advertising

  3. foodic

    Joe Wright Guest

    foodic wrote:
    > Hi all
    > I have a little bit of programming experience on windows 98 using C.
    > But this is my first attempt to compile a program on
    > Linux using gcc.I have two files s1.c and s1.h and while
    > compiling i am getting compilation problem,
    > here is the content of s1.c
    >
    > #include <stdio.h>
    > #include <s1.h>
    > void hi();
    > int main(){
    > hi();
    > }
    > here is the content of s1.h
    > void hi(){
    > printf ("Hi all\n");
    > }
    >
    > when i compile this program,
    >
    > gcc s1.c
    > it gives error like,
    > s1.c:2:16: s1.h: No such file or directory
    >
    >
    > and also i have tried
    > gcc s1.h s1.c
    > it gives error like,
    >
    > gcc: compilation of header file requested
    > s1.c:2:16: s1.h: No such file or directory
    >
    > Please somebody explain how to compile it
    >
    > thanks
    > divya


    You need a better C book. Headers should not contain function
    definitions (or any other definitions). Headers are for declarations.
    And yes, '#define FOO 10' is a declaration, not a definition. Hence s1.h
    is useless and even an error. s1.c should look like..

    #include <stdio.h>

    void hi(void) {
    printf("Hi all\n");
    }

    int main(void) {
    hi();
    return 0;
    }

    If you want to complicate it a little, arrange for hi() to be in s1.c
    after main() and provide s1.h to declare the prototype. So s1.h looks like..

    #ifndef S1_H
    #define S1_H
    void hi(void);
    #endif

    ... and s1.c looks like ..

    #include <stdio.h>
    #include "s1.h"

    int main(void) {
    hi();
    return 0;
    }

    void hi(void) {
    printf("Hi all\n");
    }

    "The C Programming Language", Second Edition, Kernighan and Ritchie will
    cost you about $40 and will be the best money you've spent (if you read
    the book).
    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Mar 20, 2005
    #3
  4. foodic

    pete Guest

    Parts of a C program, Re: problem with compilation

    Joe Wright wrote:

    > You need a better C book. Headers should not contain function
    > definitions (or any other definitions). Headers are for declarations.
    > And yes, '#define FOO 10' is a declaration, not a definition.
    > Hence s1.h is useless and even an error. s1.c should look like..


    Definitions are also declarations, which is why CBFalconer
    likes to put the definitions of other functions, above main.

    #define FOO 10 is a preprocessor directive.

    Each byte in a C program is one of 4 things:
    1 part of a comment
    2 part of a preprocessor directive
    3 part of an external declaration
    4 all other bytes in a C program are white space

    Function definitions are unique among declarations in two ways:
    1 they are not terminated by a semicolon ;
    2 they are always external

    --
    pete
     
    pete, Mar 20, 2005
    #4
  5. foodic wrote:
    > Hi all
    > I have a little bit of programming experience on windows 98 using C.
    > But this is my first attempt to compile a program on
    > Linux using gcc.I have two files s1.c and s1.h and while
    > compiling i am getting compilation problem,
    > here is the content of s1.c
    >
    > #include <stdio.h>
    > #include <s1.h>

    #include "s1.h"
    and make sure the include path is correct. The way to do this is
    covered in your documentation. If that documentation is insufficiently
    clear, ask about it in a newsgroup like
    gnu.gcc.help
    or
    linux.dev.gcc


    > void hi();


    This should be in s1.h, so is unneeded here.
    [...]

    > here is the content of s1.h
    > void hi(){

    Specifying no arguments, 'void ji(void)' is a good idea.

    > printf ("Hi all\n");
    > }


    headers are not the place to put definitions of functions or objects.
    Put this is a separate compilation unit (*.c file); "s1.h" should have
    simply
    void hi(void);
     
    Martin Ambuhl, Mar 20, 2005
    #5
  6. foodic

    CBFalconer Guest

    Re: Parts of a C program, Re: problem with compilation

    pete wrote:
    >

    .... snip ...
    >
    > Function definitions are unique among declarations in two ways:
    > 1 they are not terminated by a semicolon ;
    > 2 they are always external


    static int foo(...) {...} /* not exactly external */
    static int bar(...); /* forward declaration, not external */

    --
    "I conclude that there are two ways of constructing a software
    design: One way is to make it so simple that there are obviously
    no deficiencies and the other way is to make it so complicated
    that there are no obvious deficiencies." -- C. A. R. Hoare
     
    CBFalconer, Mar 20, 2005
    #6
  7. foodic

    Joe Wright Guest

    Re: Parts of a C program, Re: problem with compilation

    pete wrote:
    > Joe Wright wrote:
    >
    >
    >>You need a better C book. Headers should not contain function
    >>definitions (or any other definitions). Headers are for declarations.
    >>And yes, '#define FOO 10' is a declaration, not a definition.
    >>Hence s1.h is useless and even an error. s1.c should look like..

    >
    >
    > Definitions are also declarations, which is why CBFalconer
    > likes to put the definitions of other functions, above main.
    >


    Like CBFalconer, I do that too, all the time. Nowhere do I say that a
    definition is not a declaration.

    > #define FOO 10 is a preprocessor directive.
    >


    Of course it is. Declarations refer to variables and other objects. I
    was making the point that #define doesn't indicate a definition of an
    object and therefore create memory for it.

    > Each byte in a C program is one of 4 things:
    > 1 part of a comment
    > 2 part of a preprocessor directive
    > 3 part of an external declaration
    > 4 all other bytes in a C program are white space
    >


    Nonsense. I give you 1, 2 and 4 but your item 3 is too much. What part of ..

    static int foo;

    ... is an external declaration?

    > Function definitions are unique among declarations in two ways:
    > 1 they are not terminated by a semicolon ;


    Yes.

    > 2 they are always external


    Nonsense. They are external by default unless qualified 'static', in
    which case they are not external.

    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Mar 20, 2005
    #7
  8. foodic

    CBFalconer Guest

    Re: Parts of a C program, Re: problem with compilation

    Joe Wright wrote:
    > pete wrote:
    >>

    .... snip ...
    >>
    >> Definitions are also declarations, which is why CBFalconer
    >> likes to put the definitions of other functions, above main.

    >
    > Like CBFalconer, I do that too, all the time. Nowhere do I say
    > that a definition is not a declaration.


    Which has a multitude of advantages. I never make a mistake in
    matching the function header to its prototype, and it caters to
    extreme laziness in that I never type something twice, or even have
    to cut and paste a header. Whenever I am looking for the
    definition of a function I know precisely in which direction in the
    source to look. :)

    I also like to have a clear demarcation between the code for one
    function and the header of the next. Marking the trailing '}' with
    a comment indicating which function it ends is also handy when
    rummaging about in the source. Thus:

    } /* foo */

    /* --------------- */

    /* Do foo(lish) things in a bar */
    static int bar(...)
    {

    --
    "I conclude that there are two ways of constructing a software
    design: One way is to make it so simple that there are obviously
    no deficiencies and the other way is to make it so complicated
    that there are no obvious deficiencies." -- C. A. R. Hoare
     
    CBFalconer, Mar 20, 2005
    #8
  9. foodic

    Tim Rentsch Guest

    Re: Parts of a C program, Re: problem with compilation

    pete <> writes:

    > Each byte in a C program is one of 4 things:
    > 1 part of a comment
    > 2 part of a preprocessor directive
    > 3 part of an external declaration
    > 4 all other bytes in a C program are white space


    Mostly I agree with this (glossing over the discussions about 'static'
    vs "external" and possible declaration/definition distinctions).

    But, wouldn't you agree that \ NEWLINE aren't exactly like
    other white space? (And they certainly don't fall into the
    other three categories.) I feel a little bit uneasy referring
    to \ NEWLINE as "white space".
     
    Tim Rentsch, Mar 21, 2005
    #9
  10. foodic

    pete Guest

    Re: Parts of a C program, Re: problem with compilation

    Tim Rentsch wrote:
    >
    > pete <> writes:
    >
    > > Each byte in a C program is one of 4 things:
    > > 1 part of a comment
    > > 2 part of a preprocessor directive
    > > 3 part of an external declaration
    > > 4 all other bytes in a C program are white space

    >
    > Mostly I agree with this (glossing over the discussions about 'static'
    > vs "external" and possible declaration/definition distinctions).
    >
    > But, wouldn't you agree that \ NEWLINE aren't exactly like
    > other white space? (And they certainly don't fall into the
    > other three categories.) I feel a little bit uneasy referring
    > to \ NEWLINE as "white space".


    N869
    7.4.1.9 The isspace function
    Description
    [#2] The
    standard white-space characters are the following: space
    (' '), form feed ('\f'), new-line ('\n'), carriage return
    ('\r'), horizontal tab ('\t'), and vertical tab ('\v')

    --
    pete
     
    pete, Mar 21, 2005
    #10
  11. foodic

    pete Guest

    Re: Parts of a C program, Re: problem with compilation

    Joe Wright wrote:
    >
    > pete wrote:
    > > Joe Wright wrote:
    > >
    > >
    > >>You need a better C book. Headers should not contain function
    > >>definitions (or any other definitions). Headers are for declarations.
    > >>And yes, '#define FOO 10' is a declaration, not a definition.
    > >>Hence s1.h is useless and even an error. s1.c should look like..

    > >
    > >
    > > Definitions are also declarations, which is why CBFalconer
    > > likes to put the definitions of other functions, above main.
    > >

    >
    > Like CBFalconer, I do that too, all the time. Nowhere do I say that a
    > definition is not a declaration.
    >
    > > #define FOO 10 is a preprocessor directive.
    > >

    >
    > Of course it is. Declarations refer to variables and other objects. I
    > was making the point that #define doesn't indicate a definition of an
    > object and therefore create memory for it.
    >
    > > Each byte in a C program is one of 4 things:
    > > 1 part of a comment
    > > 2 part of a preprocessor directive
    > > 3 part of an external declaration
    > > 4 all other bytes in a C program are white space
    > >

    >
    > Nonsense. I give you 1, 2 and 4 but your item 3 is too much.
    > What part of ..
    >
    > static int foo;
    >
    > .. is an external declaration?


    K&R2 A10.2 External Declarations
    The term "external" refers to their location outside functions,
    and is not directly connected with the extern keyword;
    the storage class for an externall-declared object may be
    left empty, or it may be specified as extern or static.

    N869
    6.9 External definitions

    [#4] As discussed in 5.1.1.1, the unit of program text after
    preprocessing is a translation unit, which consists of a
    sequence of external declarations. These are described as
    ``external'' because they appear outside any function (and
    hence have file scope). As discussed in 6.7, a declaration
    that also causes storage to be reserved for an object or a
    function named by the identifier is a definition.

    --
    pete
     
    pete, Mar 21, 2005
    #11
  12. foodic

    pete Guest

    Re: Parts of a C program, Re: problem with compilation

    Tim Rentsch wrote:
    >
    > pete <> writes:
    >
    > > Each byte in a C program is one of 4 things:
    > > 1 part of a comment
    > > 2 part of a preprocessor directive
    > > 3 part of an external declaration
    > > 4 all other bytes in a C program are white space


    > possible declaration/definition distinctions


    N869
    6.9 External definitions
    [#5] An external definition is an external declaration that
    is also a definition of a function or an object.

    --
    pete
     
    pete, Mar 21, 2005
    #12
  13. foodic

    pete Guest

    Re: Parts of a C program, Re: problem with compilation

    CBFalconer wrote:
    >
    > pete wrote:
    > >

    > ... snip ...
    > >
    > > Function definitions are unique among declarations in two ways:
    > > 1 they are not terminated by a semicolon ;
    > > 2 they are always external

    >
    > static int foo(...) {...} /* not exactly external */
    > static int bar(...); /* forward declaration, not external */


    "External" means something else in C.

    --
    pete
     
    pete, Mar 21, 2005
    #13
  14. foodic

    CBFalconer Guest

    Re: Parts of a C program, Re: problem with compilation

    pete wrote:
    > Joe Wright wrote:
    >>

    .... snip ...
    >>
    >> Nonsense. I give you 1, 2 and 4 but your item 3 is too much.
    >> What part of ..
    >>
    >> static int foo;
    >>
    >> .. is an external declaration?

    >

    .... snip ...
    >
    > N869
    > 6.9 External definitions


    I just reared back and let fly on that, and ran into the same
    quote. It does justify hia statement, weird though it may be.

    I even deleted the reply containing the rearing! A useful subject
    for trolling purposes in this group.

    --
    "I conclude that there are two ways of constructing a software
    design: One way is to make it so simple that there are obviously
    no deficiencies and the other way is to make it so complicated
    that there are no obvious deficiencies." -- C. A. R. Hoare
     
    CBFalconer, Mar 21, 2005
    #14
  15. "foodic" <> wrote in message
    news:...
    > Hi all
    > I have a little bit of programming experience on windows 98 using C.
    > But this is my first attempt to compile a program on
    > Linux using gcc.I have two files s1.c and s1.h and while
    > compiling i am getting compilation problem,

    ....
    > when i compile this program,
    >
    > gcc s1.c
    > it gives error like,
    > s1.c:2:16: s1.h: No such file or directory


    That's because a non-system header file should be "file.h" instead of
    <file.h>.

    > Please somebody explain how to compile it


    The "normal" way to do what you want is:

    /*** main.c ***/
    #include "s1.h"

    int main() {
    hi();
    }

    /*** s1.h ***/
    void hi();

    /*** s1.c ***/
    #include <stdio.h>

    void hi() {
    printf ("Hi all\n");
    }

    <OT reason="implementation-specific">

    Then to compile, you do:

    gcc -c main.c
    gcc -c s1.c
    gcc -o s1 main.o s1.o

    This will give you an executable named "s1". If you get into projects much
    larger than this, you'll want to learn how to build a Makefile, but for one
    or two files it's simpler to do it by hand.

    </OT>

    S

    --
    Stephen Sprunk "Stupid people surround themselves with smart
    CCIE #3723 people. Smart people surround themselves with
    K5SSS smart people who disagree with them." --Aaron Sorkin
     
    Stephen Sprunk, Mar 21, 2005
    #15
  16. Re: Parts of a C program, Re: problem with compilation

    On Sun, 20 Mar 2005 19:49:09 +0000, CBFalconer wrote:

    > pete wrote:
    >>

    > ... snip ...
    >>
    >> Function definitions are unique among declarations in two ways:
    >> 1 they are not terminated by a semicolon ;
    >> 2 they are always external

    >
    > static int foo(...) {...} /* not exactly external */


    This is external becuase it cannot be inside another function i.e. it is
    at file scope.

    > static int bar(...); /* forward declaration, not external */


    The text above refers specifically to function definitions, and this isn't.

    Lawrence
     
    Lawrence Kirby, Mar 21, 2005
    #16
  17. foodic

    Joe Wright Guest

    Re: Parts of a C program, Re: problem with compilation

    pete wrote:
    > Joe Wright wrote:
    >
    >>pete wrote:
    >>
    >>>Joe Wright wrote:
    >>>

    [ much snippage ]
    >>
    >>>Each byte in a C program is one of 4 things:
    >>>1 part of a comment
    >>>2 part of a preprocessor directive
    >>>3 part of an external declaration
    >>>4 all other bytes in a C program are white space
    >>>

    >>
    >>Nonsense. I give you 1, 2 and 4 but your item 3 is too much.
    >>What part of ..
    >>
    >>static int foo;
    >>
    >>.. is an external declaration?

    >
    >
    > K&R2 A10.2 External Declarations
    > The term "external" refers to their location outside functions,
    > and is not directly connected with the extern keyword;
    > the storage class for an externall-declared object may be
    > left empty, or it may be specified as extern or static.
    >
    > N869
    > 6.9 External definitions
    >
    > [#4] As discussed in 5.1.1.1, the unit of program text after
    > preprocessing is a translation unit, which consists of a
    > sequence of external declarations. These are described as
    > ``external'' because they appear outside any function (and
    > hence have file scope). As discussed in 6.7, a declaration
    > that also causes storage to be reserved for an object or a
    > function named by the identifier is a definition.
    >


    Touché. Getting stuck with these 'points' is starting to get annoying. I
    also note, grudgingly, that you are getting better at it. I have learned
    something. Thanks.

    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Mar 21, 2005
    #17
  18. foodic

    Tim Rentsch Guest

    Re: Parts of a C program, Re: problem with compilation

    pete <> writes:

    > Tim Rentsch wrote:
    > >
    > > pete <> writes:
    > >
    > > > Each byte in a C program is one of 4 things:
    > > > 1 part of a comment
    > > > 2 part of a preprocessor directive
    > > > 3 part of an external declaration
    > > > 4 all other bytes in a C program are white space

    >
    > > glossing over ... possible declaration/definition distinctions


    ^^^^^^^^^^^^^^^^^ [snipped text restored]

    > N869
    > 6.9 External definitions
    > [#5] An external definition is an external declaration that
    > is also a definition of a function or an object.


    It's because I wanted to avoid the topic and not take a position on it
    that I used the phrase "glossing over", together with "possible".
     
    Tim Rentsch, Mar 22, 2005
    #18
  19. foodic

    Tim Rentsch Guest

    Re: Parts of a C program, Re: problem with compilation

    pete <> writes:

    > Tim Rentsch wrote:
    > >
    > > pete <> writes:
    > >
    > > > Each byte in a C program is one of 4 things:
    > > > 1 part of a comment
    > > > 2 part of a preprocessor directive
    > > > 3 part of an external declaration
    > > > 4 all other bytes in a C program are white space

    > >
    > > Mostly I agree with this (glossing over the discussions about 'static'
    > > vs "external" and possible declaration/definition distinctions).
    > >
    > > But, wouldn't you agree that \ NEWLINE aren't exactly like
    > > other white space? (And they certainly don't fall into the
    > > other three categories.) I feel a little bit uneasy referring
    > > to \ NEWLINE as "white space".

    >
    > N869
    > 7.4.1.9 The isspace function
    > Description
    > [#2] The
    > standard white-space characters are the following: space
    > (' '), form feed ('\f'), new-line ('\n'), carriage return
    > ('\r'), horizontal tab ('\t'), and vertical tab ('\v')


    It's clear that \ NEWLINE sequences are not intended as white-space
    in the language of the stanard document. For example, they are
    treated in separate phases -- \ NEWLINE in phase 2, white-space in
    phase 3 (or higher).
     
    Tim Rentsch, Mar 22, 2005
    #19
  20. foodic

    pete Guest

    Re: Parts of a C program, Re: problem with compilation

    Tim Rentsch wrote:
    >
    > pete <> writes:
    >
    > > Tim Rentsch wrote:
    > > >
    > > > pete <> writes:
    > > >
    > > > > Each byte in a C program is one of 4 things:
    > > > > 1 part of a comment
    > > > > 2 part of a preprocessor directive
    > > > > 3 part of an external declaration
    > > > > 4 all other bytes in a C program are white space
    > > >
    > > > Mostly I agree with this (glossing over the discussions about 'static'
    > > > vs "external" and possible declaration/definition distinctions).
    > > >
    > > > But, wouldn't you agree that \ NEWLINE aren't exactly like
    > > > other white space?


    I'll admit that they get treated differently during translation
    but I don't see why that is relevant.

    > > > (And they certainly don't fall into the
    > > > other three categories.) I feel a little bit uneasy referring
    > > > to \ NEWLINE as "white space".

    > >
    > > N869
    > > 7.4.1.9 The isspace function
    > > Description
    > > [#2] The
    > > standard white-space characters are the following: space
    > > (' '), form feed ('\f'), new-line ('\n'), carriage return
    > > ('\r'), horizontal tab ('\t'), and vertical tab ('\v')

    >
    > It's clear that \ NEWLINE sequences are not intended as white-space
    > in the language of the stanard document.


    Here's the language of the standard document:
    N869
    5.1.1.2 Translation phases
    3.
    Whether each nonempty
    sequence of white-space characters other than new-line
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    is retained or replaced by one space character is
    implementation-defined.

    It sure looks likes the language of the standard considers
    newline, to be one of the white-space characters.

    It almost seems as though you're trying to say
    that a white-space character that gets treated differently
    from the other white-space characters during translation,
    shouldn't be considered a white-space character.

    --
    pete
     
    pete, Mar 23, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ELJ
    Replies:
    2
    Views:
    563
    Paulo Valentim
    Feb 25, 2005
  2. Dan
    Replies:
    3
    Views:
    602
    =?Utf-8?B?RGFu?=
    Jun 14, 2004
  3. JSR
    Replies:
    2
    Views:
    362
  4. bcassand
    Replies:
    4
    Views:
    1,483
    Martin Moessner
    Jan 12, 2004
  5. C__chp
    Replies:
    4
    Views:
    510
    Puppet_Sock
    Feb 15, 2008
Loading...

Share This Page