最近剛剛開始玩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; }