Visual c++ 尝试使用二进制外部库时发生链接器错误 我是C++新手,最近刚上过一门课,把我介绍给语言,所以我理解了语法的基本知识,但是没有讨论如何使用外部库并将它们连接到我们的代码。

Visual c++ 尝试使用二进制外部库时发生链接器错误 我是C++新手,最近刚上过一门课,把我介绍给语言,所以我理解了语法的基本知识,但是没有讨论如何使用外部库并将它们连接到我们的代码。,visual-c++,Visual C++,我正在尝试使用CLP硬币库来解决线性程序从。。。 从我在那里读到的内容来看,它建议使用预编译的二进制库,而不是下载源代码,因为我在Windows7平台上,因为他们建议在Windows上重新编译有时会带来问题 我只是想让Hello World的功能发挥作用。下面是他们提供的用于测试的示例代码 * Copyright (C) 2004, International Business Machines Corporation and others. All Rights Reserved.

我正在尝试使用CLP硬币库来解决线性程序从。。。

从我在那里读到的内容来看,它建议使用预编译的二进制库,而不是下载源代码,因为我在Windows7平台上,因为他们建议在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;
}    
作为一名新手,我不知道如何解决这个问题,因为我的课程只涉及与代码语法相关的调试,而不涉及链接器问题


任何指向其他线程的提示或链接都将非常有用。我已经在谷歌上搜索了一整天,但我不知所措……

我不确定您到底做了什么来将Lib目录与项目关联起来,但您需要确保这些都完成了(第一个经常被忽略):

  • 您需要将库文件名添加到项目的“Linker/Input/Additional Dependencies”属性中

  • 您可能还需要将库的位置放置在项目的“Linker/General/Additional library Directories”属性中,具体取决于您是否为上述属性提供了正确的路径或文件名。这可能是你已经做过的


更新:

在您发布的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版本:)

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

  • 工作经历:VC 2005和VC 2008
  • 失败:VC6和VC2010

与C++ CLI和外部微小XML LIB有类似的问题,是的,我做了每一步,同时改变C++ + ADDL目录,这样它就可以找到标题。即使有3个配置设置,我仍然会显示错误。如果有其他建议,请告诉我(如果你仍然遵循这个线程)。谢谢…@JoshDailey:尝试打开VS命令窗口并运行
dumpbin/symbols coin-lib-name.lib |找到“readMps”
并发布结果(当然,用库的实际文件名替换
coin-lib name.lib
,必要时包括路径)。我想看看库中的名称与上面错误消息中的名称的比较。如何在这件事中设置一个该死的换行符?!?有两个lib文件在讨论中。第一个libClp.lib生成这些结果(天知道没有换行符的人应该怎么读):
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