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

我正在运行的Ubuntu,我可以build立ICU

我已经包括:

#include <unistr.h> using namespace icu; 

这是我build立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)' 

我在这里find了关于同样的问题的另一个post,但是当我按照提供的步骤,它不能解决我的问题,可能是一个不同的版本。

编辑:这是build立该项目的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 

我注意到你的g ++行不是指向/usr/local/lib/libicuuc.a,你说你安装了icu。 它显然是编译对头好,但静态库存在于你正在使用的路径(“/ home / user / myProject / icu / libicuuc.a”)?

当你跑时:

 ./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>之前,例如

main.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.hunicode/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 ++语法中的:

 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库, -licuuc告诉它链接一个名为libicuuc.a或libicuuc.so的库