How to mix Qt, C++ and Obj-C/Cocoa

﹥>﹥吖頭↗ 提交于 2019-12-18 03:19:12

问题


I have a pure C++/Qt project on a Mac, but I now find that I need to call a few methods only available in the Cocoa API. Following instructions listed here:

http://el-tramo.be/blog/mixing-cocoa-and-qt

I have a C++ class implementation in a ".m" file. As a test, my "foo.m" file contains the following code (relevant #include methods have been stripped for clarity).:

int foo::getMagicNumber()
{
    NSCursor *cursor = [NSCursor new];
}

Apparently, I need to add the .m file to a qmake variable called OBJECTIVE_SOURCES. My project .pro file looks like this:

TARGET = testApp
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
OBJECTIVE_SOURCES += foo.m
HEADERS += test.h

However, I get the following error whenever I try and compile my project:

foo.h:4expected '=', ',', ';', 'asm' or '__attribute__' before 'foo'

This is pointing at the class foo file in my header file. If I remove all cocoa calls from the .m file, and move the .m file into the SOURCES section of my Qt .pro file everything works as expected.

I'm using Qt 4.6.0.

My question is: What is the recommended way of integrating Cocoa calls with Qt / C++, and what am i doing wrong in the example above?


回答1:


It's compiling your .m file as Objective-C. You want it to be a .mm file for Objective-C++.



来源:https://stackoverflow.com/questions/2355056/how-to-mix-qt-c-and-obj-c-cocoa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!