On the ARM development board iconv_open ("gb2312", "UTF-8") call failure Solution

Source: Internet
Author: User
Tags automake
Iconv_open ("UTF-8", "gb2312") on ARM development board

The application code is as follows:

Static int code_convert (char * from_charset, char * to_charset, char * inbuf, size_t inlen, char * outbuf, size_t outlen)

{
Iconv_t CD;
Char ** pin = & inbuf;
Char ** pout = & outbuf;

Cd = iconv_open (to_charset, from_charset );
If (Cd =-1)
{
Perror ("iconv_open :");
Return-1;
}

Memset (outbuf, 0, outlen );
If (iconv (Cd, pin, & inlen, pout, & outlen) =-1)
{
// Printf ("% s: Call iconv failed! \ N ", _ function __);
Printf ("errno = % d \ n", errno );
Perror ("iconv failed: \ n ");
Iconv_close (CD );
Return-1;
}
Iconv_close (CD );
Return 0;
}

Static int g2u (char * inbuf, size_t inlen, char * outbuf, size_t outlen)
{
Return code_convert ("UTF-8", "gb2312", inbuf, inlen, outbuf, outlen );
}

The development environment is ubuntu11.10, the Development Board is arm Development Board, the cross compiler version is arm-linux-4.4.3. The same C source program can be executed normally on ubuntu11.10, but cannot be executed normally on the arm Development Board. Call iconv_open ("gb2312 ",
"UTF-8") returns an error message "invalid argument ". After querying the information, I learned that the iconv-related function is a function in libc. The preliminary analysis shows that it may be caused by different versions of iconv-related functions in libc. Therefore, I need to update the iconv-related function. There are two ways to update iconv functions:

  1. First, update the libc library;
  2. Update the libiconv library.

The first method is troublesome to update the libc library, because we use the compiled cross compiler. In this method, we need to re-compile to generate a cross compiler, you also need to use the new compilation tool to re-compile the application. Therefore, this method is too costly and the second method is used.


The second method is to update only the libiconv library, go to the iconv official website to download the latest library source code package, which is: Upgrade:

This library can be built and installed in two variants:

-The library mode. This works on all systems, and uses a library

'Libiconv. so' and a header file' <iconv. h> '. (both are installed

Through "make install ".)

To use it, simply # include <iconv. h> and use the functions.

To use it in an autoscaling package:

-If you don't use automake, append M4/iconv. M4 to your aclocal. M4

File.

-If you do use automake, add M4/iconv. M4 to your M4 macro repository.

-Add to the link command line of libraries and executables that use

The functions the placeholder @ libiconv @ (or, if using libtool

The link, @ ltlibiconv @). If you use automake, the right place

These additions are the * _ ldadd variables.

Note that 'iconv. M4 'is also part of the GNU gettext package, which

Installit in/usr/local/share/aclocal/iconv. M4.

-The libc plug/override mode. This works on GNU/Linux, Solaris and OSF/1

Systems only. It is a way to get good iconv support without having

Glibc-2.1.

It installa library 'preloadable _ libiconv. so'. This library can be used

With ld_preload, to override the iconv * functions present in the C library.

On GNU/Linux and Solaris:

$ Export ld_preload =/usr/local/lib/preloadable_libiconv.so

On OSF/1:

$ Export _ rld_list =/usr/local/lib/preloadable_libiconv.so: Default

A program's source need not be modified, the program need not even be

Recompiled. Just set the ld_preload environment variable, that's it!

I successfully used the "the libc plug/override mode" experiment on the arm Development Board. The following describes the compilation process:

Under the libiconv directory:

$./Configure -- prefix = $ PWD/out -- Host = arm-Linux

$ Make

$ Make install

After the preceding command is executed, a new out directory is generated under the libiconv Directory, which contains four directories: Bin, include, Lib, and share; under the lib directory is the generated library file, one of which is preloadable_libiconv.so, download it to the lib directory of the Development Board, and then set the system environment variables of the Development Board:

$ Export ld_preload =/lib/preloadable_libiconv.so

Then run the application.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.