C ++链接器错误:未定义的引用仅在优化的版本上 [英] c++ linker error: undefined references only on optimized build

查看:66
本文介绍了C ++链接器错误:未定义的引用仅在优化的版本上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

链接优化对象文件时,出现的未定义引用链接器错误,链接未优化对象文件时不是.我不明白问题是什么或可能是什么.

I'm getting an undefined references linker error only when linking optimized objects files, not when linking unoptimized object files. I don't understand what the problem is or could be.

这是我未优化的版本:


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'

有什么想法吗?

[带有showDecimals()函数的固定问题]

COMPASS.h文件包含此类模板:

The file COMPASS.h includes this class template:

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包含以下代码:

And COMPASS.cpp include the following code:

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

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

The logging.cpp files includes the following code:

#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";
}

推荐答案

int VisitedSet<T>::getSize()是模板函数,因此其定义应位于COMPASS.h之类的头文件中,而不应位于COMPASS.cpp中.

int VisitedSet<T>::getSize() is a templated function, so its definition should be in a header file like COMPASS.h, not in COMPASS.cpp.

原因如下: http://www.parashift.com /c++-faq/templates-defn-vs-decl.html

这篇关于C ++链接器错误:未定义的引用仅在优化的版本上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆