Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when linking a program which uses ICU

Tags:

c

icu

I have the following piece of code which uses ICU macros in order to determine UTF-8 string length:

#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;
}

However, when compiling and linking it as

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

I get the following error:

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

The command above expands to cc -ldl -lm -L/usr/lib -licui18n -licuuc -licudata -ldl -lm test.c, and libicuuc does have utf8_nextCharSafeBody_48 defined in it. Why does the linking error happen?

like image 734
Victor Vasiliev Avatar asked Mar 29 '26 00:03

Victor Vasiliev


1 Answers

Try:

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

you typically need to list libraries last.

like image 120
William Pursell Avatar answered Apr 02 '26 05:04

William Pursell