包括 header 静态库给出 dllimport 错误
included header for static library giving dllimport error
我有稍后使用 Xerces-c 提供的代码,它可以构建为静态或动态库。未能包含当然会导致编译器错误,但是当我添加 #include <xercesc/util/PlatformUtils.hpp>
visual studio 2012 时,我会收到 linker 错误提示:
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static void __cdecl xercesc_3_1::XMLPlatformUtils::Initialize(char const * const,char const * const,class xercesc_3_1::PanicHandler * const,class xercesc_3_1::MemoryManager * const)" (__imp_?Initialize@XMLPlatformUtils@xercesc_3_1@@SAXQBD0QAVPanicHandler@2@QAVMemoryManager@2@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static void __cdecl xercesc_3_1::XMLPlatformUtils::Terminate(void)" (__imp_?Terminate@XMLPlatformUtils@xercesc_3_1@@SAXXZ) referenced in function __catch$_main[=11=]
1>main.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static char const * const xercesc_3_1::XMLUni::fgXercescDefaultLocale" (__imp_?fgXercescDefaultLocale@XMLUni@xercesc_3_1@@2QBDB)
根据错误的 dllimport
部分,它似乎无法找到 dll。当我将 Xerces-c 构建为动态库并向其 link 时,错误消失了,这证实了这一点。但是,如果我将 Xerces-c 构建为静态库并将 link 构建为它,同样的错误仍然存在。所以我的问题是,为什么当我包含并 linking 到静态库时,请求 dll 时出现错误?
using namespace xercesc;
int main(int argc, char* argv[])
{
std::ifstream inputFile(argv[1]);
char c = inputFile.get();
while (inputFile.good()) {
std::cout << c;
c = inputFile.get();
}
try {
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch) {
// Do your failure processing here
return 1;
}
// Do your actual work with Xerces-C++ here.
//XercesDOMParser parser;
//parser.useScanner(XMLUni::fgDGXMLScanner);
XMLPlatformUtils::Terminate();
// Other terminations and cleanup.
return 0;
}
您需要使用 XERCES_STATIC_LIBRARY
预处理器宏编译您的应用程序以禁用 Xerces 库的 DLL import/export。
还要检查您 link 是否针对 .lib
文件的静态版本。
我有稍后使用 Xerces-c 提供的代码,它可以构建为静态或动态库。未能包含当然会导致编译器错误,但是当我添加 #include <xercesc/util/PlatformUtils.hpp>
visual studio 2012 时,我会收到 linker 错误提示:
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static void __cdecl xercesc_3_1::XMLPlatformUtils::Initialize(char const * const,char const * const,class xercesc_3_1::PanicHandler * const,class xercesc_3_1::MemoryManager * const)" (__imp_?Initialize@XMLPlatformUtils@xercesc_3_1@@SAXQBD0QAVPanicHandler@2@QAVMemoryManager@2@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static void __cdecl xercesc_3_1::XMLPlatformUtils::Terminate(void)" (__imp_?Terminate@XMLPlatformUtils@xercesc_3_1@@SAXXZ) referenced in function __catch$_main[=11=]
1>main.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static char const * const xercesc_3_1::XMLUni::fgXercescDefaultLocale" (__imp_?fgXercescDefaultLocale@XMLUni@xercesc_3_1@@2QBDB)
根据错误的 dllimport
部分,它似乎无法找到 dll。当我将 Xerces-c 构建为动态库并向其 link 时,错误消失了,这证实了这一点。但是,如果我将 Xerces-c 构建为静态库并将 link 构建为它,同样的错误仍然存在。所以我的问题是,为什么当我包含并 linking 到静态库时,请求 dll 时出现错误?
using namespace xercesc;
int main(int argc, char* argv[])
{
std::ifstream inputFile(argv[1]);
char c = inputFile.get();
while (inputFile.good()) {
std::cout << c;
c = inputFile.get();
}
try {
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch) {
// Do your failure processing here
return 1;
}
// Do your actual work with Xerces-C++ here.
//XercesDOMParser parser;
//parser.useScanner(XMLUni::fgDGXMLScanner);
XMLPlatformUtils::Terminate();
// Other terminations and cleanup.
return 0;
}
您需要使用 XERCES_STATIC_LIBRARY
预处理器宏编译您的应用程序以禁用 Xerces 库的 DLL import/export。
还要检查您 link 是否针对 .lib
文件的静态版本。