我有以下代码使用ICU宏来确定UTF-8字符串长度:
#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
,libicuuc中定义了utf8_nextCharSafeBody_48
。为什么会发生链接错误?
答案 0 :(得分:8)
尝试:
$ cc test.c $( icu-config --ldflags )
您通常需要最后列出库。