volatile struct in dot h vs dot c

  • Thread starter Christopher M. Lusardi
  • Start date
C

Christopher M. Lusardi

Hello,

Can you tell me is it my imagination, or should I not stick a "volatile"
definition into a dot h file?

My problem is if I put my declaration in main.c before main () my code
works. But, if I move it to a dot h and include the dot h into main.c, I
get an error when I run the program.

I am using the volatile qualifier because I am using mmap.

Any suggestions,
Christopher Lusardi
 
L

Leor Zolman

Hello,

Can you tell me is it my imagination, or should I not stick a "volatile"
definition into a dot h file?

My problem is if I put my declaration in main.c before main () my code
works. But, if I move it to a dot h and include the dot h into main.c, I
get an error when I run the program.

Your problem, whatever it is, is most likely due to the contents of the .h
file being included in more than one translation unit. When the declaration
is only processed once, in main.c, the name is only in scope there; if you
include the .h file in other modules, you're introducing the name into
those scopes as well, and something, somewhere, is changing in meaning.

Or, you've got an unrelated bug and just re-arranging your code is enough
to change the program's (mis-)behavior. Post the shortest complete program
you can create that exhibits the problem, and we'll have something more to
go on.
 
C

Case

Christopher said:
Can you tell me is it my imagination, or should I not stick a "volatile"
definition into a dot h file?

My problem is if I put my declaration in main.c before main () my code
works. But, if I move it to a dot h and include the dot h into main.c, I
get an error when I run the program.

Moving code to an include file does not affect the
meaning of this code in any way. All that happens
is that the contents of the include file is literally
pasted at the spot where you write #include ... So,
simply a textual thing.

There is no other way than that you have changed your
code in some way in the process of creating the header
file, or that you include the moved code at a different
spot wrt the original code.

If your code is not too long you could investigate the
preprocessor (with #includes and #defines expanded) output
of both versions: before and after moving to .h file.
Many compilers have an -E switch to get this output.

Good luck,

Case
 
P

Peter Shaggy Haywood

Groovy hepcat Christopher M. Lusardi was jivin' on 11 May 2004
07:33:32 -0700 in comp.lang.c.
volatile struct in dot h vs dot c's a cool scene! Dig it!
Can you tell me is it my imagination, or should I not stick a "volatile"
definition into a dot h file?

No. You should never put variable definitions in headers. Headers
are for variable and function declarations, macro definitions and type
definitions, not variable or function definitions.
My problem is if I put my declaration in main.c before main () my code
works. But, if I move it to a dot h and include the dot h into main.c, I
get an error when I run the program.

I am using the volatile qualifier because I am using mmap.

Any suggestions,

Yes. Simply put the declaration in a header, but the definition in
one and only one .c file.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

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,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top