Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 835
Thomi Avatar asked Mar 01 '10 10:03

Thomi


1 Answers

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

like image 171
richb Avatar answered Sep 25 '22 18:09

richb