0
votes

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

1
Did you link your library in VS setting?billz
@billz, yes, i added the right paths under configuration Properties->C/C++->General->Additional Include Directories.david.a.p.

1 Answers

1
votes

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")