Jump to content

Error: Extra Tokens at end of #ifndef Directive

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
18 replies to this topic

#1
fantanoice

fantanoice

    Newbie

  • Members
  • PipPip
  • 26 posts
EDIT: Nevermind, I'm pretty sure I solved it by removing the .h at the end.

-----

(This is C Programming, by the way).

=.= So I have this major assignment to do and naturally, I get errors on the first line.

Okay, so I'm basically using my own header files for the first time. I typed them all out, try and compile and I get a weird error extra tokens at end of #ifndef directive error.

Here is the code that caused the problem:

/* FILE: square.h */


#ifndef square.h

#define square.h


/* Actual code goes here. /*


#endif


I'm getting the error at the #ifndef square.h line. Any clues as to what is causing the problem?

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
The best way to write header guards is with all caps and using underscore characters, like so:

#ifndef SQUARE_H_INCLUDED
#define SQUARE_H_INCLUDED

/* Actual code */

#endif

Wow I changed my sig!

#3
fantanoice

fantanoice

    Newbie

  • Members
  • PipPip
  • 26 posts
Oh, okay, nice. Thank you. :)

Posted Image
Posted Image
Join Dashing today! http://www.dashing.co.nr


#4
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
The modern (and equivalent way) is to use #pragma once. Its just a bit faster on certain compilers because it doesn't need to do a full pass looking for the #endif. Newer versions of GCC and I believe VCC have specific optimizations to look for either, so it doesn't matter that much. Just a timbit.

Edit: Zeke hit me ;(

Anyways, he had a valid point. #pragma once is not a standard, its just supported by convention. As an alternative, although pointless unless you're supporting the stone age, is:

#pragma once
#ifndef lolum_H
#define lolum_H
...
...
#endif

This gives you the speedup and backwards compatibility. But again, unless you're planning on supporting something old, its redundant.

#5
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

TkTech said:

Its just a bit faster on certain compilers because it doesn't need to do a full pass looking for the #endif.
There is a standard way to perform the avoidance as well, I'm just not particularly fond of that code pattern.

TkTech said:

This gives you the speedup and backwards compatibility. But again, unless you're planning on supporting something old, its redundant.
Frankly I'll take "guaranteed to compile anytime, anywhere" over "this might possibly buy you two precious seconds if it happens to work on your system" pretty much every time.

Now if you're compiling several thousand source and respective included files, as is the case in quite a number of libraries, these few seconds do add up, so I'll grant that your preferred approach does have its place.

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
I really think there needs to be some sort of standard set for common stuff like this... #pragma once would be a good thing to have on every compiler - and it's not like it's that hard to implement, either.
sudo rm -rf /

#7
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
It does exist on every modern compiler. ICC, GCC, VCC, ect...

@dcs that example WILL work anywhere. Compilers are supposed to ignore #pragma's it doesn't recognize and will continue on to the #ifndef...#endif guard.

#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Weird...it failed epically for me once on a GCC compiler, got a bajillion duplicate declaration errors. I don't know what version it was, because it was at school.
sudo rm -rf /

#9
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
You might have had gcc treating warnings as errors. The oldest versions of GCC will raise a warning on unknown pragmas. The middle-aged will work but raise an 'obsolete' or similar message (part of the epic GCC vs. #pragma once wars) and the newest ones work perfectly (since 3.2 I believe)

#10
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
That must've been it. Our CS profs always have us use the -Werror flag, so that would've tripped it.
sudo rm -rf /

#11
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

TkTech said:

It does exist on every modern compiler. ICC, GCC, VCC, ect...

@dcs that example WILL work anywhere. Compilers are supposed to ignore #pragma's it doesn't recognize and will continue on to the #ifndef...#endif guard.

Are you assuming that all compilers target PC platforms? I use a lot of compilers that you've probably never heard of. Some useful code I've ported has come from PC-land where folks seem to assume much and the code is unbelieveably nonportable. It kinda sucks. And that's an aftertaste I have from PC folks -- they may be writing "good" code, but all the hoops you have to jump through when porting it gives it that "sucky" feel.

And while #pragma are supposed to be ignored, that isn't always the case. In fact I have run into code that errors out on unrecognized pragmas. So no, it won't work everywhere. It will break in some places. I think it's kinda useful information for people to know.

#12
TkTech

TkTech

    The Crazy One

  • Moderators
  • 1,396 posts
okay...so other than garbage small-c compilers, mind listing the ones it doesn't work on?