在Windows上混合Qt和Objective-C [英] Mixing Qt and Objective-C on Windows

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

问题描述

我通常在OSX和iOS上开发应用程序。我发现Objective-C对许多任务非常有用,在许多情况下,可以加速工作。

I usually develop application on OSX and iOS. I found that Objective-C is quite useful for many tasks and, in many cases, can speed up work.

我想在Windows系统上移植我的应用程式。我意识到(如果你有任何其他解决方案,请让我知道!)Qt可能是一个非常有用的工具!使用Qt创建GUI简单而快速。问题是,Qt使用C ++和OSX工程与Objective-C。

I would like to port my applications also on Windows systems. I realized (if you have any other solutions please let me know!) that Qt could be a quite useful tool! Creating GUI with Qt is simple and fast. The problem is that Qt works with C++ and OSX works with Objective-C.

我读到Qt可以处理Objective-C源,实际上你可以找到 OBJECTIVE_SOURCES OBJECTIVE_HEADERS makefile变量。

I read that Qt can handle Objective-C sources, in fact you can find the OBJECTIVE_SOURCES and OBJECTIVE_HEADERS makefile variables.

我搜索了如何配置Qt Creator以接受 .mm 文件,但我无法得到它工作。

I searched how to configure Qt Creator to accept .mm files, but I was not able to get it to work.

.pro 文件中,我有以下类似的东西:

Inside the .pro file I have something like following:

    ...
    OBJECTIVE_SOURCES += \
        test.mm
    INCLUDEPATH += "C:\GNUStep\...\Headers" 
    LIBS += -L"C:\GNUStep\...\Libraries" -lobjc -lgnustep-base
    QMAKE_CXXFLAGS += -fconstant-string-class=NSConstantString -std=c99
    ...

我的非常简单的项目是一个小部件$ c> .h 和 .cpp ), main.cpp c $ c> test.h 与 test.mm 。混合C ++和Objective-C我想到了一个桥梁(就像我在Cocoa中混合使用Objective-C和C ++时),所以我创建了 test.h 作为一个简单的头(没有Objective-C,只有一个桥):

My very simple project is a widget (.h and .cpp), the main.cpp and test.h with its test.mm. To mix C++ and Objective-C I thought about a "bridge" (just like I do in the Cocoa when mixing Objective-C and C++), so I create the test.h as a simple header (no Objective-C, only a bridge):

    // test.h
    void testFunction( void );

比:

    // test.mm
    #import <Foundation/Foundation.h>

    void testFunction( void ) {
       NSString* string;

       string = @"This is a test string";
       NSLog( @"%@", string ); 
    }

finally:

    // main.cpp
    ...
    #include "test.h"

    ...
    testFunction();
    ...

链接器给我经典的未定义的函数testFunction引用。类似它无法链接从 .mm 文件创建的 .o 对象文件。

The linker gives me the classic "undefined reference to function testFunction". Something like it can not link the .o object files created from .mm files.

任何人都可以告诉我这个过程有什么问题,或者指向我一个关于如何配置Qt接受Objective-C语言的教程?

Can anyone of you tell me if something is wrong with this process or points me to a tutorial on how to configure Qt to accept Objective-C language?

还有另一个解决方案,我真的很喜欢,它是纯粹的Objective-C,即使用GNUStep Gorm界面构建器,但不幸的是,使用Gorm构建的GUI完全不可访问的工具,如Jaws或NVDA,我需要

There is another solution that I really like, it's pure Objective-C, i.e. using the GNUStep Gorm interface builder, but, unfortunately, GUIs built with Gorm are completely not accessible with accessibility tools like Jaws or NVDA, and I need to create some applications for visually impaired people!

推荐答案

非常感谢您为视障人士创建一些应用程序!

解决方案

链接器错误未定义的引用功能testFunction,我怀疑是由于缺乏在testFunction中定义的东西。

The linker error "undefined reference to function testFunction", I suspect is due to a lack of something being defined in the testFunction.

您可以将Objective-c与Qt混合,只需使用C函数创建一个头文件以及一个具有.m或.mm扩展名的实现文件,你可以编写objective-c代码,并在OBJECTIVE_SOURCES

You're right in that to mix Objective-c with Qt you simply create a header with C functions and an implementation file with a .m or .mm extension in which you can write objective-c code and reference the implementation in the .pro under OBJECTIVE_SOURCES

下引用.pro中的实现。由于你使用objective-c和类如 NSString ,您还需要链接到相关框架。

As you're using objective-c and classes such as NSString, you're also going to need to link to the relevant framework(s). In this case, it's Foundation.

因此,在您的配置文件(.pro)中: -

So, in your config (.pro) file:-

LIBS += -framework Foundation 

如果要添加另一个,如Appkit,它将是: -

If you want to add another, such as Appkit, it would then be:-

LIBS += -framework Foundation 
LIBS += -framework AppKit

当我混合使用这些语言时,我对.pro文件的另外一个补充是: -

The only other addition to my .pro files when I mix the languages is this:-

QMAKE_CXXFLAGS += -F/Library/Frameworks
QMAKE_CFLAGS += -F/Library/Frameworks
QMAKE_LFLAGS += -F/Library/Frameworks

在测试函数实现中引用NSString时,包括它的定义,所以只需将它添加到包含,就像你通常在一个Objective-C程序中: -

As you reference NSString in the test function implementation, you'll need to include its definition, so just add it to the includes, as you would normally do in an Objective-C program:-

#import <Foundation/NSString.h>

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

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