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...

david.a.p.
  • 55
  • 1
  • 6
  • 3
    Did you link your library in VS setting? – billz Sep 25 '13 at 10:16
  • 1
    http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/12574400#12574400 – stijn Sep 25 '13 at 10:47
  • @billz, yes, i added the right paths under configuration Properties->C/C++->General->Additional Include Directories. – david.a.p. Sep 25 '13 at 10:58

1 Answers1

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")
Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
  • I tried to add the pragma directive in my test programm and i still get the same error. – david.a.p. Sep 25 '13 at 11:10
  • When i try to add the lib without it's path to the additional dependencies of the linker the file 'TestLib.lib' cannot be opened, when i add it with it's path then i get a couple of errors. – david.a.p. Sep 25 '13 at 11:16