Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...