2012-06-29 73 views
1

我正在嘗試使用遺傳算法編寫程序,這是一種特殊類型的優化算法。我找到了一個名爲「Evolutionary Objects」這個任務的免費庫,並實現了一個非常簡單的遺傳算法實例。程序代碼,netbeans實現的構建命令,以及我收到的錯誤消息將在以下單獨的塊中發佈。如果您允許我提供幫助,您會發現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(流行)問題是正確的。然而,我現在正在使用一個靜態庫的頭文件中看到類似的錯誤,以及看起來與某個基本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&)' 
+0

它看起來沒有像'cout'的任何錯誤 - 但嚴重的是,在標題中刪除了'using namespace std;'。如果你需要*它,把它放在源文件中,但[*從不*在頭部](http://stackoverflow.com/questions/6077566/using-namespace-std)。它看起來像'cout << pop;'是你的問題。什麼是'eoPop'? –

+0

@Sam:[eoPop < EOT > Class Template Reference](http://eodev.sourceforge.net/eo/doc/html/classeo_pop.html) – ildjarn

回答

3

的問題是,(例如)在這裏:

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,你會做以下幾點:當他們嘗試使用EO C++的lib首次

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

Hello Sam。我的建議奏效了。謝謝!但是,我發現了另外兩個類似的外觀錯誤,這些錯誤並未由您的修復程序解決,並相應地編輯了我原來的帖子你能幫助解決新問題嗎?謝謝! – gcolumbus

+0

@gcolumbus它看起來像是同一個問題,'ostream << eoPop'沒有定義,所以當庫的函數在內部試圖調用它們時,你會看到錯誤。你肯定你已經把所有的頭文件放在正確的位置,並且'#inc''d? –

+0

@SamDeHann我相信一切都是正確的順序。我三重檢查,但也許我需要離開它一段時間。但是,我不明白爲什麼我在「流迭代器」頭中看到錯誤。這是gnu C++庫的一部分,不是嗎?你能幫我理解爲什麼會這樣嗎?也許我錯誤地指責了那個標題?謝謝! – gcolumbus

1

許多國家的人民遇到此問題。我也遇到了這個問題,並能夠找到解決方案。問題在於鏈接器在構建EO程序時沒有找到必須鏈接的靜態庫。我現在能夠與EO-1.3.1上Ubunut 10.04(LTS)成功編譯和使用教程的第一篇即FirstBitGA.cpp & FirstRealGA.cpp

注:我正在寫從安裝開始的完整步驟,因此請耐心等待。用您的用戶帳戶替換「用戶名」一詞。

< <解決方案>>

  1. 下載EO庫從http://sourceforge.net/projects/eodev/files/latest/download?source=files
  2. 創建一個名爲EO;我在/ home/username /中創建它。
  3. 將EO Zip文件複製到/ home/username/EO並解壓縮。這將在/ home/username/EO下創建另一個名爲'eo'的目錄。
  4. 現在通過在終端中運行/ home/username/EO/eo/build_gcc_linux_release中的構建腳本來構建安裝文件。
  5. 步驟4中的構建腳本將在/ home/username/EO/eo下創建一個名爲'版本'的目錄。在版本目錄中,'lib'文件夾包含編譯任何EO程序時必須鏈接的庫。
  6. 現在必須向編譯器提供兩條路徑才能成功編譯:

    6.1。如果你使用的是Qt creator IDE,那麼輸入'INCLUDEPATH + =/home/username/EO/eo/src'在probpro的.pro文件中。

    6.2。與-L參數所有鏈接庫的路徑至g ++即

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

    對於Qt創建者的用戶,進入下面的行進pro文件,

    LIBS + = -L /家/用戶名/ EO/eo/release/lib/libcma.a /home/username/EO/eo/release/lib/libeo.a /home/username/EO/eo/release/lib/libeoserial.a/home/username/EO/EO/release/lib目錄/ libeoutils.a /home/username/EO/eo/release/lib/libes.a /home/username/EO/eo/release/lib/libga.a

  7. 享受

相關問題