0

i've written my own little static library with the following header and source file

TestLib.h

#include <iostream>

class TestLib
{
public:
  static void HelloTest();
};

TestLib.cpp

#include "TestLib.h"

void TestLib::HelloTest()
{
  std::cout << "Hello World this is my .lib!";
}

When i build the library and include the created lib in a new project and try to use it like this:

#include "stdafx.h"
#include <iostream>

#include <TestLib.h>

int _tmain(int argc, _TCHAR* argv[])
{
  TestLib::HelloTest();

  int i;
  std::cin >> i;

  return 0;
}

I just get the following error in 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

Edit: After following Marius Bancila's tip and stijns link and adding the lib and it's path to the linker settings in the project settings and setting vs to release mode i get the following error:

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

Edit2: After setting visual studio into release mode in which i compiled my lib file and setting the 'additional incude directories', 'additional library directories' and the 'additional dependencies' it works. Thanks a lot. I feel so dumb right now...

4

1 回答 1

1

You have two options: either add the library to Project properties > Configuration Properties > Linker > Input > Additional dependencies or use a #pragma directive for that.

#pragma comment(lib, "testlib.lib")
于 2013-09-25T10:50:45.783 回答