链接使用ICU的程序时出错

时间:2012-02-04 22:05:50

标签: c icu

我有以下代码使用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。为什么会发生链接错误?

1 个答案:

答案 0 :(得分:8)

尝试:

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

您通常需要最后列出库。