problem with compilation

F

foodic

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
 
H

Helmut Weber

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
 
J

Joe Wright

foodic said:
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).
 
P

pete

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
 
M

Martin Ambuhl

foodic said:
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);
 
C

CBFalconer

pete said:
.... 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 */
 
J

Joe Wright

pete said:
Joe Wright wrote:




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.
 
C

CBFalconer

Joe said:
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(...)
{
 
T

Tim Rentsch

pete said:
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".
 
P

pete

Tim said:
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')
 
P

pete

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


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.


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.
 
P

pete

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.
 
P

pete

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

"External" means something else in C.
 
C

CBFalconer

pete said:
.... 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.
 
S

Stephen Sprunk

foodic said:
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
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
 
L

Lawrence Kirby

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
 
J

Joe Wright

pete said:
Joe said:
pete said:
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.
 
T

Tim Rentsch

^^^^^^^^^^^^^^^^^ [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".
 
T

Tim Rentsch

pete said:
Tim said:
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).
 
P

pete

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top