尝试使用二进制外部库时链接器错误

时间:2012-04-18 01:35:19

标签: visual-c++

我是C ++的新手,最近上了一堂课向我介绍了这门语言,所以我理解了语法的基础知识,但是没有讨论如何使用外部库并将它们连接到我们的代码。

我正在尝试使用CLP COIN库来解决来自...的线性程序 http://www.coin-or.org/Clp/userguide/clpuserguide.html#id4766717

从我在那里读到的,它建议使用预编译的二进制库而不是下载源,因为我在Windows 7平台上,因为他们建议在Windows上重新编译有时会引入问题。

我只是想尝试相当于Hello World的工作。以下是他们提供的用于测试的示例代码...

* Copyright (C) 2004, International Business Machines Corporation 
   and others.  All Rights Reserved.

   This sample program is designed to illustrate programming 
   techniques using CoinLP, has not been thoroughly tested
   and comes without any warranty whatsoever.

   You may copy, modify and distribute this sample program without 
   any restrictions whatsoever and without any payment to anyone.
*/

/* This shows how to provide a simple picture of a matrix.
   The default matrix will print Hello World
*/

#include "ClpSimplex.hpp"

int main (int argc, const char *argv[])
{
  ClpSimplex  model;
  int status;
  // Keep names
  if (argc<2) {
    status=model.readMps("hello.mps",true);
  } else {
    status=model.readMps(argv[1],true);
  }
  if (status)
    exit(10);

  int numberColumns = model.numberColumns();
  int numberRows = model.numberRows();

  if (numberColumns>80||numberRows>80) {
    printf("model too large\n");
    exit(11);
  }
  printf("This prints x wherever a non-zero elemnt exists in matrix\n\n\n");

  char x[81];

  int iRow;
  // get row copy
  CoinPackedMatrix rowCopy = *model.matrix();
  rowCopy.reverseOrdering();
  const int * column = rowCopy.getIndices();
  const int * rowLength = rowCopy.getVectorLengths();
  const CoinBigIndex * rowStart = rowCopy.getVectorStarts();

  x[numberColumns]='\0';
  for (iRow=0;iRow<numberRows;iRow++) {
    memset(x,' ',numberColumns);
    for (int k=rowStart[iRow];k<rowStart[iRow]+rowLength[iRow];k++) {
      int iColumn = column[k];
      x[iColumn]='x';
    }
    printf("%s\n",x);
  }
  printf("\n\n");
  return 0;
}    

我已将Include和Lib目录与我在Visual Studio中的项目相关联,但是当我尝试构建时,会出现许多链接器错误,例如:

Simplex(void)" (??1ClpSimplex@@QAE@XZ) referenced in function _main
1>hello.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall CoinPackedMatrix::~CoinPackedMatrix(void)" (??1CoinPackedMatrix@@UAE@XZ) referenced in function _main
1>hello.obj : error LNK2019: unresolved external symbol "public: void __thiscall CoinPackedMatrix::reverseOrdering(void)" (?reverseOrdering@CoinPackedMatrix@@QAEXXZ) referenced in function _main
1>hello.obj : error LNK2019: unresolved external symbol "public: __thiscall CoinPackedMatrix::CoinPackedMatrix(class CoinPackedMatrix const &)" (??0CoinPackedMatrix@@QAE@ABV0@@Z) referenced in function _main
1>hello.obj : error LNK2019: unresolved external symbol "public: int __thiscall ClpSimplex::readMps(char const *,bool,bool)" (?readMps@ClpSimplex@@QAEHPBD_N1@Z) referenced in function _main
1>hello.obj : error LNK2019: unresolved external symbol "public: __thiscall ClpSimplex::ClpSimplex(bool)" (??0ClpSimplex@@QAE@_N@Z) referenced in function _main

作为一个新手,我对如何解决这个问题毫无头绪,因为我的课程只涉及与代码语法相关的调试,而不是链接器问题。

任何提示或链接到其他线程都会有很大帮助。我一整天都在谷歌搜索,但我不知所措......

1 个答案:

答案 0 :(得分:0)

我不确定您将Lib目录与项目关联的确切做法,但您需要确保完成这些(第一个经常被忽略):

  • 您需要将库文件名添加到项目的“链接器/输入/附加依赖项”属性中。

  • 您可能还需要将库的位置放在项目的“链接器/常规/附加库目录”属性中 - 具体取决于您是否提供正确的路径或仅提供上述属性的文件名。这可能就是你已经做过的事情。


更新

在您发布的dumpbin输出中,您将看到以下条目:

4C3 0000AE4A SECT98 notype () External | ?readMps@ClpSimplex@@QEAAHPEBD_N 1@Z (public: int __cdecl ClpSimplex::readMps(char const *,bool,bool)) 

在错误消息中,您会看到:

1>hello.obj : error LNK2019: unresolved external symbol "public: int __thiscall ClpSimplex::readMps(char const *,bool,bool)" (?readMps@ClpSimplex@@QAEHPBD_N1@Z) referenced in function _main

比较两条消息中的去除名称:

public: int __cdecl ClpSimplex::readMps(char const *,bool,bool)     // what's in the .lib
public: int __thiscall ClpSimplex::readMps(char const *,bool,bool)  // what hello.obj is asking for

你会看到两个调用约定是不同的。看看coin-or.org网站,看起来这个库是用VC 2005构建的。对于Microsoft编译器,这些库不能总是与不同的编译器版本一起使用。如果你想使用这个预建的库,我建议你使用VC 2005(你仍然可以在这里获得免费的Express版本:http://go.microsoft.com/fwlink/?linkid=57034)。

FWIW,我尝试使用多个MS编译器编译示例并得到以下结果(仅构建 - 当我尝试运行时程序抱怨某些文件丢失:

  • 工作:VC 2005和VC 2008
  • 失败:VC 6和VC2010