4
votes

I'm trying to create a static library that I will be using in a project. The library compiles fine and creates the *.lib file for it. To test my code, I added another project to the same solution. When I try to build the test project I get the following errors.

error LNK1120: 4 unresolved externals

error LNK2019: unresolved external symbol "public: __thiscall >matrix::~matrix(void)" (??1?$matrix@M@@QAE@XZ) referenced in function _main

error LNK2019: unresolved external symbol "public: __thiscall >matrix::matrix>(int,int)" (??0?$matrix@M@@QAE@HH@Z) referenced in function >_main C:\Users\Ryan\Documents\Spring 2013\ECE >4007\PointCloudLib\matrix_test\matrix_test.obj matrix_test

error LNK2019: unresolved external symbol "public: bool __thiscall >matrix::set(int,int,float)" (?set@?$matrix@M@@QAE_NHHM@Z) referenced in function >_main

error LNK2019: unresolved external symbol "public: static void __cdecl >matrix::print(class matrix const &)" (?print@?$matrix@M@@SAXABV1@@Z) >referenced in function _main

To use the library in my code I performed the following steps:

  1. Added a reference to the static library by going to References...>Add New Reference.. and selecting my library
  2. Added the directory where the source files to the include directories via Configuration Properties>C/C++>General>Additional Include Directories (I feel like this shouldn't be necessary since it kind of defeats the purpose of creating a library)

Those are the steps listed on Microsoft's msdn site about creating your own static libraries and it seems to be what other people are saying is the solution to the problem.

Is there something else I need to do to use the library in my project?

Also, here is the code I'm using to test the library:

#pragma once

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

#define PI 3.14
#define matrix_f matrix<float>

int main()
{
    matrix_f m(3,4);

    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 4; j++)
            m.set(i,j,PI/((i+1)*(j+1)));

    matrix_f::print(m);
}
1

1 Answers

0
votes

You need to add the reference to the library directory where the static library is to be found:

Configuration Properties->Linker->General->Additional Library Directories