当符号明确存在于我链接的静态库中时,为什么会出现链接错误? [英] Why do I get link errors when the symbol is clearly present in the static library I link against?
问题描述
我已经决定开始在我的项目中使用ICU,但遗憾的是VS2015正式不受支持(即,开发人员将不兼容列为 http://www.npcglib.org/~stathis/blog/precompiled-icu/.我已经下载了ICU 56 VS15软件包,并尝试编译以下简单程序:
I've decided to start using ICU in my project, but sadly VS2015 is officially unsupported (i.e. the developers list the incompatibility as a known issue). Luckily someone was kind enough to fix the problems on their own and share the binaries with the world: http://www.npcglib.org/~stathis/blog/precompiled-icu/ . I've downloaded the ICU 56 VS15 package and I'm trying to compile the following simple program:
#include <iostream>
#include <unicode\unistr.h>
int main()
{
auto myStr = icu::UnicodeString::fromUTF8(u8"кошка");
std::cout << "length: " << myStr.length() << std::endl;
std::getchar();
return 0;
}
当我使用编译时导入.lib
库时,一切正常,但是我在运行时当然需要DLL库.当我针对静态代码sicuxxx.lib
进行编译时,由于某种原因(有六个类似的代码,为简洁起见,省略了它们)我得到了链接错误:
When I use the compile time import .lib
libraries, everything works fine, but of course I need the DLL libraries at runtime. When I compile against the static ones, sicuxxx.lib
, I get link errors for some reason (there are six similar ones, omitted for brevity):
Error LNK2001 unresolved external symbol "__declspec(dllimport) private: int __thiscall icu_56::UnicodeString::getShortLength(void)const " (__imp_?getShortLength@UnicodeString@icu_56@@ABEHXZ)
我做了dumpbin /symbols sicuuc.lib
,在输出中我可以看到以下内容:
I did dumpbin /symbols sicuuc.lib
, and in the output I can see the following:
0E8 00000000 SECT47 notype () External | ?getShortLength@UnicodeString@icu_56@@ABEHXZ (private: int __thiscall icu_56::UnicodeString::getShortLength(void)const )
我的链接器输入如下:kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;sicudt.lib;sicuin.lib;sicuio.lib;sicule.lib;siculx.lib;sicutest.lib;sicutu.lib;sicuuc.lib;
.链接程序可以清楚地打开所有文件,因为不会出现无法打开文件"错误.
My linker input is as follows: kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;sicudt.lib;sicuin.lib;sicuio.lib;sicule.lib;siculx.lib;sicutest.lib;sicutu.lib;sicuuc.lib;
. All the files can clearly be opened by the linker because no "cannot open file" error appears.
由于dumpbin所示的库中清楚地存在该符号,所以lib.exe为什么在查找它时会遇到问题?
推荐答案
在编译代码时,需要定义U_STATIC_IMPLEMENTATION
,以免将ICU功能标记为导入.
You need to define U_STATIC_IMPLEMENTATION
when compiling your code so that the ICU functions are not marked as imports.
(您可以看到它当前正在尝试查找的功能被标记为__declspec(dllimport)
,与静态库中的功能不匹配.)
(You can see that the function it is currently trying to find is marked as __declspec(dllimport)
, which doesn't match the one in the static library.)
这篇关于当符号明确存在于我链接的静态库中时,为什么会出现链接错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!