header file question

G

Geiregat Jonas

I've got this small app , from 2 .c files. One file contains the main
and the other file contains functions used in the main. I compile this
using gcc like this: gcc -c *c && gcc -o app *o. But I want to do it in
an other way. I want to use header files I've googled but did not find
anything usefull expect that there should be prototypes in my header
file can anyone help me ?
 
S

Sheldon Simms

I've got this small app , from 2 .c files. One file contains the main
and the other file contains functions used in the main. I compile this
using gcc like this: gcc -c *c && gcc -o app *o. But I want to do it in
an other way. I want to use header files I've googled but did not find
anything usefull expect that there should be prototypes in my header
file can anyone help me ?

It's not clear what you are talking about.
Try this:

gcc -o app *.c
 
A

Aaron Walker

Geiregat said:
I've got this small app , from 2 .c files. One file contains the main
and the other file contains functions used in the main. I compile this
using gcc like this: gcc -c *c && gcc -o app *o. But I want to do it in
an other way. I want to use header files I've googled but did not find
anything usefull expect that there should be prototypes in my header
file can anyone help me ?


I will do a small example.


main.c
------

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

int main()
{
int x;
printf("Enter an integer: ");
scanf("%d", &x);
printf("The square of %d is %ld\n", x, square(x));
return (0);
}

calc.h
------
#ifndef CALC_H
#define CALC_H

long square(int);

#endif

calc.c
------
#include "calc.h"

long square(int x)
{
return ((long) x * x);
}

---------
NOTES
notice the quotes when including calc.h because it is in the same
directory as the source file

also, if you've never used preprocessor directives, the "#ifndef
CALC_H" may look confusing. You want this in the header file, though.
It prevents multiple inclusions of that header file. Just change CALC_H
to whatever.. I dont think it matters as long as its the same as the
#define statement below it.

HTH,
Aaron
 
R

Richard Heathfield

tuchka said:
Look at an article at this link.
It is good simple tutorial on C programming and also there detailed
instructions how to make library at 'libraries' link using gcc
compiler. (I've done that and it worked).

http://computer.howstuffworks.com/c.htm

The first incorrect statement I spotted was at

http://computer.howstuffworks.com/c1.htm

Here it is:

"C is what is called a compiled language. This means that once you write
your C program, you must run it through a C compiler to turn your program
into an executable that the computer can run (execute)."

The second, related error:

"...to write and run a C program, you must have access to a C compiler."

The third error:

"When you enter this program, position #include so that the pound sign is in
column 1 (the far left side). Otherwise, the spacing and indentation can be
any way you like it."

Counter-example:

#include<stdio.h>intmain(){p r i n t f("This is output from my first
program!\ n"); return0;}

When Mr Brain has fixed those errors, I'll find some more for him to do.
 
J

Joona I Palaste

The first incorrect statement I spotted was at

Here it is:
"C is what is called a compiled language. This means that once you write
your C program, you must run it through a C compiler to turn your program
into an executable that the computer can run (execute)."

I don't see how that statement is incorrect.
The second, related error:
"...to write and run a C program, you must have access to a C compiler."

Hmm, I can see two ways to parse that as erroneous.
1) If writing a program merely consists of writing the source code,
you don't need a compiler.
2) You don't need *any* programming tools to run programs.
But does "to write and run a C program" mean "to write a C program
and then to run the same C program" or "to write a C program, and
to run (possibly another) C program"?

(snip third error)
When Mr Brain has fixed those errors, I'll find some more for him to do.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"You can pick your friends, you can pick your nose, but you can't pick your
relatives."
- MAD Magazine
 
R

Richard Heathfield

Aaron said:
This is most excellent ;) I didn't know they had C interpreters now.. heh.

The first time I used a C interpreter was in 1989. (HiSoft C for the Atari
ST.)
 
J

Joona I Palaste

The first time I used a C interpreter was in 1989. (HiSoft C for the Atari
ST.)

Hmm, there are several things that can be done in an interpreter that
can't be done in a compiler. I have experience of writing interpreters
slightly above "trivial" level, but I have never written a full
compiler. One of my interpreters allowed interpreting the contents of
a string variable as if it were program code. I don't think that's quite
possible in compilers?
Are there any things compilers can do that interpreters can't? I'm
fairly sure there are things compilers can do *easier* than
interpreters, but is there any that compilers can do and interpreters
can't do *at all*?
Should I ask at comp.programming?

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The obvious mathematical breakthrough would be development of an easy way to
factor large prime numbers."
- Bill Gates
 
C

Christopher Benson-Manica

Joona I Palaste said:
Are there any things compilers can do that interpreters can't? I'm
fairly sure there are things compilers can do *easier* than
interpreters, but is there any that compilers can do and interpreters
can't do *at all*?

In my limited experience with JavaScript (which is interpreted, yes?),
it seems to be dollars to dougnuts that any reported error messages
won't have a great deal to do with the actual error. Miss a
punctuation mark and you'll get one error in some bizarre place. At
least C will yell at you in a number of places, one of which is in the
ballpark of the actual error.
 
M

Mark McIntyre

In my limited experience with JavaScript (which is interpreted, yes?),
it seems to be dollars to dougnuts that any reported error messages
won't have a great deal to do with the actual error. Miss a
punctuation mark and you'll get one error in some bizarre place.

hmm, sounds familiar - a bit like when in C you miss off a brace in a
header and get an "unexpected end of file" message from some random
place? :)
At
least C will yell at you in a number of places, one of which is in the
ballpark of the actual error.

err.... :)
 
K

karl malbrain

Joona I Palaste said:
Hmm, there are several things that can be done in an interpreter that
can't be done in a compiler. I have experience of writing interpreters
slightly above "trivial" level, but I have never written a full
compiler. One of my interpreters allowed interpreting the contents of
a string variable as if it were program code. I don't think that's quite
possible in compilers?

Sure it is, see scheme/lisp for an example. karl m
 
R

Richard Heathfield

Christopher said:
I suppose this accepts a superset of ISO C?

I haven't the faintest idea. I've never actually used it. I only know of its
existence, that's all. (Feel free to investigate and report back!)
 
D

Dan Pop

In said:
The first time I used a C interpreter was in 1989. (HiSoft C for the Atari
ST.)

Are you sure it was not an incremental compiler? HiSoft C for the ZX
spectrum certainly was: you could type source code directly to the
compiler, but it wouldn't be executed until you typed the eof key and
the compiler finished the translation process.

Dan
 
D

Dan Pop

In said:
Hmm, there are several things that can be done in an interpreter that
can't be done in a compiler.

Are there? QBASIC (or was it QB?) was a compiler that created very
nicely the illusion of a BASIC interpreter on MSDOS.
I have experience of writing interpreters
slightly above "trivial" level, but I have never written a full
compiler. One of my interpreters allowed interpreting the contents of
a string variable as if it were program code. I don't think that's quite
possible in compilers?

It certainly is. Include an interpreter in the run-time and give it
access to the program's symbol tables. To a limited extent, this is
what most high level debuggers do when evaluating an expression.
Are there any things compilers can do that interpreters can't?

Make your program run as fast as possible ;-) Genuine interpreters really
suck in terms of program execution speed. That's why code generation on
the fly is a technique more and more popular with interpreters. You
should be familiar with just-in-time Java bytecode "interpreters".
I'm fairly sure there are things compilers can do *easier* than
interpreters, but is there any that compilers can do and interpreters
can't do *at all*?

No.

Dan
 
R

Richard Bos

Christopher Benson-Manica said:
In my limited experience with JavaScript (which is interpreted, yes?),
it seems to be dollars to dougnuts that any reported error messages
won't have a great deal to do with the actual error. Miss a
punctuation mark and you'll get one error in some bizarre place. At
least C will yell at you in a number of places, one of which is in the
ballpark of the actual error.

This can easily be done by an interpreter. Many don't, perhaps, but that
doesn't make a syntax check before running impossible.

Richard
 
C

Christopher Benson-Manica


Well, at least when you leave off a quote in C, the compiler is sure
to be upset - "Line 666: Unterminated string constant", and lo, on
line 666 will be an unterminated string. In Javascript, however, do
the same thing and you're likely to see something like "Line
123456789: Unterminated string constant" which, coincidentally, has
nothing to do with the *real* unterminated string constant on line
666. No, I don't get frustrated easily ;)
 

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
474,102
Messages
2,570,645
Members
47,247
Latest member
GabrieleL2

Latest Threads

Top