在xcode中设置include和lib路径

最近刚刚开始玩xcode,对着教程学编程时很少要动到项目设置,但昨天晚上想使用freetype验证上篇博文的问题,就需要设置include和lib路径了。

首先我下了freetype的源码,并在本地编译安装:

$ cd freetype-2.6
$ ./configuration
$ make check
$ make install

很顺利。

直接新建一个命令行的project,贴入代码 - 编译,就会得到如下编译错误:

找不到头文件,应该是没有把ft2build.h所在的路径添加到include path中来,找到代码的Build Settings,找到Header Search Paths,把freetype相关头文件所在路径加进去,如下:

很显然下面那行Library Search Paths也是需要的:

再次编译,发现还是有链接错误:

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

一定是没有指定freetype的lib文件。这个设置和VisualStudio下的不太一样,需要直接设置链接参数。我去/usr/local/lib下找到多个和freetype相关的文件:

libfreetype.6.dylib
libfreetype.a
libfreetype.dylib
libfreetype.la

经过反复试验,应该填-lfreetype:

 

写几行使用freetype的代码,终于可以编过了!

#import <Foundation/Foundation.h>
#import <ft2build.h>
#include FT_FREETYPE_H

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        FT_Library  library;
        FT_Face     face;
        int error = 0;
        
        if (FT_Init_FreeType(&library)){
            printf("1\n");
            return 0; /* leave it uninitialized */
        }
        
        error = FT_New_Face( library, "/System/Library/Fonts/Apple Color Emoji.ttf", 0, &face );
        if(error){
            printf("2:error=%d\n", error);
            return 0;
        }
        
        error = FT_Set_Pixel_Sizes(face, 0, 16);
        if (error) {
            printf("3:error=%d\n", error);
        }
        
        // insert code here...
        NSLog(@"Hello, World!");
    }
    return 0;
}

 

posted @ 2015-09-15 13:14  palance  阅读(5223)  评论(0编辑  收藏  举报