Author Topic: Disabling PCH for a single file  (Read 4044 times)

Offline nji

  • Multiple posting newcomer
  • *
  • Posts: 54
Disabling PCH for a single file
« on: July 03, 2020, 10:06:25 am »
Hello.
If the whole project is using pch, but a single file doesn't...
Is there a way to disable it for this file only?
Or do I have to remove the project-wide pch usage
and insert it for all but the one file?
Thanks in advance.

Offline nji

  • Multiple posting newcomer
  • *
  • Posts: 54
Re: Disabling PCH for a single file
« Reply #1 on: July 03, 2020, 09:28:28 pm »
That problem arises when inserting sqlite amalgamation (C) into
an wxWidget project (C++).
The latter has project wide PCH usage and this doesn't work for
the sqlite code.
I meanwhile tried to disable PCH usage in the advanced properties
of sqlite.c, adding  -DNOPCH to
Code
$compiler $options $includes -c $file -o $object
But no effect, it still uses pch.
The command line is like this
Code
gcc.exe  -DNOPCH -Wall -pipe -mthreads -Winvalid-pch -include wx_pch.h -D__GNUWIN32__ -D__WXMSW__ -DWXUSINGDLL -DwxUSE_UNICODE -DWX_PRECOMP -g -ID:\develop\wxWidgets\include -ID:\develop\wxWidgets\lib\gcc_dll\mswu -c D:\develop\sqlite-amalgamation-3310100\sqlite3.c -o obj\Debug\sqlite-amalgamation-3310100\sqlite3.o


Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Disabling PCH for a single file
« Reply #2 on: July 04, 2020, 11:39:12 am »
I don't think so. You have to probably put the sqlite code in a static library and build it separately.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline nji

  • Multiple posting newcomer
  • *
  • Posts: 54
Re: Disabling PCH for a single file
« Reply #3 on: July 05, 2020, 05:39:51 pm »
Thanks.  :)

I meanwhile tried that way.

Originally I thought taking the amalgamation would be the simplest way of using sqlite.
But taking the precompiled dll + headers from amalgamation is even easier.
As long as your compiler matches with the one used for the precompiled dll.
You don't even need the def file.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: Disabling PCH for a single file
« Reply #4 on: July 06, 2020, 02:03:00 am »
Do you understand the idea of CB targets?
If not, I suggest you learn how to have multiple targets in a project.
You can set different compiler and linker options for each target.

I would create an static library target for the sqlite file.

Tim S.

C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline sodev

  • Regular
  • ***
  • Posts: 497
Re: Disabling PCH for a single file
« Reply #5 on: July 06, 2020, 02:46:00 am »
The problem here is not PCH itself but a mix of C and C++. The SQLite file has the .c extension so it will be compiled as C code, the project settings however force include the PCH which was created in C++ mode and this causes the error. So beside the two outlined solutions, extra target that compiles the file with different settings and using a precompiled variant, i propose a third solution: rename the file to .cpp so it will be compiled in C++ mode. If SQLite can be compiled in C++ mode this is the simplest solution :).

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Disabling PCH for a single file
« Reply #6 on: July 06, 2020, 08:02:24 pm »
If SQLite can be compiled in C++ mode this is the simplest solution :).
This is one big "if". C and C++ aren't really compatible in this way. Even if it compiles it might be broken due to different UB rules in the two languages.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline sodev

  • Regular
  • ***
  • Posts: 497
Re: Disabling PCH for a single file
« Reply #7 on: July 06, 2020, 10:11:00 pm »
If the code doesn't use any constructs that are invalid in C++ and contains the usual extern "C" wrapping there should be no problem. And a quick check reveals that at least the amalgamation source does contain this wrapping, so im pretty confident that works without problem.

In my experience C libraries work without issues in C++ mode (especially if they already contain the wrapping), however i am not dealing with embedded c libraries which might contain hardcore c hacks that break c++ :).

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: Disabling PCH for a single file
« Reply #8 on: July 07, 2020, 07:53:36 pm »
What I think is the proper long term fix for this problem.

Git patch attached

Code
--- a/src/plugins/scriptedwizard/resources/wxwidgets/pch/wx_pch.h
+++ b/src/plugins/scriptedwizard/resources/wxwidgets/pch/wx_pch.h
@@ -10,7 +10,9 @@
 #ifndef WX_PCH_H_INCLUDED
 #define WX_PCH_H_INCLUDED
 
-// basic wxWidgets headers
+#ifdef __cplusplus
+
+// basic wxWidgets C++ headers
 #include <wx/wxprec.h>
 
 #ifdef __BORLANDC__
@@ -25,4 +27,6 @@
     // put here all your rarely-changing header files
 #endif // WX_PRECOMP
 
+#endif // __cplusplus
+
 #endif // WX_PCH_H_INCLUDED
--

Tim S.

C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org