即使设置了`-lm`,也找不到Math.h

时间:2011-10-12 12:28:34

标签: linux linker g++

即使我设置了标记math.h,gcc也没有链接到-lm的原因是什么?

me@mycomputer$ g++ template_gold.cpp -o template_gold -lm
template_gold.cpp: In function ‘void computeGold(valpoint*, centroid*, int)’:
template_gold.cpp:68: error: ‘max’ was not declared in this scope
template_gold.cpp:70: error: ‘abs’ was not declared in this scope

我很抱歉,如果这是一个骗局,但搜索谷歌,所以我发现只有帖子建议设置-lm

违规行为 代码

#include <math.h>
#include "kmeans.h"
extern "C" 
void computeGold( valpoint* h_idata, centroid* h_centroids, int numClusters);
...
for (long valindex = 0; valindex<1024*1024; valindex++)
    {
        minDistance = 0xFFFFFFFF;
        for (k = 0; k<numClusters; k++)
            if (max((long)(h_idata[valindex].value - h_centroids[k].value))<minDistance)
            {
                minDistance = abs((long)(h_idata[valindex].value - h_centroids[k].value));
                myCentroid = k;
            }

        h_idata[valindex].centroid = h_centroids[myCentroid].value;

    }
}

1 个答案:

答案 0 :(得分:3)

您可能忘记使用std::max,或者您忘记添加using namespace std。尝试

void void computeGold(valpoint* ..., centroid* ..., int ...)
{
    using namespace std; /* or using std::max etc. */
}