C ++链接器无法找到对本地函数的引用

时间:2010-11-30 07:37:08

标签: c++ function linker

我将这个简单的代码插入到我的项目中的myfile.hpp文件中:

#ifndef _PERFORMANCE_INDEX_HPP_
#define _PERFORMANCE_INDEX_HPP_

#include <boost/math/special_functions/powm1.hpp>
#include <boost/math/special_functions/log1p.hpp>
#include <boost/math/special_functions/sqrt1pm1.hpp>

//-----------------------------------------------------------------------------
// Enum, struct, aliases
namespace middleware {
namespace calculus {
/*!
 * Parameters needed for calculating PI.
 */
typedef struct {
    double eval_cpu_mhz; /* CPU MHz */
    double eval_ram_mb; /* RAM in MegaBytes */
    unsigned int eval_core_num; /* Number of cores per processor */
    unsigned int eval_cpu_num; /* Number of processors */
    unsigned int eval_hops; /* INET hop distance */
    double eval_bandwidth; /* Bandwidth in KBit/s */
    unsigned int eval_load; /* Number of tasks in queue in worker */
} PerformanceEvalParams;
/*!
 * PI type.
 */
typedef double PIType;
}
}
//-----------------------------------------------------------------------------
// Constants
namespace middleware {
namespace calculus {
const double kPerformanceEvalBeta = 3.0;
const double kPerformanceEvalDelta = 1.0;
const double kPerformanceEvalG = 1.60;
const double kPerformanceEvalH = 1.014;
const double kPerformanceEvalR = 512;
}
}
//-----------------------------------------------------------------------------
// Functions declarations
namespace middleware {
namespace calculus {
/*!
 * Used to calculate log in a given base.
 */
double Log(double base, double arg);
/*!
 * Used to calculate sqrt.
 */
double Sqrt(double arg);
/*!
 * Used to calculate exp by a given base.
 */
double Exp(double base, double arg);
/*!
 * Used for calculate PI
 */
PIType CalulatePI(const PerformanceEvalParams& pep);
}
}
//-----------------------------------------------------------------------------
// Functions implementations
using namespace middleware::calculus;
PIType CalculatePI(const PerformanceEvalParams& pep) {
    double N = -1, S = -1, C = -1; /* Parts */
    double PI = -1; /* Final result */
    N = (double)(kPerformanceEvalBeta * pep.eval_bandwidth);
    N = (double)(N / (double)((kPerformanceEvalDelta * (pep.eval_hops + 1))));
    N = (double)(Sqrt(N));
    S = (double)(pep.eval_cpu_mhz * pep.eval_cpu_num);
    S = (double)(S / ((double)(pep.eval_load + 1)));
    C = Log(kPerformanceEvalG, pep.eval_core_num);
    C = (double)(C + 1);
    C = (double)((double)1 + Exp(kPerformanceEvalH, -(pep.eval_ram_mb - 512)));
    PI = (double)(N * S * C);
    return PI;
}
double Log(double base, double arg) {
    // Boost Log returns boost::math::log1p(x) = log(e, x + 1)
    double res = (double)(boost::math::log1p(arg - 1));
    // Base conversion: log(new, y) = log(old, y) / log(old, new)
    // Then ==> log(base, arg) = log(e, arg) / log(e, base)
    res = (double)(res / boost::math::log1p(base));
    return res;
}
double Sqrt(double arg) {
    // Boost Sqrt returns boost::math::sqrt1pm1(x) = sqrt(1 + x) - 1
    double res = (double)(boost::math::sqrt1pm1(arg - 1));
    res = (double)(res + 1);
    return res;
}
double Exp(double base, double arg) {
    // Boost Pow returns boost::math::powm1(x, y) = x^y - 1
    double res = (double)(boost::math::powm1(base, arg));
    res = (double)(res + 1);
    return res;
}

#endif

我使用这个编译指令进行编译:

  

g ++ * .cpp

在main.cpp中注意到#include“myfile.hpp”,所以它被编译。

g ++告诉我:

  

/tmp/ccY04BAx.o:在功能上   CalculatePI(middleware::calculus::PerformanceEvalParams const&)': main.cpp:(.text+0x1edd): undefined reference to 中间件::演算:: SQRT(双)”   main.cpp :(。text + 0x1f43):undefined   参考   middleware::calculus::Log(double, double)' main.cpp:(.text+0x1f72): undefined reference to 中间件::演算:: EXP(双,   double)'collect2:ld返回1退出   状态

有什么问题?谢谢

2 个答案:

答案 0 :(得分:4)

您的函数声明位于命名空间middleware::calculus中,您的函数 definitions 不是。

您在实现之前添加的using声明在这里没有任何帮助(顺便说一下,通常不建议在头文件中使用),当然也不意味着省略定义中的命名空间将隐含地“匹配”声明。

答案 1 :(得分:2)

using namespace middleware::calculus;
PIType CalculatePI(const PerformanceEvalParams& pep)

这不会定义您之前在CalculatePI命名空间内声明的middleware::calculus函数。这在全局命名空间中声明并定义了一个新函数。

您需要在定义中限定函数名称:

using namespace middleware::calculus;
PIType middleware::calculus::CalculatePI(const PerformanceEvalParams& pep) { 
    /* ... */ 
}

或将定义放在命名空间中:

namespace middleware {
    namespace calculus {
        PIType CalculatePI(const PerformanceEvalParams& pep) { 
            /* ... */ 
        }
    }
}