windows 帮助 GCC 和 ObjectiveC 代码和 Cygwin

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1466718/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:11:30  来源:igfitidea点击:

Help with GCC and ObjectiveC code and Cygwin

objective-cwindowswinapigcccygwin

提问by Berlin Brown

Is it possible to build objective c code correctly with GCC under cygwin.

是否可以在 cygwin 下使用 GCC 正确构建目标 c 代码。

I have the following application that should work on a Mac environment, but can't get the most basic stuff to work with gcc. Are there more libraries I need to have.

我有以下应该在 Mac 环境下工作的应用程序,但无法获得使用 gcc 的最基本的东西。我需要拥有更多图书馆吗?

#import "HelloWorldApp.h"

int main(int argc, char *argv[]) {
    return 0;
} // End of the // 

@interface Car
{
    int test;
}

//The registration is a read-only field, set by copy
@property int (readonly, copy) test;

//the driver is a weak reference (no retain), and can be modified
//@property Person* (assign) driver;

@end


CC=gcc
CXX=gcc-g++
LD=$(CC)

CFLAGS=

LDFLAGS=-lobjc

all: HelloWorld

HelloWorld: HelloWorld.o
    $(LD) $(LDFLAGS) -o $@ $^

%.o: %.m
    $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@

clean:
    rm -rvf *.o HelloWorld HelloWorld.exe

Error:

错误:

gcc -c HelloWorld.m -o HelloWorld.o
In file included from HelloWorld.m:6:
HelloWorldApp.h:19: error: stray '@' in program
HelloWorldApp.h:19: error: parse error before "int"
make: *** [HelloWorld.o] Error 1

回答by F'x

I think it's because you're using Objective-C 2.0, which is a private extension that Apple developed and that they did not contribute back to the "standard", FSF GCC. Thus, your mingw compiler (which is not based on Apple's, but on the FSF's) does not understand new syntax like properties.

我认为这是因为您使用的是 Objective-C 2.0,这是 Apple 开发的私有扩展,并且他们没有为“标准”FSF GCC 做出贡献。因此,您的 mingw 编译器(不是基于 Apple 的,而是基于 FSF 的)不理解新的语法,如属性。

回答by NewToObjC

@ Malaxeur - you hit the nail right on the head. I've just been bashing about with ObjC on a PC and had that exact problem - my path was pointing to a v3.x gcc compiler. I switched to the 4.x version and voila!

@ Malaxeur - 你一针见血。我刚刚在 PC 上用 ObjC 抨击并遇到了那个确切的问题 - 我的路径指向 v3.x gcc 编译器。我切换到 4.x 版本,瞧!

It's also worth noting that the "stray '@'" error also pops up if you spell 'implementation' wrong, which is a little misleading.

还值得注意的是,如果“implementation”拼写错误,也会弹出“stray '@'”错误,这有点误导。

Anyway, thanks for the help.

无论如何,谢谢你的帮助。

回答by Jesse Vogt

Have you tried running the gcc commands directly from command line rather than via makefile?

您是否尝试过直接从命令行而不是通过 makefile 运行 gcc 命令?

Found this reference:

找到这个参考:

Taken directly from http://www.cs.indiana.edu/classes/c304/ObjCompile.html

直接取自http://www.cs.indiana.edu/classes/c304/ObjCompile.html

Compiling Objective-C

编译 Objective-C

Objective-C code can be compiled using the GNU C compiler gcc. Objective-C interface files usually marked by the extension .h as in List.h. Implementation files are usually marked by the extension .m as in List.m. The implementation file should #import its corresponding interface file. Your main program should also #import the interface files of all of the classes it uses. Usually, you will want to compile the files which implement your classes separately. For instance, to compile List.m, use the following command:

可以使用 GNU C 编译器 gcc 编译 Objective-C 代码。Objective-C 接口文件通常用扩展名 .h 标记,如 List.h。实现文件通常用扩展名 .m 标记,如 List.m。实现文件应该#import 其对应的接口文件。您的主程序还应该#import 它使用的所有类的接口文件。通常,您需要分别编译实现您的类的文件。例如,要编译 List.m,请使用以下命令:

gcc -c -Wno-import List.m

The -c switch tells the compiler to produce an object file, List.o, which can then later be linked into your program. Do this for each of your implementation files, and your main program.

-c 开关告诉编译器生成一个目标文件 List.o,然后可以将其链接到您的程序中。对每个实现文件和主程序执行此操作。

The -Wno-import switch tells the compiler not to give a warning if you have a #import statement in your code. For some reason, Richard Stallman (the GNU Project founder) doesn't like the #import construction.

-Wno-import 开关告诉编译器不要在代码中有 #import 语句时发出警告。出于某种原因,Richard Stallman(GNU 项目创始人)不喜欢#import 结构。

When you are ready to compile your program, you can link all of the implementations of your classes using gcc again. For example, to compile the files List.o, and main.o you could use the following command:

当您准备好编译您的程序时,您可以再次使用 gcc 链接您的类的所有实现。例如,要编译文件 List.o 和 main.o,您可以使用以下命令:

gcc -o prog -Wno-import List.o main.o -lobjc

The -o prog tells gcc to create an executable program with the name prog (if you do not specify this, it will default to a.out.

-o prog 告诉 gcc 创建一个名为 prog 的可执行程序(如果你不指定它,它会默认为 a.out。

Note that when linking Objective-C with gcc, you need to specify the Objective-C library by using the -lobjc switch. If gcc reports an error, contact your system administrator to make sure that the Objective-C library and header files (objc/Object.h) were installed when gcc was built.

请注意,将 Objective-C 与 gcc 链接时,需要使用 -lobjc 开关指定 Objective-C 库。如果 gcc 报告错误,请联系您的系统管理员以确保在构建 gcc 时安装了 Objective-C 库和头文件 (objc/Object.h)。