Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Using QCocoaNativeContext



  • I'm wanting to get the native OpenGL context on macOS but when including
    <QtPlatformHeaders/QCocoaNativeContext> I get the following error:

    In file included from
    .../qt-5.12.7/qtbase/include/QtPlatformHeaders/QCocoaNativeContext:1:
    .../qt-5.12.7/qtbase/include/QtPlatformHeaders/qcocoanativecontext.h:1:
    In file included from
    .../qt-5.12.7/qtbase/include/QtPlatformHeaders/../../src/platformheaders/nativecontexts/qcocoanativecontext.h:44:
    In file included from
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/AppKit.framework/Headers/NSOpenGL.h:8:
    In file included from
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/AppKit.framework/Headers/AppKitDefines.h:11:
    /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:492:1:
    error: expected unqualified-id
    @class NSString, Protocol;

    Looks like it's including Objective-C stuff that the compiler doesn't like.

    This is in a Xcode (11.3) project that was created by CMake.

    How do I make this work?


  • Lifetime Qt Champion

    Hi,

    You are going to use Objective-C code, therefore you need to change your file extension to .mm so that the compiler treats it as Objective-C++ code. Then you'll be able to compile it.



  • @SGaist said in Using QCocoaNativeContext:

    Hi,

    You are going to use Objective-C code, therefore you need to change your file extension to .mm so that the compiler treats it as Objective-C++ code. Then you'll be able to compile it.

    I created a myfile.m with #include <QtPlatformHeaders/QCocoaNativeContext> and added that to my project.

    Now I get a compilation error: Qt requires C++11 support


  • Lifetime Qt Champion

    Not .m but .mm

    There's a difference between the two. One is for Objective-C and the other for Objective-C++. You want the second one.



  • @SGaist Thanks. Changed to .mm and it compiles now.

    I didn't find this information in any of the Qt documentation. Do you know if it is in there somewhere? If not I'll submit a bug report.


  • Lifetime Qt Champion

    Well, when you want to use native API's like that, you are expected to have that kind of knowledge.


  • Lifetime Qt Champion

    @glennr said in Using QCocoaNativeContext:

    I didn't find this information in any of the Qt documentation.

    There is also no information that a file ending with 'c' is treated as c and 'cpp' as c++ - it's basic knowledge when you want to use a language.



  • @Christian-Ehrlicher said in Using QCocoaNativeContext:

    @glennr said in Using QCocoaNativeContext:

    I didn't find this information in any of the Qt documentation.

    There is also no information that a file ending with 'c' is treated as c and 'cpp' as c++ - it's basic knowledge when you want to use a language.

    I wasn't meaning the .m/.mm part. I was referring to the fact that the QtPlatformHeaders/QCocoaNativeContext header can only be included in a .mm file.

    Qt is a cross platform C++ framework and presumably its users (who are C++ developers and not necessarily platform experts) expect some sort of insulation from platform issues. In this case it is unavoidable so it would be a courtesy to those non-mac developers to mention in the documentation for QCocoaNativeContext that it is not C++ and can only be included in an Objective-C++ file.

    My specific use case for this is that I need a pointer to the OpenGLContext to pass to a third party library. I don't need to access any of the Objective-C++ APIs in my code.


Log in to reply