链接使用ICU的程序时出错

Vic*_*iev 3 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;
}
Run Code Online (Sandbox Code Playgroud)

但是,在编译和链接时

cc `icu-config --ldflags` test.c
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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

上面的命令扩展为cc -ldl -lm -L/usr/lib -licui18n -licuuc -licudata -ldl -lm test.c,并且libicuuc确实在其中utf8_nextCharSafeBody_48定义.为什么会发生链接错误?

Wil*_*ell 8

尝试:

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

您通常需要最后列出库.