对'icu_56::UnicodeString::UnicodeString(signed char, unsigned short const*, int)' 的未定义引用



>我正在运行 ubuntu,我可以构建 ICU

我包括:

#include <unistr.h>
using namespace icu;

这是我为 ICU 构建的方法:

CPPFLAGS="-DU_USING_ICU_NAMESPACE=0" 
CPPFLAGS="-DU_CHARSET_IS_UTF8=1"
export CFLAGS="-DU_CHARSET_IS_UTF8=1 -DU_GNUC_UTF16_STRING=1 -DU_STATIC_IMPLEMENTATION"
export CXXFLAGS="-DU_USING_ICU_NAMESPACE=0 -std=gnu++0x -DU_CHARSET_IS_UTF8=1 -DU_GNUC_UTF16_STRING=1 -DU_HAVE_CHAR16_T=1 -DUCHAR_TYPE=char16_t -Wall --std=c++0x -DU_STATIC_IMPLEMENTATION"
export CPPFLAGS="-DU_USING_ICU_NAMESPACE=0 -DU_CHARSET_IS_UTF8=1 -DU_STATIC_IMPLEMENTATION"
export LDFLAGS="-std=gnu++0x"
./runConfigureICU Linux --enable-static --disable-shared --disable-renaming
make check 
sudo make install

然后我链接到

/usr/local/lib/libicuuc.a

并尝试编译

icu::UnicodeString s1=UNICODE_STRING("such characters are safe 123 %-.", 32);

但得到错误

undefined reference to `icu_56::UnicodeString::UnicodeString(signed char, unsigned short const*, int)'

我在SO上找到了另一篇关于同一问题的帖子,但是当我按照提供的步骤进行操作时,它不能解决我的问题,并且可能是不同的版本。

编辑:这是构建项目时IDE的输出

Cleaning Solution: myProject (Debug)
Cleaning: myProject (Debug)
Removing output files...
Clean complete
Building Solution: myProject (Debug)
Building: myProject (Debug)
Performing main compilation...
Precompiling headers
    Compiling source to object files
    g++  -MMD "/home/user/myProject/myProject/main.cpp" -g -O0 -std=c++11 -DDEBUG -I"/home/user/myProject/myProject/include" -I"/home/user/myProject/icu/unicode" -I"/home/user/myProject/myProject/.prec/Debug"  -c -o "/home/user/myProject/myProject/bin/Debug/main.o"
    Generating binary "myProject" from object files
    g++ -o "/home/user/myProject/myProject/bin/Debug/myProject" "/home/user/myProject/myProject/bin/Debug/main.o"
"/home/user/myProject/icu/libicuuc.a" 
    /home/user/myProject/myProject/bin/Debug/main.o: In function `icuTest':
    /home/user/myProject/myProject/icuTest.hpp:3: undefined reference to `icu_56::StringPiece::StringPiece(char const*)'
    /home/user/myProject/myProject/icuTest.hpp:3: undefined reference to `icu_56::UnicodeString::fromUTF8(icu_56::StringPiece const&)'
    /home/user/myProject/myProject/icuTest.hpp:3: undefined reference to `icu_56::UnicodeString::~UnicodeString()'
    /home/user/myProject/myProject/icuTest.hpp:3: undefined reference to `icu_56::UnicodeString::~UnicodeString()'
    collect2: error: ld returned 1 exit status
    Build complete -- 4 errors, 0 warnings
    ---------------------- Done ----------------------
    Build: 4 errors, 0 warnings

当你运行时:

./runConfigureICU Linux --enable-static --disable-shared --disable-renaming

你注意到警告了吗?

*** WARNING: You must set the following flags before code compiled against this ICU will function properly:
    -DU_DISABLE_RENAMING=1
The recommended way to do this is to prepend the following lines to source/common/unicode/uconfig.h or #include them near the top of that file.
 /* -DU_DISABLE_RENAMING=1 */
#define U_DISABLE_RENAMING 1

如果没有,那么现在就这样做。

接下来,先修改测试程序以#include <unicode/uconfig.h>,例如

主.cpp

#include <unicode/uconfig.h>
#include <unicode/platform.h>
#include <unicode/unistr.h>
int main()
{
    icu::UnicodeString s1=UNICODE_STRING("such characters are safe 123 %-.", 32);
    (void)s1;
    return 0;
}

它将编译并链接到libicuuc.a .

如果您愿意,可以确保样板标头喜欢unicode/uconfig.h并且unicode/platform.h在编译器之前自动包含通过使用预包含标头的任何其他内容,例如

icu_preinc.h

// pre-include header for icu
#include <unicode/uconfig.h>
#include <unicode/platform.h>

您传递给 GCC 或使用选项叮当:

-include /path/to/icu_preinc.h

如果使用基于 make 的生成系统,则可以将此选项放在CPPFLAGS中。

在上面的玩具程序的情况下,只需在命令行上定义U_DISABLE_RENAMING=1就足够了,不包括<unicode/uconfig.h>

g++ -DU_DISABLE_RENAMING=1 -o prog main.cpp -L/your/libicuuc/search/path -licuuc

我注意到你的 g++ 行没有指向你说你安装了 icu 的/usr/local/lib/libicuuc.a。它显然是针对标头进行编译的,但是静态库是否存在于您正在使用的路径("/home/user/myProject/icu/libicuuc.a")中?

我认为问题出在g ++语法上:

g++ -o "/home/user/myProject/myProject/bin/Debug/myProject" "/home/user/myProject/myProject/bin/Debug/main.o" "/home/user/myProject/icu/libicuuc.a" 

你说使用对象文件 main.o 和对象文件 libicuuc.a 构建 myProject。但是,后者不是目标文件,它是一个静态库。我认为您应该将 g++ 行修改为:

g++ /home/user/myProject/myProject/bin/Debug/main.o -L/home/user/myProject/icu/ -licuuc -o /home/user/myProject/myProject/bin/Debug/myProject

-L/home/user/myProject/icu/告诉链接器在/home/user/myProject/icu/文件夹中查找库,-licuuc告诉它链接一个名为 libicuuc.a 或 libicuuc.so 的库

注意:对于ICU 67,它应该是using namespace icu_67

最新更新