C++ 链接器错误:仅在优化构建时出现未定义的引用。

4

当链接优化的目标文件时,我遇到了未定义引用的链接器错误,但是链接未经优化的目标文件时却没有出现这个问题。我不明白问题出在哪里。

以下是未经优化的构建:

构建文件:../COMPASS.cpp 调用:GCC C++编译器 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” 完成构建:../COMPASS.cpp 构建文件:../PSA.cpp 调用:GCC C++编译器 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” 完成构建:../PSA.cpp 构建文件:../SAR.cpp 调用:GCC C++编译器 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” 完成构建:../SAR.cpp 构建文件:../constraints.cpp 调用:GCC C++编译器 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” 完成构建:../constraints.cpp 构建文件:../genetic.cpp 调用:GCC C++编译器 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” 完成构建:../genetic.cpp 构建文件:../globals.cpp 调用:GCC C++编译器 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” 完成构建:../globals.cpp 构建文件:../logging.cpp 调用:GCC C++编译器 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” 完成构建:../logging.cpp 构建文件:../main.cpp 调用:GCC C++编译器 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” 完成构建:../main.cpp 构建文件:../sampling.cpp 调用:GCC C++编译器 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” 完成构建:../sampling.cpp 构建文件:../simulation1.cpp 调用:GCC C++编译器 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:在全局范围内: ../globals.h:43:警告:使用但未定义的内联函数‘double showDecimals(const double&, const int&)’ 完成构建:../simulation1.cpp 构建文件:../test_function.cpp 调用:GCC C++编译器 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

这是我的优化构建版本:

构建文件: ../COMPASS.cpp 调用: GCC C++编译器 g++ -O3 -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"COMPASS.d" -MT"COMPASS.d" -o"COMPASS.o" "../COMPASS.cpp" 完成构建: ../COMPASS.cpp 构建文件: ../PSA.cpp 调用: GCC C++编译器 g++ -O3 -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"PSA.d" -MT"PSA.d" -o"PSA.o" "../PSA.cpp" 完成构建: ../PSA.cpp 构建文件: ../SAR.cpp 调用: GCC C++编译器 g++ -O3 -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"SAR.d" -MT"SAR.d" -o"SAR.o" "../SAR.cpp" 完成构建: ../SAR.cpp 构建文件: ../constraints.cpp 调用: GCC C++编译器 g++ -O3 -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"constraints.d" -MT"constraints.d" -o"constraints.o" "../constraints.cpp" 完成构建: ../constraints.cpp 构建文件: ../genetic.cpp 调用: GCC C++编译器 g++ -O3 -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"genetic.d" -MT"genetic.d" -o"genetic.o" "../genetic.cpp" 完成构建: ../genetic.cpp 构建文件: ../globals.cpp 调用: GCC C++编译器 g++ -O3 -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"globals.d" -MT"globals.d" -o"globals.o" "../globals.cpp" 完成构建: ../globals.cpp 构建文件: ../logging.cpp 调用: GCC C++编译器 g++ -O3 -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"logging.d" -MT"logging.d" -o"logging.o" "../logging.cpp" 完成构建: ../logging.cpp 构建文件: ../main.cpp 调用: GCC C++编译器 g++ -O3 -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp" 完成构建: ../main.cpp 构建文件: ../sampling.cpp 调用: GCC C++编译器 g++ -O3 -Wall -c -fmessage-length=0 -Wno-sign-compare -fopenmp -MMD -MP -MF"sampling.d" -MT"sampling.d" -o"sampling.o" "../sampling.cpp" 完成构建: ../sampling.cpp 构建文件: ../simulation1.cpp 调用: GCC C++编译器 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: 在全局作用域中: ../globals.h:43: 警告: 内联函数 'double showDecimals(const double&, const int&)' 被使用但从未定义 完成构建: ../simulation1.cpp 构建文件: ../test_function.cpp 调用: GCC C++编译器 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" 完成构建: ../test_function.cpp 构建目标: pc2 调用: GCC C++链接器 g++ -fopenmp -o"pc2" ./COMPASS.o ./PSA.o ./SAR.o ./constraints.o ./gen

有什么想法吗?

[修复了 showDecimals() 函数的问题]

文件 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";
}

你是不是没有实现 showDecimals(double const&, int const&) 函数? - Luchian Grigore
1个回答

5

1
顺便提一下,有趣的是(据我回忆),你必须小心模板特化 - 部分 模板特化(仍然依赖于模板参数)应该放在头文件中,但是 完整 的模板特化(不需要)应该放在 .cpp 文件中。 - Stuart Golodetz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接