首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用静态库时对函数的引用未定义

使用静态库时对函数的引用未定义
EN

Stack Overflow用户
提问于 2012-06-30 02:27:32
回答 2查看 1K关注 0票数 1

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

程序代码紧跟在下面:

代码语言:javascript
运行
复制
#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向我展示了它在尝试构建时正在做的事情:

代码语言:javascript
运行
复制
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 

错误消息紧跟在下面:

代码语言:javascript
运行
复制
/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)问题。但是,我现在在我正在使用的一个静态库的头文件中看到了类似的错误,以及看起来与一个基本c++库相关的头文件。我得到了以下结论。这对你来说有意义吗?

代码语言:javascript
运行
复制
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&)'
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-30 02:40:29

问题是(例如)这里:

代码语言:javascript
运行
复制
cout << pop;

错误,

代码语言:javascript
运行
复制
/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,您需要执行以下操作:

代码语言:javascript
运行
复制
pop.printOn(cout); //or pop.printOn(std::cout) if you remove using namespace std;
pop.printSortedOn(cout);
票数 3
EN

Stack Overflow用户

发布于 2015-06-02 17:20:26

许多人在第一次尝试使用EO C++库时都遇到了这个问题。我也遇到了这个问题,并找到了解决方案。问题是链接器在构建EO程序时找不到必须链接的静态库。我现在能够在Ubunut10.04 (LTS)上成功地编译和使用第一个教程,即FirstBitGA.cpp & FirstRealGA.cppEO-1.3.1

注意:我正在编写从安装开始的完整步骤,请耐心等待。同时将“username”替换为您的用户帐户。

<< Solution >>

主目录从http://sourceforge.net/projects/eodev/files/latest/download?source=files

  • Create下载EO,一个名为EO的目录;我在//
  1. /中创建它。
  2. 将EO Zip文件复制到/home/用户名/EO并解压缩。这将在terminal.
  3. The构建脚本中的/home/username/EO/eo / build _ /home/username/EO.
  4. Now _linux_release中运行构建脚本,在/home/用户名/eo/eo/eo下创建另一个名为'eo‘的目录。在step-4中的terminal.
  5. The构建脚本中,将在/home/用户名/eo/eo下创建一个名为'release’的目录。在发布目录中,'lib‘文件夹包含编译任何EO程序时必须链接的库。
  6. 现在必须向编译器提供两个路径才能成功编译:

6.1。将文件作为g++的命令行参数包含的路径,即-I /home/用户名/eo/eo/src < OR >如果您使用的是Qt creator IDE,则在probject的.pro文件中输入行‘INCLUDEPATH += /home/username/EO/eo/src’。

6.2。带有g++的-L参数的所有链接库的路径,即

-L/home/用户名/eo/release/lib/libcma.a/home/用户名/eo/eo/release/lib/libeo.a /home/username/EO/eo/release/lib/libeoserial/home/username/EO/eo/release/lib/libeoutils.a /home/用户名/eo/eo/release/lib/libes.a/home/用户名/eo/release/lib/libga.a

对于Qt Creator用户,在.pro文件中输入以下行:

/home/username/EO/eo/release/lib/libga.a

  • Enjoy

+= -L/home/用户名/eo/eo/release/lib/libcma.a/home/用户名/eo/release/lib/libeo.a /home/username/EO/eo/release/lib/libeoserial.a /home/用户名/eo/eo/release/lib/libeoutils.a/home/用户名/eo/eo/release/lib/libes.a LIBS

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11267073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档