Using a custom mkspec on macOS, and #ifdef blocks aren't being highlighted correctly in Qt Creator
-
wrote on 28 Feb 2023, 16:42 last edited by
I've made a mkspec for Qt 6.2.3 that makes use of a custom installation of clang 14 and a separate copy of the macOS SDK. The mkspec is intended to be the same as the standard
macx-clang
mkspec with just a few variables changed. It looks basically like this, which paths changed to placeholders:include(/path/to/qt/6.2.3/macos/mkspecs/macx-clang/qmake.conf) QMAKE_CC = /path/to/clang14/usr/bin/clang QMAKE_CXX = /path/to/clang14/usr/bin/clang++ QMAKE_LINK = /path/to/clang14/usr/bin/clang++ DSYMUTIL_PATH = /path/to/clang14/usr/bin/dsymutil QMAKE_MAC_SDK.macosx.Path = /path/to/Xcode14.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk QMAKE_MAC_SDK.macosx.PlatformPath = /path/to/Xcode14.app/Contents/Developer/Platforms/MacOSX.platform QMAKE_MAC_SDK.macosx.SDKVersion = 12
When I compile my projects with Qt 6.2.3 and this mkspec, everything works fine. However, in Qt Creator, it's acting as though certain precompiler definitions are not present, resulting in it acting like
Q_OS_MAC
is not defined and thus highlighting the wrong blocks when I use an#ifdef
statement. This makes my code a lot harder to work with.I dug into this, and found that Qt Creator is acting as though none of the following preprocessor directives are defined, even though they definitely are when compiling:
__GNUC__
,__APPLE__
,TARGET_OS_MAC
,Q_OS_DARWIN
, andQ_OS_MAC
.Anyone know what's going wrong, and how to fix this?
Things I've tried so far:
I tried adding something like
DEFINES += __GNUC__ __APPLE__ ...
to my mkspec, but then I get lots of double definition warnings. Again: these are all defined when compiling, and compilation works just as it should. (If there were a qmake variable I could use that was likeDEFINES
but only applies to Qt Creator and not actual compilation then that would work.)I also tried using Qt Creator's "Additional C++ Preprocessor Directives" feature, but it doesn't work either. Adding
__GNUC__
,__APPLE__
or evenQ_OS_MAC
to it still doesn't result in correct highlighting of code in Qt Creator: adding them has no effect at all. But even if it did work, it's already an unacceptable solution to this problem, because I'd have to add these defines for all of the hundreds of files in my project, and I most certainly do not want to do that. -
wrote on 3 Mar 2023, 15:29 last edited by
Nevermind! I was looking in the wrong place for a solution this whole time. It turns out it was the kit I had set up in Qt Creator. The path for its compiler was incorrect. As soon as I fixed that, the
#ifdef
blocks were highlighted correctly. I had assumed the issue was with my mkspec because qmake doesn't use the compiler set up by Qt Creator. -
wrote on 3 Mar 2023, 14:08 last edited by
So far the only solution I've found is to add the following to the top of qglobal.h:
#ifndef __GNUC__ #define __GNUC__ #endif #ifndef __APPLE__ #define __APPLE__ #endif #ifndef TARGET_OS_MAC #define TARGET_OS_MAC #endif
Obviously modifying Qt's header files is not a great solution. I'm hoping there's a better way.
-
wrote on 3 Mar 2023, 15:29 last edited by
Nevermind! I was looking in the wrong place for a solution this whole time. It turns out it was the kit I had set up in Qt Creator. The path for its compiler was incorrect. As soon as I fixed that, the
#ifdef
blocks were highlighted correctly. I had assumed the issue was with my mkspec because qmake doesn't use the compiler set up by Qt Creator. -
1/3