2012-02-04 58 views
3
程序時

我有下面的代碼段,它使用ICU宏以便確定UTF-8字符串長度:錯誤連接,它使用ICU

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <stdint.h> 
#include <unicode/utf.h> 

size_t utf8_strlen(uint8_t* str, size_t length) { 
    int32_t i = 0; 
    int32_t result = 0; 
    UChar32 cur; 

    while(i < length) { 
     U8_NEXT(str, i, length, cur); 
     if(cur < 0) 
      return -1; 
     result++; 
    } 

    return result; 
} 

然而,編譯和鏈接它作爲

cc `icu-config --ldflags` test.c 

我得到以下錯誤:

/tmp/ccaVwSaO.o: In function `utf8_strlen': 
test.c:(.text+0x141): undefined reference to `utf8_nextCharSafeBody_48' 
collect2: ld returned 1 exit status 

上面的命令將擴展爲cc -ldl -lm -L/usr/lib -licui18n -licuuc -licudata -ldl -lm test.c,一nd libicuuc確實在其中定義了utf8_nextCharSafeBody_48。爲什麼鏈接錯誤發生?

回答

8

嘗試:

 
$ cc test.c $(icu-config --ldflags) 

您通常需要列出去年的庫。

+0

它的工作!謝謝。 – 2012-02-04 22:28:23