2012-09-03 34 views
3

我得到未定義的參考鏈接器錯誤只有鏈接優化的對象文件時,而不是鏈接未優化的對象文件時。我不明白問題是或可能是什麼。C++鏈接器錯誤:未定義的參考僅在優化的版本

這是我沒有優化的編譯:

 
Building file: ../COMPASS.cpp 
Invoking: GCC C++ Compiler 
g++ -O0 -g3 -pg -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"COMPASS.d" -MT"COMPASS.d" -o"COMPASS.o" "../COMPASS.cpp" 
Finished building: ../COMPASS.cpp 

Building file: ../PSA.cpp 
Invoking: GCC C++ Compiler 
g++ -O0 -g3 -pg -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"PSA.d" -MT"PSA.d" -o"PSA.o" "../PSA.cpp" 
Finished building: ../PSA.cpp 

Building file: ../SAR.cpp 
Invoking: GCC C++ Compiler 
g++ -O0 -g3 -pg -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"SAR.d" -MT"SAR.d" -o"SAR.o" "../SAR.cpp" 
Finished building: ../SAR.cpp 

Building file: ../constraints.cpp 
Invoking: GCC C++ Compiler 
g++ -O0 -g3 -pg -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"constraints.d" -MT"constraints.d" -o"constraints.o" "../constraints.cpp" 
Finished building: ../constraints.cpp 

Building file: ../genetic.cpp 
Invoking: GCC C++ Compiler 
g++ -O0 -g3 -pg -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"genetic.d" -MT"genetic.d" -o"genetic.o" "../genetic.cpp" 
Finished building: ../genetic.cpp 

Building file: ../globals.cpp 
Invoking: GCC C++ Compiler 
g++ -O0 -g3 -pg -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"globals.d" -MT"globals.d" -o"globals.o" "../globals.cpp" 
Finished building: ../globals.cpp 

Building file: ../logging.cpp 
Invoking: GCC C++ Compiler 
g++ -O0 -g3 -pg -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"logging.d" -MT"logging.d" -o"logging.o" "../logging.cpp" 
Finished building: ../logging.cpp 

Building file: ../main.cpp 
Invoking: GCC C++ Compiler 
g++ -O0 -g3 -pg -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp" 
Finished building: ../main.cpp 

Building file: ../sampling.cpp 
Invoking: GCC C++ Compiler 
g++ -O0 -g3 -pg -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"sampling.d" -MT"sampling.d" -o"sampling.o" "../sampling.cpp" 
Finished building: ../sampling.cpp 

Building file: ../simulation1.cpp 
Invoking: GCC C++ Compiler 
g++ -O0 -g3 -pg -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"simulation1.d" -MT"simulation1.d" -o"simulation1.o" "../simulation1.cpp" 
../globals.h: At global scope: 
../globals.h:43: warning: inline function 'double showDecimals(const double&, const int&)' used but never defined 
Finished building: ../simulation1.cpp 

Building file: ../test_function.cpp 
Invoking: GCC C++ Compiler 
g++ -O0 -g3 -pg -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"test_function.d" -MT"test_function.d" -o"test_function.o" "../test_function.cpp" 
Finished building: ../test_function.cpp 

Building target: pc2 
Invoking: GCC C++ Linker 
g++ -pg -fopenmp -pg -o"pc2" ./COMPASS.o ./PSA.o ./SAR.o ./constraints.o ./genetic.o ./globals.o ./logging.o ./main.o ./sampling.o ./simulation1.o ./test_function.o 
Finished building target: pc2 

這裏是我的優化編譯:

 
Building file: ../COMPASS.cpp 
Invoking: GCC C++ Compiler 
g++ -O3 -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"COMPASS.d" -MT"COMPASS.d" -o"COMPASS.o" "../COMPASS.cpp" 
Finished building: ../COMPASS.cpp 

Building file: ../PSA.cpp 
Invoking: GCC C++ Compiler 
g++ -O3 -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"PSA.d" -MT"PSA.d" -o"PSA.o" "../PSA.cpp" 
Finished building: ../PSA.cpp 

Building file: ../SAR.cpp 
Invoking: GCC C++ Compiler 
g++ -O3 -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"SAR.d" -MT"SAR.d" -o"SAR.o" "../SAR.cpp" 
Finished building: ../SAR.cpp 

Building file: ../constraints.cpp 
Invoking: GCC C++ Compiler 
g++ -O3 -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"constraints.d" -MT"constraints.d" -o"constraints.o" "../constraints.cpp" 
Finished building: ../constraints.cpp 

Building file: ../genetic.cpp 
Invoking: GCC C++ Compiler 
g++ -O3 -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"genetic.d" -MT"genetic.d" -o"genetic.o" "../genetic.cpp" 
Finished building: ../genetic.cpp 

Building file: ../globals.cpp 
Invoking: GCC C++ Compiler 
g++ -O3 -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"globals.d" -MT"globals.d" -o"globals.o" "../globals.cpp" 
Finished building: ../globals.cpp 

Building file: ../logging.cpp 
Invoking: GCC C++ Compiler 
g++ -O3 -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"logging.d" -MT"logging.d" -o"logging.o" "../logging.cpp" 
Finished building: ../logging.cpp 

Building file: ../main.cpp 
Invoking: GCC C++ Compiler 
g++ -O3 -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp" 
Finished building: ../main.cpp 

Building file: ../sampling.cpp 
Invoking: GCC C++ Compiler 
g++ -O3 -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"sampling.d" -MT"sampling.d" -o"sampling.o" "../sampling.cpp" 
Finished building: ../sampling.cpp 

Building file: ../simulation1.cpp 
Invoking: GCC C++ Compiler 
g++ -O3 -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"simulation1.d" -MT"simulation1.d" -o"simulation1.o" "../simulation1.cpp" 
../globals.h: At global scope: 
../globals.h:43: warning: inline function 'double showDecimals(const double&, const int&)' used but never defined 
Finished building: ../simulation1.cpp 

Building file: ../test_function.cpp 
Invoking: GCC C++ Compiler 
g++ -O3 -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"test_function.d" -MT"test_function.d" -o"test_function.o" "../test_function.cpp" 
Finished building: ../test_function.cpp 

Building target: pc2 
Invoking: GCC C++ Linker 
g++ -fopenmp -o"pc2" ./COMPASS.o ./PSA.o ./SAR.o ./constraints.o ./genetic.o ./globals.o ./logging.o ./main.o ./sampling.o ./simulation1.o ./test_function.o 
./logging.o: In function `LOG_COMPASS_display_points_MATLAB(int const&, VisitedSet&)': 
logging.cpp:(.text+0x686): undefined reference to `VisitedSet::getSize()' 
collect2: ld returned 1 exit status 
make: *** [pc2] Error 1 
make: Leaving directory `/home/djunderw/ncsu/workspace/pc2/release' 

任何想法?

[WITH showDecimals固定的問題()FUNCTION]

文件COMPASS.h包括這個類的模板:

template<class T> class VisitedSet { 
public: 
    VisitedSet(const T& soln); 
    int getSize(); 
    void addSolution(const T& soln); 
    void evaluate(); 
    void sample(const int& numNewSolutions, Constraints& space); 
    void CSBiased_generateDistribution(); 
    void constructMPA(Constraints& space); 
    vector<T> vec; 
    double bestValue; 
    int bestIndex; 

    /* used to implement biased coordinate sampling */ 
    double CSBiased_coefficient; 
    vector< vector<double> > lastSampledPoints; 
    vector<int> lastSampledPointsIndex; 
    vector< vector<double> > newSampledPoints; 
    vector<int> newSampledPointsIndex; 
    vector<double> CSBiased_distribution; 

private: 
    int iteration; 
    int dimension; 
    int SAR(); // simulation allocation rule 
}; 

而且COMPASS.cpp包括以下代碼:

template<class T> 
int VisitedSet<T>::getSize() { 
    return vec.size(); 
} 

logging.cpp文件包含以下代碼:

#include "COMPASS.h" 
void LOG_COMPASS_display_points_MATLAB(const int& iteration, VisitedSet<Policy>& visited) { 
    cout << "points{"<<iteration+1<<"} = [\n" 
     << visited.vec[visited.bestIndex].x[0] << "\t" <<  visited.vec[visited.bestIndex].x[1] << "\t" << visited.vec[visited.bestIndex].meanQALY() << ";" << endl; 
for(int i=0; i<visited.getSize(); i++) { 
    if(i != visited.bestIndex) 
     cout << visited.vec[i].x[0] << "\t" << visited.vec[i].x[1] << "\t" << visited.vec[i].meanQALY() << ";" << endl; 
} 
cout << "];\n"; 
} 
+1

難道在你的匆忙,你忘了準備最小的情況下,並張貼一些代碼? ;-) –

+0

顯示代碼。可疑的是,您在日誌中的早些時候收到了有關'showDecimals'的警告,然後得到了一個稍後提及的鏈接器錯誤,以瞭解它的價值。 –

+0

難道你沒有實現'showDecimals(double const&,int const&)'? –

回答

3

int VisitedSet<T>::getSize()是一個模板化函數,所以它的定義應該在像COMPASS.h這樣的頭文件中,而不是在COMPASS.cpp中。

這裏的原因:http://www.parashift.com/c++-faq/templates-defn-vs-decl.html

+0

哇,非常感謝!我從來沒有聽說過... – synaptik

+1

作爲一個觀點,有趣的是(從我記得的)你必須小心模板專業化 - *部分*模板專業化(仍然取決於模板參數)應該去一個頭,但*完整*模板專業化(不)應該在'.cpp'文件。 –