我用下面的头文件和源文件编写了我自己的小静态库
测试库.h
#include <iostream>
class TestLib
{
public:
static void HelloTest();
};
测试库.cpp
#include "TestLib.h"
void TestLib::HelloTest()
{
std::cout << "Hello World this is my .lib!";
}
当我构建库并将创建的库包含在新项目中并尝试像这样使用它时:
#include "stdafx.h"
#include <iostream>
#include <TestLib.h>
int _tmain(int argc, _TCHAR* argv[])
{
TestLib::HelloTest();
int i;
std::cin >> i;
return 0;
}
我刚在 vs2012 中得到以下错误:
1>TestLib_VS2012.obj : error LNK2019: unresolved external symbol "public: static void __cdecl TestLib::HelloTest(void)" (?HelloTest@TestLib@@SAXXZ) referenced in function _wmain
1>C:\Users\DavidP\Desktop\PROG\TestLib_VS2012\Debug\TestLib_VS2012.exe : fatal error LNK1120: 1 unresolved externals
编辑: 在遵循 Marius Bancila 的提示和 stijns 链接并添加 lib 及其路径到项目设置中的链接器设置并将 vs 设置为 Release模式后,我收到以下错误:
1>TestLib_VS2012.obj : error LNK2001: unresolved external symbol "public: static void __cdecl TestLib::HelloTest(void)" (?HelloTest@TestLib@@SAXXZ)
1>C:\Users\DavidP\Desktop\PROG\TestLib_VS2012\Release\TestLib_VS2012.exe : fatal error LNK1120: 1 unresolved externals
编辑2: 在将 visual studio 设置为 Release模式后,我在其中编译了我的 lib 文件并设置了“其他包含目录”、“其他库目录”和“其他依赖项”,它可以正常工作。多谢。我现在觉得很愚蠢......
最佳答案
您有两个选择:将库添加到 Project properties > Configuration Properties > Linker > Input > Additional dependencies 或为此使用 #pragma 指令。
#pragma comment(lib, "testlib.lib")
关于c++ - VS2012自备静态库抛链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19002097/