将对象c(.m)文件编译为osx

时间:2016-03-28 03:47:16

标签: objective-c macos gcc

感谢您查看问题。有一个名为try.m的对象c文件,我使用以下命令将其编译为目标文件try.o:

gcc -c try.m -o try.o -framework Foundation

try.h是

int print_word( void );

try.m是:

#include "try.h" 
#import <Foundation/Foundation.h>

int print_word( void )
{
  NSLog (@"say hello");
  return 0;
}

另外,还有一个main.c文件,它包含main()函数,它看起来像:

#include <stdio.h>
#include "try.h"

int main()
{
   printf( "This is main\n");
}

我通过以下命令将main.c编译为main.o:

gcc -o main.o -c main.c

然后,我链接main.o和try.o以形成可执行文件main:

gcc  -o main main.o try.o

执行这些步骤后,发生以下错误: enter image description here

错误是:

Undefined symbols for architecture x86_64:
"_NSLog", referenced from:
    _print_word in try.o
ld: symbol(s) not found for architecture x86_64    
clang: error: linker command failed with exit code 1 (use -v to see invocation)

如何解决这些错误?

1 个答案:

答案 0 :(得分:2)

您需要链接到Foundation框架。您可以在一个命令中完成所有操作:gcc main.c try.m -framework Foundation