Adding a static library to a project in the same solution (Visual Studio 2012)

故事扮演 提交于 2019-12-12 12:19:26

问题


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:


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



来源:https://stackoverflow.com/questions/15725483/adding-a-static-library-to-a-project-in-the-same-solution-visual-studio-2012

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!