Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > problem with compilation

Reply
Thread Tools

problem with compilation

 
 
foodic
Guest
Posts: n/a
 
      03-20-2005
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
 
Reply With Quote
 
 
 
 
Helmut Weber
Guest
Posts: n/a
 
      03-20-2005
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" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) om...
> 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



 
Reply With Quote
 
 
 
 
Joe Wright
Guest
Posts: n/a
 
      03-20-2005
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 (E-Mail Removed)
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      03-20-2005
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
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      03-20-2005
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);
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-20-2005
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


 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      03-20-2005
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 (E-Mail Removed)
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-20-2005
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

 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      03-21-2005
pete <(E-Mail Removed)> 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".
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      03-21-2005
Tim Rentsch wrote:
>
> pete <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compilation error with seperate compilation C__chp C++ 4 02-15-2008 03:57 PM
Compilation Problem with Quartus II V4.0 (a new joke ?) ELJ VHDL 2 02-25-2005 11:43 AM
Problem with runtime compilation of aspx files. Register Directive ignores Assembly Dan ASP .Net 3 06-14-2004 06:13 AM
Problem with embedded Tomcat and JSP compilation. bcassand Java 4 01-12-2004 12:38 PM
Java compilation problem - Caused by package access JSR Java 2 12-11-2003 03:16 AM



Advertisments