5

我只是有一个问题,我一直在尝试解决最长时间。

我有一个 Visual c++ 中的静态库项目,我希望另一个项目能够链接到它。到目前为止,我只是添加了对静态库项目的引用,它会自动链接库。

我希望能够仅使用头文件和 .lib 文件链接到库。但是,我收到“未解析的外部符号”错误。

我认为我做得对 - 我指定了包含目录、库目录,并进入链接器输入属性并将 lib 作为附加依赖项提供。

我可以通过这种方式引用其他静态库(如 SDL),那么为什么在尝试引用我的库时会出错?

谢谢您的帮助。

问题是它没有引用实际的 lib 文件,还是 lib 本身的问题?这些是我收到的错误消息:

错误 2 错误 LNK2019:无法解析的外部符号“public: void __thiscall XEngine::XCore::XScreen::init(class XEngine::XCore::XGame &)”(?init@XScreen@XCore@XEngine@@QAEXAAVXGame@23@ @Z) 在函数“void __cdecl XEngine::XEngineInit(class XEngine::XCore::XScreen &,class XEngine::XCore::XGame &)”中引用 (?XEngineInit@XEngine@@YAXAAVXScreen@XCore@1@AAVXGame@ 31@@Z) C:\Users\Xander Masotto\Documents\Visual Studio 2010\Projects\Pong\Pong\source.obj Pong

错误 3 错误 LNK2019:未解析的外部符号“public: __thiscall XEngine::XCore::XScreen::~XScreen(void)”(??1XScreen@XCore@XEngine@@QAE@XZ) 在函数“void __cdecl XEngine:: XEngineInit(class XEngine::XCore::XGame &)" (?XEngineInit@XEngine@@YAXAAVXGame@XCore@1@@Z) C:\Users\Xander Masotto\Documents\Visual Studio 2010\Projects\Pong\Pong\source .obj 乒乓

错误 4 错误 LNK2019:未解析的外部符号“public: __thiscall XEngine::XCore::XScreen::XScreen(void)”(??0XScreen@XCore@XEngine@@QAE@XZ) 在函数“void __cdecl XEngine::XEngineInit”中引用(class XEngine::XCore::XGame &)" (?XEngineInit@XEngine@@YAXAAVXGame@XCore@1@@Z) C:\Users\Xander Masotto\Documents\Visual Studio 2010\Projects\Pong\Pong\source.对象乒乓

4

1 回答 1

4

确保您正在导出库中您希望向其他应用程序(即您的 dll 或 exe)公开的函数、类和变量。默认情况下,它们不公开。

当您为您的库创建项目时,通常会安排执行此操作的基础工作。

#ifdef TESTLIB_EXPORTS
#define TESTLIB_API __declspec(dllexport)
#else
#define TESTLIB_API __declspec(dllimport)
#endif

使用上面在项目创建期间生成的代码,我只需要做两件事来公开函数、类或变量:

1)确保我已将 TESTLIB_EXPORTS 定义为预处理器。项目设置:C++/Preprocessor/PreprocessorDefinitions

2) 在我想公开的每个函数、类或变量上使用 TESTLIB_API 定义:

class TESTLIB_API Order {
    void doSomething();
};
于 2010-09-09T22:46:47.247 回答