making #define's to be seen across files 
Author Message
 making #define's to be seen across files

Hi there,
    There is (hopefully) a simple answer to my question:

I have some C code divided into different source files.
When I try to compile using
  "cc file1.c file2.c file3.c"
the constants #define'd in file1.c are not visible in file2.c, is there a way to make them visible to the other source files?  In other words, is there som equivalent to "extern" for constants?

Any help will be deeply appreciated.

Heffel



Sat, 26 Sep 1998 03:00:00 GMT  
 making #define's to be seen across files


Quote:
>Hi there,
>    There is (hopefully) a simple answer to my question:

>I have some C code divided into different source files.
>When I try to compile using
>  "cc file1.c file2.c file3.c"
>the constants #define'd in file1.c are not visible in file2.c, is there a way to make them visible to the other source files?  In other words, is there som equivalent to "extern" for constants?

Put them in a header file and include the header file in both file1.c and
file2.c

-tim

--
##########################################################################


900 Chelmsford Street                                        (508)551-1125
Lowell, MA 01851



Sat, 26 Sep 1998 03:00:00 GMT  
 making #define's to be seen across files


 >Hi there,
 >    There is (hopefully) a simple answer to my question:
 >
 >I have some C code divided into different source files.
 >When I try to compile using
 >  "cc file1.c file2.c file3.c"
 >the constants #define'd in file1.c are not visible in file2.c, is there a way to make them visible to the other source files?  In other words, is there som equivalent to "extern" for constants?
 >
 >Any help will be deeply appreciated.

Ha! There is one question we haven't seen around here!

You have to move these #defines to a separate file, usually named with a .h
suffix. Then you #include this file in all the others.

--



Sat, 26 Sep 1998 03:00:00 GMT  
 making #define's to be seen across files

Quote:

>Hi there,
>    There is (hopefully) a simple answer to my question:

>I have some C code divided into different source files.
>When I try to compile using
>  "cc file1.c file2.c file3.c"
>the constants #define'd in file1.c are not visible in file2.c, is there a
>way to make them visible to the other source files?  In other words, is
>there som equivalent to "extern" for constants?

Put anything you want  known to multiple files in a .h file that you #include
in each .c file  (actually, you don't _have_ to call the #included file
something.h, but it is traditional...)

Peter Bennett VE7CEI                | Vessels shall be deemed to be in sight


TRIUMF, Vancouver, B.C., Canada     |                          ColRegs 3(k)
GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
or: ftp://ftp-i2.informatik.rwth-aachen.de/pub/arnd/GPS/peter/index.html



Sat, 26 Sep 1998 03:00:00 GMT  
 making #define's to be seen across files

Quote:
>Hi there,
>    There is (hopefully) a simple answer to my question:
>I have some C code divided into different source files.
>When I try to compile using
>  "cc file1.c file2.c file3.c"
>the constants #define'd in file1.c are not visible in file2.c, is there a way to make them visible to the other source files?  In other words, is there som equivalent to "extern" for constants?

Put those defines in a header file, and include the header file in
"file1.c", "file2.c" and "file3.c"? No, too simple :-). You could try
to use _real_ constants:

Declare your "constants" in _one_ translation unit as

  const double PI = 3.141506;

Use

  extern const double PI;

in all other translation units. This has some advantages when working
with a good de{*filter*} that _can_ inspect a const, but not a define.

The #define-directive is a preprocessor statement, and the proeprocessor
performs textual exchange of a "#defined" macro with it's definition.
The preprocessor does not create any constants that have any linkage
if you write

   #define PI 3.141506

After preprocessing, the compiler does not distinguish between a
line that contains "PI" and the same line that contains "3.141506",
because both lines now contain "3.141506". "3.141506" is a floating
point literal, and all you can do with it is use it in an expression.

Kurt
--
| Kurt Watzka                             Phone : +49-89-2180-6254



Sat, 26 Sep 1998 03:00:00 GMT  
 making #define's to be seen across files

Quote:
>Hi there,
>    There is (hopefully) a simple answer to my question:

>I have some C code divided into different source files.
>When I try to compile using
>  "cc file1.c file2.c file3.c"
>the constants #define'd in file1.c are not visible in file2.c, is there a way to make them visible to the other source files?  In other words, is there som equivalent to "extern" for constants?

>Any help will be deeply appreciated.

>Heffel

You could put them in a .h file which you then #include in each source file.


Sat, 26 Sep 1998 03:00:00 GMT  
 making #define's to be seen across files
|> I have some C code divided into different source files.
|> When I try to compile using
|>   "cc file1.c file2.c file3.c"
|> the constants #define'd in file1.c are not visible in file2.c, is there a
|> way to make them visible to the other source files?  In other words, is
|> there som equivalent to "extern" for constants?

Make a common header file and #include this in all of your other files.
That way they will be visible to all your source files. Sorta like when
you #include <stdio.h> everywhere.
--
"If it wasn't for C, we would be using BASI, PASAL, and OBOL."




Tue, 06 Oct 1998 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Why am I seeing such a delay between my catch block and my finally block

2. making a 'default' file

3. making across directories

4. Help: making Serialisation work across classes in subprojects!

5. How to span #define across multiple source lines?

6. Using a define that hasn't been #defined

7. Protype form I hav'nt seen before

8. Who's seen the standard?

9. why my Control methods can't be seen by outside

10. Seeing pre=processor's work

11. The most frustrating windows bug i've seen

 

 
Powered by phpBB® Forum Software