Compiler stops working when including a file using backslashes
#1

I have always included files using FORWARD slashes, for example:

pawn Код:
#include "files/scripts/test.inc"
But ****** recommended that I use BACKslashes instead, so I have started to. But there's one file that will include fine with forward slashes, but if I try and use BACKslashes, the compiler crashes..

This file is pretty much empty. It only has this in it:

pawn Код:
#define ADMIN_LEVEL_MOD 1
#define ADMIN_LEVEL_ADMIN 2
#define ADMIN_LEVEL_MANAGER 3
#define ADMIN_LEVEL_OWNER 4

new const AdminLevel[][] =
{
    "Player",
    "Moderator",
    "Admin",
    "Manager",
    "Owner"
};

new stock const AdminLevelPrefix[][] =
{
    "a player",
    "a moderator",
    "an admin",
    "a manager",
    "an owner"
};
This is the literal #include line:

pawn Код:
#include "files\scripts\staff\general.def"
To clarify exactly where this file is, my main .pwn file for my gamemode is in a folder called 'MNS' in the gamemodes directory, and inside that directory is another directory called 'files' which has all the other files in, which are included into the main .pwn file.

Using quotation marks in #include includes files that are relative to the file, so it's nothing to do with the pawno\include\ folder or using "..\" at the start.

Why is this happening? :/

Here's the directory itself, and the path:


All other files (at least 20) include perfectly fine with backslashes..
Reply
#2

I suspect this is something to do with the length:

pawn Код:
_inc_files/scripts/staff/genera
As the include-guard would be called when using forward slashes (not a valid name, but that's the problem with forward slashes) is 31 characters. When using "\" the separators are correctly identified and you get this:

pawn Код:
_inc_general
Thus, if you have another file called "general" in another folder, this one won't get included because the compiler thinks it already has been. This sounds like the best candidate for the problem (ignore what I started saying about length) - it MAY crash if you include code here that is needed, but it doesn't get included because of the mix-up with include guards.

I don't always recommend using "\" for exactly this reason - which to use varies by use-case. In your case you may want to instead use "/" since it worked anyway (just be aware of long directory chains).

Zeex's compiler doesn't automatically generate include guards so this won't be a problem there, but that can cause other issues if your files don't contain explicit include guards as required.
Reply
#3

So if I use forward slashes, the include guard won't work properly because of the length? But I have 3 files included from the 'staff' folder and they all include correctly, but shouldn't the guard for them all be "_inc_files/scripts/staff/genera"? I'm confused.

Three files in 'staff':
infractions.pwn
general.def
functions.inc

If I include all these with forwardslashes, surely the include guard for all THREE of them would be the same (truncated to "_inc_files/scripts/staff/genera"), making the latter two not be included? But this isn't the case..
Reply
#4

The include guards for all three would be truncated, but you would end up with:

"_inc_files/scripts/staff/infrac"
"_inc_files/scripts/staff/genera"
"_inc_files/scripts/staff/functi"

Hence they would all still be included.
Reply
#5

So would the best solution be to use backslashes and name the files uniquely? e.g. 'staff_general.def'?
Reply
#6

I'd just stick to forward slashes for now - the only problem comes when you include directories get too long, and even that can be fixed by mixing:

pawn Код:
#include "files\scripts\staff/general.def"
Would give an include guard name of "_inc_staff/general".

That may be enough to ensure that all your files are included and use backslashes more frequently.
Reply
#7

Okay, thanks. Why do you recommend backslashes so often if it causes this problem?
Reply
#8

Because you can't do:

pawn Код:
#undef _inc_staff/general
"/" is not a valid symbol character so you can't detect or remove that guard. They both have problems, I just tended to favor "\" because it was used by YSI for the reason above, and because "\" is the native directory separator on Windows - the system most people use for development.
Reply
#9

Heh, I just found this in YSI:

pawn Код:
// Yes - FORWARD slash here!
#include "y_als/impl"
I see now, thanks.
Reply
#10

Yes, almost all the "impl.inc" files (there is now one for almost every library in the framework) are included that way for the reason above of getting unique include-guard names. The files that start with an underscore are all included using "\", and are all included multiple times so use the well-definedness of their guard names to #undef them so that they can be included multiple times.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)