简体   繁体   English

用GCC编译* .mm

[英]Compiling *.mm with GCC

I'm making a very simple Objective-C/C program in a .mm file but I don't know how to link the Objective-C libraries/frameworks. 我正在.mm文件中制作一个非常简单的Objective-C / C程序,但是我不知道如何链接Objective-C库/框架。 I only need the Foundation framework. 我只需要基金会框架。 How can I do this? 我怎样才能做到这一点? I don't want to do this in Xcode because Xcode always creates an entire folder with project files and then buries the product in a hard-to-find folder and I feel that it is a lot of memory used for a one-file program. 我不想在Xcode中执行此操作,因为Xcode始终会创建一个包含项目文件的完整文件夹,然后将产品埋在一个难以找到的文件夹中,我觉得一个文件程序会占用很多内存。

Code: 码:

#include <stdio.h>
#import <Foundation/Foundation.h>

NSString* charToNSString(char * c){
    return [NSString stringWithUTF8String:c];
}

const char* NSStringToChar(NSString *str){
    return [str UTF8String];
}

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

Compiling: gcc file.mm 编译: gcc file.mm

Output: 输出:

Undefined symbols for architecture x86_64:
  "_OBJC_CLASS_$_NSString", referenced from:
      objc-class-ref in file-4bd3e4.o
  "_objc_msgSend", referenced from:
      charToNSString(char*) in file-4bd3e4.o
      NSStringToChar(NSString*) in file-4bd3e4.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
$ gcc -framework Foundation main.mm

Although above command works perfectly, but I suggest you to create Makefile 虽然上面的命令运行完美,但是我建议您创建Makefile

$vim Makefile

paste the content below 粘贴下面的内容

CC=gcc

FRAMEWORKS:= -framework Foundation

SOURCE=main.mm

OUT=-o main

all:
        $(CC) $(SOURCE) $(FRAMEWORKS) $(OUT)
#   ^ this is a tab not spaces, if you give spaces here it will through error :*** missing separator

then run 然后跑

$ make

Just look at this simple tutorial about Makefile 只需看一下有关Makefile的简单教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM