如何混合Qt,C ++和Obj-C/Cocoa [英] How to mix Qt, C++ and Obj-C/Cocoa

查看:122
本文介绍了如何混合Qt,C ++和Obj-C/Cocoa的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Mac上有一个纯C ++/Qt项目,但是现在我发现我需要调用一些仅在Cocoa API中可用的方法.此处列出了以下说明:

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-和-qt

我在".m"文件中具有C ++类的实现.作为测试,我的"foo.m"文件包含以下代码(为清楚起见,已删除了相关的#include方法).

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];
}

显然,我需要将.m文件添加到名为OBJECTIVE_SOURCES的qmake变量中.我的项目.pro文件如下所示:

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'

这指向我的头文件中的class foo文件.如果我从.m文件中删除所有可可调用,然后将.m文件移至Qt .pro文件的SOURCES部分,则一切正常.

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.

我正在使用Qt 4.6.0.

I'm using Qt 4.6.0.

我的问题是:将Cocoa调用与Qt/C ++集成的推荐方法是什么,在上面的示例中我做错了什么?

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?

推荐答案

它将.m文件编译为Objective-C.您希望它成为Objective-C ++的.mm文件.

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

这篇关于如何混合Qt,C ++和Obj-C/Cocoa的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆