Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: nji on July 03, 2020, 10:06:25 am

Title: Disabling PCH for a single file
Post by: nji 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.
Title: Re: Disabling PCH for a single file
Post by: nji 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

Title: Re: Disabling PCH for a single file
Post by: oBFusCATed 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.
Title: Re: Disabling PCH for a single file
Post by: nji 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.
Title: Re: Disabling PCH for a single file
Post by: stahta01 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.

Title: Re: Disabling PCH for a single file
Post by: sodev 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 :).
Title: Re: Disabling PCH for a single file
Post by: oBFusCATed 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.
Title: Re: Disabling PCH for a single file
Post by: sodev 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++ :).
Title: Re: Disabling PCH for a single file
Post by: stahta01 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.