Need help to compile and run c++ source file with icu4c

406 views Asked by At

OS: Windows 10

Compiler: g++.exe (MinGW-W64 x86_64-posix-seh, built by Brecht Sanders) 11.2.0

Shell: msys2

Describe the bug:

I want to use class UnicodeString in icu4c in my c++ code. I downloaded the icu4c source code and compiled it. I compiled my code with icu4c library, but the result .exe file can't be executed successfully. If I run that .exe file in msys2, it outputs nothing. If I double clicked that .exe file in windows file explorer, it shows a dialog saying "xxx.exe - Can't find the entry. Unable to locate program entry point xxxxx in the dynamic link library xxx.exe".

enter image description here

What I did:

  1. Download icu4c-70_1-src.zip and compile it following steps of this link Compiling-ICU-with-MinGW
$ cd icu/source
$ CC=gcc CXX=g++  ./runConfigureICU MinGW prefix=$PWD/../dist
make && make install

When I run make install, it displays error like: create symbol link failed. But there exists files like libicuuc.dll.a in the directory. So I supposed I have compiled icu4c successfully.

  1. my c++ source file

#include <unicode/unistr.h>
#include <unicode/ustream.h>

#include <iostream>

using namespace std;
using namespace icu;

int main() {
    cout << "hello,world" << endl;

    UnicodeString s("你好,世界");
    cout << s << endl;
}

  1. compile my code
g++ main.cpp -I/c/Users/stskyblade/source/icu4c-70_1-src/icu/dist/include -L/c/Users/stskyblade/source/icu4c-70_1-src/icu/dist/lib -licuuc -licuio
  1. run

Execute ./a.exe or double click the icon in file explorer

What I expect

My program can output hello,world both in English and in Chinese.

2

There are 2 answers

0
stskyblade On

I have spent some time on this problem. Here is the answer: This program used wrong dll files when be executed.

There are two versions of ICU4C dll files in my computer.

Some of them are located in directory C:\Program Files\icu4c-70_1-Win64-MSVC2019\bin64, and they are downloaded directly from github release page of ICU4C. They are built with Microsoft's compiler. This directory is in the PATH environment variable.

Another version are located in directory C:\Users\xxxx\source\icu4c-70_1-src\icu\dist\bin. They are built with mingw-w64 GCC compiler on my own. And this directory is not in the PATH environment variable.

I compiled my program with library files of the second version. But the system use the first version dll files when I execute my program. That's it. These two versions of library is not compatible.

Symbol _ZN6icu_70lsERSoRKNS_13UnicodeStringE is a function called by my function when I compile my program with MinGW version library. The entry point of that symbol should be provided by ICUIO70.dll when I execute my program. But the MSVC version library doesn't provide it.

0
Brecht Sanders On

I tried your code and it works fine.

But you're building a shared version of your application, which makes your .exe file depend on .dll files, which most likely can't be found when you run your .exe.

A few ways to get around that:

  • (simplest and quickest) copy a.exe to the folder under the location where you extracted icu4c-70_1-src.zip where the .dll files are located and run it from there
  • copy the required .dll files (or all of them if you don't know which ones) to the same location as a.exe and then try running a.exe again
  • add the location containing the .dll files to the PATH environment variable (e.g. in Command Prompt: SET PATH=C:\Program Files\icu4c-70_1-Win64-MSVC2019\bin64;%PATH%) before running a.exe
  • build a static version that links a.exe to static libraries, but this may also require icu's dependencies to be specified manually and their dependencies, etc...

Warning: Make sure not to mix 32-bit and 64-bit .exe and .dll files.