c++ - 使用静态库时未定义的函数引用

标签 c++ netbeans undefined static-linking

我正在尝试使用遗传算法编写程序,这是一种特殊类型的优化算法。我为此任务找到了一个名为“Evolutionary Objects”的免费库,并实现了一个非常简单的遗传算法实例。程序代码、netbeans 实现的构建命令以及最后我收到的错误消息都在下面单独的 block 中发布。如果您允许我提供帮助,您会发现 cout 函数出了问题。当我在互联网上搜索类似的困难时,我发现人们一直在通过简单地使用 g++ 而不是 gcc 来纠正问题,但我已经在使用 g++,如您所见。任何帮助将不胜感激....

程序代码如下:

#include <stdexcept>
#include <iostream>
#include <eo>
#include <ga.h>

typedef eoBit<double> Indi;

double binary_value(const Indi & _indi)
{
  double sum = 0;
  for (unsigned i = 0; i < _indi.size(); i++)
    sum += _indi[i];
  return sum;
}

void main_function(int argc, char **argv)
{

  const unsigned int SEED = 42;      // seed for random number generator
  const unsigned int T_SIZE = 3;     // size for tournament selection
  const unsigned int VEC_SIZE = 16;   // Number of bits in genotypes
  const unsigned int POP_SIZE = 100;  // Size of population
  const unsigned int MAX_GEN = 400;  // Maximum number of generation before STOP
  const float CROSS_RATE = 0.8;      // Crossover rate
  const double P_MUT_PER_BIT = 0.01; // probability of bit-flip mutation
  const float MUT_RATE = 1.0;        // mutation rate

  rng.reseed(SEED);

  eoEvalFuncPtr<Indi> eval(  binary_value );

  eoPop<Indi> pop;

  for (unsigned int igeno=0; igeno<POP_SIZE; igeno++)
    {
      Indi v;           // void individual, to be filled
      for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)
    {
      bool r = rng.flip(); // new value, random in {0,1}
      v.push_back(r);      // append that random value to v
    }
      eval(v);                 // evaluate it
      pop.push_back(v);        // and put it in the population
    }

  pop.sort();

  cout << "Initial Population" << endl;
  cout << pop;

  eoDetTournamentSelect<Indi> select(T_SIZE);  // T_SIZE in [2,POP_SIZE]
  eo1PtBitXover<Indi> xover;
  eoBitMutation<Indi>  mutation(P_MUT_PER_BIT);

  eoGenContinue<Indi> continuator(MAX_GEN);

  eoSGA<Indi> gga(select, xover, CROSS_RATE, mutation, MUT_RATE, eval, continuator);

  gga(pop);

  pop.sort();
  cout << "FINAL Population\n" << pop << endl;
}

int main(int argc, char **argv)
{
main_function(argc, argv);
    return 1;
}
}

Netbeans 向我展示了它在尝试构建时这样做:

make[1]: Entering directory `/home/gregemerson/EO_GA/EO_GA'
rm -f -r build/Debug
rm -f dist/Debug/GNU-Linux-x86/eo_ga
make[1]: Leaving directory `/home/gregemerson/EO_GA/EO_GA'


CLEAN SUCCESSFUL (total time: 94ms)

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/gregemerson/EO_GA/EO_GA'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/eo_ga
make[2]: Entering directory `/home/gregemerson/EO_GA/EO_GA'
mkdir -p build/Debug/GNU-Linux-x86
rm -f build/Debug/GNU-Linux-x86/main.o.d
g++    -c -g -I../../EO/EO/eo/src -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp
mkdir -p dist/Debug/GNU-Linux-x86
g++     -o dist/Debug/GNU-Linux-x86/eo_ga build/Debug/GNU-Linux-x86/main.o -L../../EO/EO/eo/build/lib /home/gregemerson/EO/EO/eo/build/lib/libcma.a /home/gregemerson/EO/EO/eo/build/lib/libeoutils.a /home/gregemerson/EO/EO/eo/build/lib/libes.a /home/gregemerson/EO/EO/eo/build/lib/libga.a 

错误信息如下:

/home/gregemerson/EO_GA/EO_GA/main.cpp:94: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'
/home/gregemerson/EO_GA/EO_GA/main.cpp:99: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'
/home/gregemerson/EO_GA/EO_GA/main.cpp:149: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'
build/Debug/GNU-Linux-x86/main.o: In function `eoPop<eoBit<double> >::sortedPrintOn(std::basic_ostream<char, std::char_traits<char> >&) const':
/home/gregemerson/EO_GA/EO_GA/../../EO/EO/eo/src/eoPop.h:294: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'
build/Debug/GNU-Linux-x86/main.o: In function `std::ostream_iterator<eoBit<double>, char, std::char_traits<char> >::operator=(eoBit<double> const&)':
/usr/include/c++/4.6/bits/stream_iterator.h:198: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/GNU-Linux-x86/eo_ga] Error 1
make[2]: Leaving directory `/home/gregemerson/EO_GA/EO_GA'
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory `/home/gregemerson/EO_GA/EO_GA'
make: *** [.build-impl] Error 2

您的回答确实正确解决了我的 cout(pop) 问题。但是,我现在在我正在使用的静态库之一的 header 中看到类似的错误,以及看起来与基本 c++ 库之一相关的内容。我得到以下信息。这对您来说有意义吗?

build/Debug/GNU-Linux-x86/main.o: In function `std::ostream_iterator<eoBit<double>, char, std::char_traits<char> >::operator=(eoBit<double> const&)':
/usr/include/c++/4.6/bits/stream_iterator.h:198: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'
build/Debug/GNU-Linux-x86/main.o: In function `eoPop<eoBit<double> >::sortedPrintOn(std::basic_ostream<char, std::char_traits<char> >&) const':
/home/gregemerson/EO_GA/EO_GA/../../EO/EO/eo/src/eoPop.h:294: undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'

最佳答案

问题(例如)在这里:

cout << pop;

错误,

/home/gregemerson/EO_GA/EO_GA/main.cpp:94: undefined reference to 
`operator<<(std::basic_ostream<char, std::char_traits<char> >&, eoPrintable const&)'

意味着没有基本函数知道如何将 eoPrintable 对象推送到 ostream 中。

基于link在注释中提供,这个 eoPop 类定义了一些打印方法:sortedPrintOnprintOn .

为了使用其中之一打印到 cout,您需要执行以下操作:

pop.printOn(cout); //or pop.printOn(std::cout) if you remove using namespace std;
pop.printSortedOn(cout);

关于c++ - 使用静态库时未定义的函数引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11267073/

相关文章:

C++,Code::Blocks,我应该为 windows 和 macos 中的 GUI 选择哪种项目类型?

javascript - 如何修复 Google Chrome 中未定义的 cookie 值?

excel - 使用 UDF 进行查询在 Access 中有效,但在 Excel 中的表达式 (Err 3085) 中给出未定义函数

JavaScript 相当于 PHP 的 isset

c++ - 从 `if constexpr` 分支扩展对象生命周期/范围

c++ - C++中sizeof运算符的操作

c++ - 如何从 extern "C"函数访问类变量?

java - java版本不匹配

java - 正确安装 Java 9 和 Java 8

java - Swing 应用程序启动时运行函数(私有(private))