适用于 Windows 的 Objective-C
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/56708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective-C for Windows
提问by Luther Baker
What would be the best way to write Objective-C on the Windows platform?
在 Windows 平台上编写 Objective-C 的最佳方式是什么?
Cygwin and gcc? Is there a way I can somehow integrate this into Visual Studio?
Cygwin和gcc?有没有办法以某种方式将它集成到 Visual Studio 中?
Along those lines - are there any suggestions as to how to link in and use the Windows SDK for something like this. Its a different beast but I know I can write assembly and link in the Windows DLLs giving me accessibility to those calls but I don't know how to do this without googling and getting piecemeal directions.
沿着这些思路 - 是否有任何关于如何链接和使用 Windows SDK 的建议。它是一个不同的野兽,但我知道我可以在 Windows DLL 中编写程序集和链接,使我可以访问这些调用,但我不知道如何在不使用谷歌搜索和获得零碎指示的情况下做到这一点。
Is anyone aware of a good online or book resource to do or explain these kinds of things?
有没有人知道一个很好的在线或书籍资源来做或解释这些事情?
采纳答案by Michael Buckley
Expanding on the two previous answers, if you just want Objective-C but not any of the Cocoa frameworks, then gcc will work on any platform. You can use it through Cygwin or get MinGW. However, if you want the Cocoa frameworks, or at least a reasonable subset of them, then GNUStep and Cocotron are your best bets.
扩展前两个答案,如果您只想要 Objective-C 而不是任何 Cocoa 框架,那么 gcc 将适用于任何平台。您可以通过 Cygwin 或获取 MinGW 使用它。但是,如果您想要 Cocoa 框架,或者至少是其中一个合理的子集,那么 GNUStep 和 Cocotron 是您最好的选择。
Cocotron implements a lot of stuff that GNUStep does not, such as CoreGraphics and CoreData, though I can't vouch for how complete their implementation is on a specific framework. Their aim is to keep Cocotron up to date with the latest version of OS X so that any viable OS X program can run on Windows. Because GNUStep typically uses the latest version of gcc, they also add in support for Objective-C++ and a lot of the Objective-C 2.0 features.
Cocotron 实现了很多 GNUStep 没有的东西,比如 CoreGraphics 和 CoreData,但我不能保证它们在特定框架上的实现有多完整。他们的目标是让 Cocotron 与最新版本的 OS X 保持同步,以便任何可行的 OS X 程序都可以在 Windows 上运行。因为 GNUStep 通常使用最新版本的 gcc,它们还添加了对 Objective-C++ 和许多 Objective-C 2.0 特性的支持。
I haven't tested those features with GNUStep, but if you use a sufficiently new version of gcc, you might be able to use them. I was not able to use Objective-C++ with GNUStep a few years ago. However, GNUStep does compile from just about any platform. Cocotron is a very mac-centric project. Although it is probably possible to compile it on other platforms, it comes XCode project files, not makefiles, so you can only compile its frameworks out of the box on OS X. It also comes with instructions on compiling Windows apps on XCode, but not any other platform. Basically, it's probably possible to set up a Windows development environment for Cocotron, but it's not as easy as setting one up for GNUStep, and you'll be on your own, so GNUStep is definitely the way to go if you're developing on Windows as opposed to just for Windows.
我没有用 GNUStep 测试过这些功能,但是如果您使用足够新的 gcc 版本,您也许可以使用它们。几年前,我无法在 GNUStep 中使用 Objective-C++。但是,GNUStep 几乎可以从任何平台进行编译。Cocotron 是一个非常以 Mac 为中心的项目。虽然可能可以在其他平台上编译它,但它带有 XCode 项目文件,而不是 makefile,因此您只能在 OS X 上开箱即用地编译它的框架。它还带有在 XCode 上编译 Windows 应用程序的说明,但没有任何其他平台。基本上,为 Cocotron 设置一个 Windows 开发环境可能是可能的,但它不像为 GNUStep 设置一个那么容易,而且您将独自一人,因此如果您正在开发,GNUStep 绝对是您的最佳选择Windows 而不是仅适用于 Windows。
For what it's worth, Cocotron is licensed under the MIT license, and GNUStep is licensed under the LGPL.
值得一提的是,Cocotron 是在 MIT 许可下获得许可的,而 GNUStep 是在 LGPL 下获得许可的。
回答by teshguru
You canuse Objective C inside the Windows environment. If you follow these steps, it should be working just fine:
您可以在 Windows 环境中使用 Objective C。如果您按照以下步骤操作,它应该可以正常工作:
- Visit the GNUstep websiteand download
GNUstep MSYS Subsystem(MSYS for GNUstep),GNUstep Core(Libraries for GNUstep), andGNUstep Devel - After downloading these files, install in that order, or you will have problems with configuration
- Navigate to
C:\GNUstep\GNUstep\System\Library\Headers\Foundation1and ensure thatFoundation.hexists - Open up a command prompt and run
gcc -vto check thatGNUstep MSYSis correctly installed (if you get a file not found error, ensure that thebinfolder ofGNUstep MSYSis in yourPATH) 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
cdto where you saved the "Hello World" program and then compile it:2gcc -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=NSConstantStringFinally, from the command prompt, type
helloworldto run it
- 访问GNUstep 网站并下载
GNUstep MSYS Subsystem(用于 GNUstep 的 MSYS)、GNUstep Core(用于 GNUstep 的库),以及GNUstep Devel - 下载完这些文件后,按顺序安装,不然配置会出问题
- 导航到
C:\GNUstep\GNUstep\System\Library\Headers\Foundation1并确保Foundation.h存在 - 打开命令提示符并运行
gcc -v以检查是否GNUstep MSYS已正确安装(如果出现文件未找到错误,请确保 的bin文件夹GNUstep MSYS在您的 中PATH) 使用这个简单的“Hello World”程序来测试 GNUstep 的功能:
#include <Foundation/Foundation.h> int main(void) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSLog(@"Hello World!."); [pool drain]; return; }返回命令提示符和
cd保存“Hello World”程序的位置,然后编译它:2gcc -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最后,在命令提示符下,键入
helloworld以运行它
All the best, and have fun with Objective-C!
祝您一切顺利,并享受 Objective-C 带来的乐趣!
NOTES:
注意事项:
- I used the default install path - adjust your command line accordingly
- Ensure the folder path of yours is similar to mine, otherwise you will get an error
- 我使用了默认安装路径 - 相应地调整你的命令行
- 确保你的文件夹路径和我的相似,否则你会得到一个错误
回答by amrox
Also:
还:
The Cocotron is an open source project which aims to implement a cross-platform Objective-C API similar to that described by Apple Inc.'s Cocoa documentation. This includes the AppKit, Foundation, Objective-C runtime and support APIs such as CoreGraphics and CoreFoundation.
Cocotron 是一个开源项目,旨在实现类似于 Apple Inc. 的 Cocoa 文档中描述的跨平台 Objective-C API。这包括 AppKit、Foundation、Objective-C 运行时和支持 API,例如 CoreGraphics 和 CoreFoundation。
回答by Lee Stott
WinObjC? Windows Bridge for iOS (previously known as ‘Project Islandwood').
WinObjC?适用于 iOS 的 Windows Bridge(以前称为“Project Islandwood”)。
Windows Bridge for iOS (also referred to as WinObjC) is a Microsoft open source project that provides an Objective-C development environment for Visual Studio/Windows. In addition, WinObjC provides support for iOS API compatibility. While the final release will happen later this fall (allowing the bridge to take advantage of new tooling capabilities that will ship with the upcoming Visual Studio 2015 Update),
Windows Bridge for iOS(也称为 WinObjC)是一个 Microsoft 开源项目,为 Visual Studio/Windows 提供了一个 Objective-C 开发环境。此外,WinObjC 提供了对 iOS API 兼容性的支持。虽然最终版本将在今年秋季晚些时候发布(允许桥利用即将推出的 Visual Studio 2015 更新附带的新工具功能),
The bridge is available to the open-source community now in its current state. Between now and the fall. The iOS bridge as an open-source project under the MIT license. Given the ambition of the project, making it easy for iOS developers to build and run apps on Windows.
该桥现在处于当前状态,可供开源社区使用。从现在到秋天。iOS 桥接器是 MIT 许可下的开源项目。鉴于该项目的雄心壮志,iOS 开发人员可以轻松地在 Windows 上构建和运行应用程序。
Salmaan Ahmed has an in-depth post on the Windows Bridge for iOS http://blogs.windows.com/buildingapps/2015/08/06/windows-bridge-for-ios-lets-open-this-up/discussing the compiler, runtime, IDE integration, and what the bridge is and isn't. Best of all, the source code for the iOS bridge is live on GitHub right now.
Salmaan艾哈迈德有一个深入的岗位上的Windows大桥于iOS http://blogs.windows.com/buildingapps/2015/08/06/windows-bridge-for-ios-lets-open-this-up/讨论编译器、运行时、IDE 集成以及桥接器是什么和不是什么。最重要的是,iOS 桥的源代码现在在GitHub 上。
The iOS bridge supports both Windows 8.1 and Windows 10 apps built for x86 and x64 processor architectures, and soon we will add compiler optimizations and support for ARM, which adds mobile support.
iOS 桥接器支持为 x86 和 x64 处理器架构构建的 Windows 8.1 和 Windows 10 应用程序,很快我们将添加编译器优化和对 ARM 的支持,从而增加移动支持。
回答by Matthieu Cormier
I have mixed feelings about the Cocotron project. I'm glad they are releasing source code and sharing but I don't feel that they are doing things the easiest way.
我对 Cocotron 项目有复杂的感觉。我很高兴他们发布源代码并共享,但我不觉得他们在做事情最简单的方式。
Examples.
Apple has released the source code to the objective-c runtime, which includes properties and garbage collection. The Cocotron project however has their own implementation of the objective-c runtime. Why bother to duplicate the effort? There is even a Visual Studio Project file that can be used to build an objc.dll file. Or if you're really lazy, you can just copy the DLL file from an installation of Safari on Windows.
例子。
Apple 已经发布了Objective-c 运行时的源代码,其中包括属性和垃圾收集。但是 Cocotron 项目有自己的 Objective-c 运行时实现。为什么要重复努力?甚至还有一个可用于构建 objc.dll 文件的 Visual Studio 项目文件。或者,如果您真的很懒惰,则可以从 Windows 上的 Safari 安装中复制 DLL 文件。
They also did not bother to leverage CoreFoundation, which is also open sourced by Apple. I posted a questionabout this but did not receive an answer.
他们也没有费心去利用同样由 Apple 开源的 CoreFoundation。我发布了一个关于此的问题,但没有收到答案。
I think the current best solution is to take source code from multiple sources (Apple, CocoTron, GnuStep) and merge it together to what you need. You'll have to read a lot of source but it will be worth the end result.
我认为目前最好的解决方案是从多个来源(Apple、CocoTron、GnuStep)获取源代码并将其合并到您需要的地方。你必须阅读大量的源代码,但最终结果是值得的。
回答by Ephemera
I'm aware this is a very old post, but I have found a solution which has only become available more recently AND enables nearly all Objective-C 2.0 features on the Windows platform.
我知道这是一篇很老的帖子,但我找到了一个最近才可用的解决方案,并且在 Windows 平台上启用了几乎所有的 Objective-C 2.0 功能。
With the advent of gcc 4.6, support for Objective-C 2.0 language features (blocks, dot syntax, synthesised properties, etc) was added to the Objective-C compiler (see the release notesfor full details). Their runtime has also been updated to work almost identically to Apple's own Objective-C 2.0 runtime. In short this means that (almost) any program that will legitimately compile with Clang on a Mac will also compile with gcc 4.6 without modification.
随着 gcc 4.6 的出现,Objective-C 编译器添加了对 Objective-C 2.0 语言特性(块、点语法、合成属性等)的支持(有关完整详细信息,请参阅发行说明)。它们的运行时也已更新,几乎与 Apple 自己的 Objective-C 2.0 运行时相同。简而言之,这意味着(几乎)任何可以在 Mac 上使用 Clang 合法编译的程序也可以使用 gcc 4.6 编译而无需修改。
As a side-note, one feature that is not available is dictionary/array/etc literals as they are all hard-coded into Clang to use Apple's NSDictionary, NSArray, NSNumber, etc classes.
作为旁注,一个不可用的功能是字典/数组/等文字,因为它们都被硬编码到 Clang 中以使用 Apple 的 NSDictionary、NSArray、NSNumber 等类。
However, if you are happy to live without Apple's extensive frameworks, you can. As noted in other answers, GNUStep and the Cocotron provide modified versions of Apple's class libraries, or you can write your own (my preferred option).
但是,如果您乐于在没有 Apple 广泛的框架的情况下生活,那么您可以。正如其他答案中所述,GNUStep 和 Cocotron 提供了 Apple 类库的修改版本,或者您可以编写自己的类库(我的首选选项)。
MinGW is one way to get GCC 4.6 on the Windows platform, and can be downloaded from The MinGW website. Make sure when you install it you include the installation of C, C++, Objective-C and Objective-C++. While optional, I would also suggest installing the MSYS environment.
MinGW 是在 Windows 平台上获取 GCC 4.6 的一种方式,可以从The MinGW 网站下载。确保在安装时包含 C、C++、Objective-C 和 Objective-C++ 的安装。虽然可选,但我还建议安装 MSYS 环境。
Once installed, Objective-C 2.0 source can be compiled with:
安装后,Objective-C 2.0 源代码可以编译为:
gcc MyFile.m -lobjc -std=c99 -fobjc-exceptions -fconstant-string-class=clsname (etc, additional flags, see documentation)
gcc MyFile.m -lobjc -std=c99 -fobjc-exceptions -fconstant-string-class=clsname (etc, additional flags, see documentation)
MinGW also includes support for compiling native GUI Windows applications with the -mwindowsflag. For example:
MinGW 还包括对使用该-mwindows标志编译本机 GUI Windows 应用程序的支持。例如:
g++ -mwindows MyFile.cpp
g++ -mwindows MyFile.cpp
I have not attempted it yet, but I imagine if you wrap your Objective-C classes in Objective-C++ at the highest possible layer, you should be able to successfully intertwine native Windows GUI C++ and Objective-C all in the one Windows Application.
我还没有尝试过,但我想如果您将 Objective-C 类包装在 Objective-C++ 中的最高层,您应该能够在一个 Windows 应用程序中成功地将本机 Windows GUI C++ 和 Objective-C 交织在一起。
回答by James Ko
Check out WinObjC:
查看WinObjC:
https://github.com/Microsoft/WinObjC
https://github.com/Microsoft/WinObjC
It's an official, open-source project by Microsoft that integrates with Visual Studio + Windows.
它是 Microsoft 的一个官方开源项目,与 Visual Studio + Windows 集成。
回答by user397362
回答by Aaron Stainback
You can get an objective c compiler that will work with Windows and play nice with Visual Studio 2008\2010 here.
你可以得到一个客观的 c 编译器,它可以在 Windows 上运行,并在此处与 Visual Studio 2008\2010 配合使用。
Just download the latest source. You don't need to build all of CF-Lite there is a solution called objc.sln. You will need to fix a few of the include paths but then it will build just fine. There is even a test project included so you can see some objective-c .m files being compiled and working in visual studio. One sad thing is it only works with Win32 not x64. There is some assembly code that would need to be written for x64 for it to support that.
只需下载最新的源。您不需要构建所有 CF-Lite,有一个名为 objc.sln 的解决方案。您将需要修复一些包含路径,但它会构建得很好。甚至还包含一个测试项目,因此您可以看到一些 Objective-c .m 文件正在编译并在 Visual Studio 中工作。一件可悲的是它仅适用于 Win32 而不适用于 x64。需要为 x64 编写一些汇编代码才能支持它。
回答by insys
A recent attempt to port Objective C 2.0 to Windows is the Subjectiveproject.
最近将 Objective C 2.0 移植到 Windows 的尝试是主观项目。
From the Readme:
来自自述文件:
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
主观是尝试将带有 ARC 支持的 Objective C 2.0 引入 Windows。
这个项目是 objc4-532.2 的一个分支,objc4-532.2 是 OS X 10.8.5 附带的 Objective C 运行时。该端口可以使用 llvm-clang 结合 MinGW 链接器在 OS X 上交叉编译。
存在某些限制,其中许多是额外工作的问题,而其他限制(例如异常和块)则取决于 3rd 方项目中更认真的工作。限制是:
? 仅限 32 位 - 64 位正在进行中
? 仅静态链接 - 正在进行动态链接
? 没有闭包/阻塞 - 直到 libdispatch 在 Windows 上支持它们
? 没有例外 - 直到 clang 在 Windows 上支持它们
? 没有旧式 GC - 直到有人关心......
? 内部结构:没有 vtables,没有 gdb 支持,只是普通的 malloc,没有预优化 - 其中一些东西将在 64 位构建下可用。
? 目前需要打补丁的 clang 编译器;该补丁添加了 -fobjc-runtime=subj 标志
The project is available on Github, and there is also a threadon the Cocotron Group outlining some of the progress and issues encountered.

