如何混合 Qt、C++和 Obj-C/Cocoa
我在 Mac 上有一个纯 C++/Qt 项目,但我现在发现我需要调用一些仅在 Cocoa API 中可用的方法。请遵循此处列出的说明:
http://el-tramo.be/blog/mixing- cocoa-and-qt
我在“.m”文件中有一个 C++ 类实现。作为测试,我的“foo.m”文件包含以下代码(为清楚起见,已删除相关的 #include
方法):
int foo::getMagicNumber()
{
NSCursor *cursor = [NSCursor new];
}
显然,我需要将 .m 文件添加到名为的 qmake 变量中OBJECTIVE_SOURCES
。我的项目 .pro 文件如下所示:
TARGET = testApp
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
OBJECTIVE_SOURCES += foo.m
HEADERS += test.h
但是,每当我尝试编译我的项目时,都会收到以下错误:
foo.h:4expected '=', ',', ';', 'asm' 或 '__attribute__ ' 在 'foo' 之前
这指向我的头文件中的 class foo
文件。如果我从 .m 文件中删除所有 cocoa 调用,并将 .m 文件移动到 Qt .pro 文件的 SOURCES
部分,一切都会按预期工作。
我正在使用 Qt 4.6.0。
我的问题是:将 Cocoa 调用与 Qt / C++ 集成的推荐方法是什么?在上面的示例中我做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它将您的
.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++.