User account creation filtered due to spam.

Bug 52566 - #include with #pragma once and files' contents is the same
Summary: #include with #pragma once and files' contents is the same
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: preprocessor (show other bugs)
Version: 4.4.3
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-03-12 16:25 UTC by JR Shih
Modified: 2012-03-15 09:06 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description JR Shih 2012-03-12 16:25:08 UTC
# gcc --version
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3

3 source files: ns1.h ns2.h test.cpp

---------------------------------------------------------
ns1.h:
#pragma once
class A
{
};
---------------------------------------------------------
ns2.h:
#pragma once
class A
{
};
---------------------------------------------------------
test.cpp:
namespace NS1
{
#include "ns1.h"
}
namespace NS2
{
#include "ns2.h"
}
int main()
{
    NS2::A a;
    return 0;
}
---------------------------------------------------------

# touch *   // important step, let ns1.h and ns2.h have same modification time
# gcc test.cpp
test.cpp: In function 'int main()':
test.cpp:11: error: 'A' is not a member of 'NS2'
test.cpp:11: error: expected ';' before 'a'

# gcc -c -E test.cpp
# 1 "test.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "test.cpp"
namespace NS1
{
# 1 "ns1.h" 1

class A
{
};
# 4 "test.cpp" 2
}
namespace NS2
{

}
int main()
{
    NS2::A a;
    return 0;
}

----------------------------------------------------
The NS2::A declaration isn't included in namespace NS2
Comment 1 Jonathan Wakely 2012-03-12 16:30:32 UTC
this has nothing to do with namespace scope, it's #pragma once confusing two separate files as one
Comment 2 Jakub Jelinek 2012-03-12 17:09:37 UTC
You either should have different content of the two headers, or don't use #pragma once, or you need to have different timestamps.  In order to support symlinks/hardlinks and also lame filesystems that lack them, the preprocessor only looks at file sizes/timestamps and content if there are multiple #pragma once (or #import) headers.
Comment 3 Jonathan Wakely 2012-03-14 10:46:54 UTC
If you want to use #pragma once the simplest solution might be to put a unique comment in each file, with e.g. just the filename, or a description of its purpose