我在我的项目中的 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: In function
CalculatePI(middleware::calculus::PerformanceEvalParams const&)': main.cpp:(.text+0x1edd): undefined reference to
middleware::calculus::Sqrt(double)' main.cpp:(.text+0x1f43): undefined reference tomiddleware::calculus::Log(double, double)' main.cpp:(.text+0x1f72): undefined reference to
middleware::calculus::Exp(double, double)' collect2: ld returned 1 exit status
有什么问题?谢谢
最佳答案
您的函数声明在命名空间middleware::calculus
中,您的函数定义不在。
您在实现之前添加的 using
声明在这里没有任何帮助(顺便说一句,通常不建议在头文件中使用),当然并不意味着省略 namespace 在定义中将隐式“匹配”声明。
关于C++ 链接器找不到对局部函数的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4311503/