Author Topic: Precompiled Header Hell  (Read 19971 times)

sviten

  • Guest
Precompiled Header Hell
« on: December 01, 2005, 10:24:39 pm »
Hi guys,

I'm new to C::B.  I was trying to import my code over (from Dev-cpp but I just imported the source files).  I seem to have a problem where g++ refuses to use my header files unless they are precompiled and if I allow it to precompile them, then I get a "protocol.h.gch: file not recognized: File format not recognized" error after it compiles it.

Is there a way to turn off using precompiled headers?  I can't find it either in the Project, Compiler settings or this forum.

I'm not very interested in using PCH since I'm still changing my headers a lot.

Thanks much for your time.

Offline Urxae

  • Regular
  • ***
  • Posts: 376
Re: Precompiled Header Hell
« Reply #1 on: December 01, 2005, 10:44:36 pm »
How exactly does it 'refuse' to use your header files? Does it seem to be using an old version? Does it give an error message?

One thing that might cause this: you have to remember to delete previously generated .gch files when switching the corresponding precompiled headers off, or GCC will keep using the .gch it finds.
Oh, and did you happen to upgrade GCC recently? "File format not recognized" sounds like the .gch was not generated by the current version of GCC (a .gch is basically a dump of the internal memory state of GCC, which can differ with each version).

sviten

  • Guest
Re: Precompiled Header Hell
« Reply #2 on: December 02, 2005, 12:24:48 am »
How exactly does it 'refuse' to use your header files? Does it seem to be using an old version? Does it give an error message?

Sorry, guess I forgot to include that
If I go into the Properties setting for each header file in my project and uncheck "Compile" (this is the only way I would guess to tell g++ not to use precompiled headers), then g++ returns the error, "mingw32-g++.exe: protocol.h.gch: No such file or directory". How would I know if I was using an old version?  I just downloaded it today.


One thing that might cause this: you have to remember to delete previously generated .gch files when switching the corresponding precompiled headers off, or GCC will keep using the .gch it finds.
Oh, and did you happen to upgrade GCC recently? "File format not recognized" sounds like the .gch was not generated by the current version of GCC (a .gch is basically a dump of the internal memory state of GCC, which can differ with each version).

Check on the deleting old .gch files.  Negative on the upgrade.

Offline Urxae

  • Regular
  • ***
  • Posts: 376
Re: Precompiled Header Hell
« Reply #3 on: December 02, 2005, 12:50:00 am »
How exactly does it 'refuse' to use your header files? Does it seem to be using an old version? Does it give an error message?

Sorry, guess I forgot to include that
If I go into the Properties setting for each header file in my project and uncheck "Compile" (this is the only way I would guess to tell g++ not to use precompiled headers), then g++ returns the error, "mingw32-g++.exe: protocol.h.gch: No such file or directory". How would I know if I was using an old version?  I just downloaded it today.

By the 'old version' question I meant if GCC seemed to be using an old version of the header (which might happen if you forgot to delete the .gch).
It's specifically looking for protocol.h.gch? Weird. You wouldn't happen to have #included that .gch directly would you? I'm not even sure that would work, but that would explain the error...
If not, go to Settings -> Compiler -> Other, and set "Compiler logging" to "Full command line". Then try to compile. See if protocol.h.gch is mentioned in the command line ("-include protocol.h.gch" perhaps?).
If after that you still can't figure it out, please post the full build log here.

sviten

  • Guest
Re: Precompiled Header Hell
« Reply #4 on: December 02, 2005, 01:14:04 am »
No, I'm definitely not putting #include"protocol.gch" in my .cpp files.  Here's my compile log (after running a dist-clean):

Code
Project   : Console application
Compiler  : GNU GCC Compiler (called directly)
Directory : D:\Projects\GeccoTester\
--------------------------------------------------------------------------------
Switching to target: default
mingw32-g++.exe   -g    -ID:\Projects\GeccoTester\ -I..\ptypes-2.0.2\include  -I"d:\Program Files\CodeBlocks\include" -ID:\Projects\ptypes-2.0.2\include -c protocol.cpp -o .objs\protocol.o
mingw32-gcc.exe   -g    -ID:\Projects\GeccoTester\ -I..\ptypes-2.0.2\include  -I"d:\Program Files\CodeBlocks\include" -ID:\Projects\ptypes-2.0.2\include -c sha256.c -o .objs\sha256.o
mingw32-g++.exe   -g    -ID:\Projects\GeccoTester\ -I..\ptypes-2.0.2\include  -I"d:\Program Files\CodeBlocks\include" -ID:\Projects\ptypes-2.0.2\include -c main.cpp -o .objs\main.o
mingw32-g++.exe   -LD:\Projects\GeccoTester\ -L..\ptypes-2.0.2\lib  -L"d:\Program Files\CodeBlocks\lib" -LD:\Projects\ptypes-2.0.2\lib -o D:\Projects\GeccoTester\GeccoTester.exe .objs\protocol.o protocol.h.gch .objs\sha256.o sha256.h.gch .objs\main.o        ..\ptypes-2.0.2\lib\libptypes.a
mingw32-g++.exe: protocol.h.gch: No such file or directory
mingw32-g++.exe: sha256.h.gch: No such file or directory
Process terminated with status 1 (0 minutes, 11 seconds)
0 errors, 0 warnings
 

It seems to be be looking for the *.h.gch files specifically.  I think this is coming from here:

Offline Urxae

  • Regular
  • ***
  • Posts: 376
Re: Precompiled Header Hell
« Reply #5 on: December 02, 2005, 01:24:06 am »
Code
[...]
mingw32-g++.exe [...] protocol.h.gch .objs\sha256.o sha256.h.gch [...]
mingw32-g++.exe: protocol.h.gch: No such file or directory
mingw32-g++.exe: sha256.h.gch: No such file or directory
Process terminated with status 1 (0 minutes, 11 seconds)
0 errors, 0 warnings
You're passing precompiled headers to the linker :shock:? Go to the dialog you posted the screenshot from, first tab, is "Link file" checked? That should never be checked for headers. (check both protocol.h and sha256.h)
If it isn't (though it probably is, due to the order on the command line), check in all locations where you can specify linker options (Project -> Build options, for every item in the tree to the left, linker tab and Settings -> Compiler, also linker tab) and see if you specified protocol.h.gch and sha256.h.gch anywhere. Remove them.

Offline rickg22

  • Lives here!
  • ****
  • Posts: 2283
Re: Precompiled Header Hell
« Reply #6 on: December 02, 2005, 01:25:26 am »
Hint: Disable BOTH the "link" and the "compile" flag to your header files. That way you WON'T have to use precompiled headers *AT ALL*.

sviten

  • Guest
Re: Precompiled Header Hell
« Reply #7 on: December 02, 2005, 02:00:00 am »
OK, that reduced it to some errors that I'm more familiar with...

Code
Project   : Console application
Compiler  : GNU GCC Compiler (called directly)
Directory : D:\Projects\GeccoTester\
--------------------------------------------------------------------------------
Switching to target: default
mingw32-g++.exe   -LD:\Projects\GeccoTester\ -L..\ptypes-2.0.2\lib  -L"d:\Program Files\CodeBlocks\lib" -LD:\Projects\ptypes-2.0.2\lib -o D:\Projects\GeccoTester\GeccoTester.exe .objs\protocol.o .objs\sha256.o .objs\main.o        ..\ptypes-2.0.2\lib\libptypes.a
.objs\protocol.o: In function `ZN21AccountRequestMessage6assignEPKcj':
D:/Projects/GeccoTester/protocol.cpp:58: undefined reference to `sha256_starts(sha256_context*)'
D:/Projects/GeccoTester/protocol.cpp:59: undefined reference to `sha256_update(sha256_context*, unsigned char*, unsigned long)'
D:/Projects/GeccoTester/protocol.cpp:60: undefined reference to `sha256_finish(sha256_context*, unsigned char*)'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 2 seconds)
3 errors, 0 warnings

However, I don't understand why I'm getting linker errors here since I've properly included " #include"protocol.h"  as well as put the current project directory in the linker path.  Any ideas?

Offline Urxae

  • Regular
  • ***
  • Posts: 376
Re: Precompiled Header Hell
« Reply #8 on: December 02, 2005, 02:33:35 am »
Code
[...]
mingw32-g++.exe   -LD:\Projects\GeccoTester\ -L..\ptypes-2.0.2\lib  -L"d:\Program Files\CodeBlocks\lib" -LD:\Projects\ptypes-2.0.2\lib -o D:\Projects\GeccoTester\GeccoTester.exe .objs\protocol.o .objs\sha256.o .objs\main.o        ..\ptypes-2.0.2\lib\libptypes.a
.objs\protocol.o: In function `ZN21AccountRequestMessage6assignEPKcj':
D:/Projects/GeccoTester/protocol.cpp:58: undefined reference to `sha256_starts(sha256_context*)'
D:/Projects/GeccoTester/protocol.cpp:59: undefined reference to `sha256_update(sha256_context*, unsigned char*, unsigned long)'
D:/Projects/GeccoTester/protocol.cpp:60: undefined reference to `sha256_finish(sha256_context*, unsigned char*)'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 2 seconds)
3 errors, 0 warnings

Those errors basically mean "AccountRequestMessage::assign(char const*, unsigned int) is using three functions that have been declared but aren't defined in any (linked) object file".
Judging by their names, I'd assume they were supposed to be in sha256.cpp. See if they are defined there, and if their signature is exactly the same (incuding constness etc.).
Another possibility that just occurred to me: these look like C functions (sha256_ prefix, char*'s). If they're actually defined in a .c file, that might also explain it: C++ uses a different naming scheme than C by default. The fix would be to either rename the file to have an extension associated with C++ (.cpp is the most popular AFAIK), specify '-x c++' as an additional command-line option for these files, or change the header associated with it to be similar to this:
Code
#ifndef SOME_HEADER_GUARD
#define SOME_HEADER_GUARD

#ifdef __cplusplus
extern "C" {
#endif

/* * * The rest of the original header * * */

#ifdef __cplusplus
} // extern "C"
#endif

#endif // SOME_HEADER_GUARD
(you can also put extern "C" in front of each and every declaration your C++ program uses, but that's probably more work)

sviten

  • Guest
Re: Precompiled Header Hell
« Reply #9 on: December 02, 2005, 02:46:12 am »
Bingo.  It was the .c extension.

Thanks so much for your help.