在Windows平台上编写Objective-C的最佳方法是什么?

Cygwin和gcc?有没有办法我可以以某种方式将它集成到Visual Studio?

沿着这条线-关于如何链接和使用Windows SDK来做这样的事情有什么建议吗?这是一个不同的野兽,但我知道我可以在Windows dll中编写汇编和链接,让我可以访问这些调用,但我不知道如何做到这一点,除非谷歌和得到零碎的方向。

有人知道有好的网络或书籍资源来做或解释这些事情吗?


当前回答

最近一个将Objective C 2.0移植到Windows的尝试是Subjective项目。

自述:

Subjective is an attempt to bring Objective C 2.0 with ARC support to Windows. This project is a fork of objc4-532.2, the Objective C runtime that ships with OS X 10.8.5. The port can be cross-compiled on OS X using llvm-clang combined with the MinGW linker. There are certain limitations many of which are a matter of extra work, while others, such as exceptions and blocks, depend on more serious work in 3rd party projects. The limitations are: • 32-bit only - 64-bit is underway • Static linking only - dynamic linking is underway • No closures/blocks - until libdispatch supports them on Windows • No exceptions - until clang supports them on Windows • No old style GC - until someone cares... • Internals: no vtables, no gdb support, just plain malloc, no preoptimizations - some of these things will be available under the 64-bit build. • Currently a patched clang compiler is required; the patch adds -fobjc-runtime=subj flag

该项目可在Github上使用,在Cocotron Group上也有一个线程概述了一些进展和遇到的问题。

其他回答

我知道这是一个非常老的帖子,但我已经找到了一个解决方案,它是最近才可用的,并且可以在Windows平台上实现几乎所有Objective-C 2.0功能。

随着gcc 4.6的出现,对Objective-C 2.0语言特性(块、点语法、合成属性等)的支持被添加到Objective-C编译器中(详见发行说明)。它们的运行时也经过了更新,几乎与苹果自己的Objective-C 2.0运行时完全相同。简而言之,这意味着(几乎)任何可以在Mac上合法地使用Clang编译的程序也可以不经修改地使用gcc 4.6编译。

作为旁注,一个不可用的功能是字典/数组/等字面量,因为它们都是硬编码到Clang中使用苹果的NSDictionary, NSArray, NSNumber等类。

然而,如果你愿意在没有苹果广泛框架的情况下生活,你也可以。 正如在其他回答中提到的,GNUStep和Cocotron提供了苹果类库的修改版本,或者你也可以编写自己的类库(我的首选)。

MinGW是在Windows平台上获得GCC 4.6的一种方式,可以从MinGW网站下载。确保在安装时包含了C, c++, Objective-C和objective - c++的安装。虽然是可选的,但我也建议安装MSYS环境。

一旦安装,Objective-C 2.0源代码可以编译:

gcc MyFile。M -lobjc -std=c99 -fobjc-exceptions -fconstant-string-class=clsname(等,其他标志,见文档)

MinGW还支持使用-mwindows标记编译本地GUI Windows应用程序。例如:

g++ -mwindows MyFile.cpp

我还没有尝试过,但我想如果你在尽可能高的层用objective - c++包装你的Objective-C类,你应该能够成功地在一个Windows应用程序中交织本机Windows GUI c++和Objective-C。

你可以在Windows环境中使用Objective C。如果你按照以下步骤操作,它应该可以正常工作:

Visit the GNUstep website and download GNUstep MSYS Subsystem (MSYS for GNUstep), GNUstep Core (Libraries for GNUstep), and GNUstep Devel After downloading these files, install in that order, or you will have problems with configuration Navigate to C:\GNUstep\GNUstep\System\Library\Headers\Foundation1 and ensure that Foundation.h exists Open up a command prompt and run gcc -v to check that GNUstep MSYS is correctly installed (if you get a file not found error, ensure that the bin folder of GNUstep MSYS is in your PATH) Use this simple "Hello World" program to test GNUstep's functionality: #include <Foundation/Foundation.h> int main(void) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSLog(@"Hello World!."); [pool drain]; return; } Go back to the command prompt and cd to where you saved the "Hello World" program and then compile it:2 gcc -o helloworld.exe <HELLOWORLD>.m -I /GNUstep/GNUstep/System/Library/Headers -L /GNUstep/GNUstep/System/Library/Libraries -std=c99 -lobjc -lgnustep-base -fconstant-string-class=NSConstantString Finally, from the command prompt, type helloworld to run it

祝一切顺利,并在Objective-C中玩得开心!


注:

我使用默认的安装路径-相应地调整您的命令行 确保您的文件夹路径与我的文件夹路径相似,否则将报错

另外:

Cocotron是一个开源项目,旨在实现一个类似于苹果公司Cocoa文档所描述的跨平台Objective-C API。这包括AppKit, Foundation, Objective-C运行时和支持api,如CoreGraphics和CoreFoundation。

http://www.cocotron.org/

截至2021年,GNUstep Windows MSVC工具链允许在任何Windows应用程序中集成Objective-C代码,包括使用LLVM/Clang的Visual Studio项目。这包括对自动引用计数(ARC)和Objective-C 2.0特性的支持,比如块。

该项目包括来自GNUstep的Foundation、CoreFoundation和libdispatch库。它目前不包括任何UI框架,如AppKit或UIKit,但它可以用来编写特定于windows的UI,使用Objective-C编写的跨平台业务逻辑。

最近一个将Objective C 2.0移植到Windows的尝试是Subjective项目。

自述:

Subjective is an attempt to bring Objective C 2.0 with ARC support to Windows. This project is a fork of objc4-532.2, the Objective C runtime that ships with OS X 10.8.5. The port can be cross-compiled on OS X using llvm-clang combined with the MinGW linker. There are certain limitations many of which are a matter of extra work, while others, such as exceptions and blocks, depend on more serious work in 3rd party projects. The limitations are: • 32-bit only - 64-bit is underway • Static linking only - dynamic linking is underway • No closures/blocks - until libdispatch supports them on Windows • No exceptions - until clang supports them on Windows • No old style GC - until someone cares... • Internals: no vtables, no gdb support, just plain malloc, no preoptimizations - some of these things will be available under the 64-bit build. • Currently a patched clang compiler is required; the patch adds -fobjc-runtime=subj flag

该项目可在Github上使用,在Cocotron Group上也有一个线程概述了一些进展和遇到的问题。