【问题标题】:Math.h not found even though `-lm` is set即使设置了“-lm”,也找不到 Math.h
【发布时间】:2011-12-06 02:32:24
【问题描述】:

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

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;

    }
}

【问题讨论】:

  • 在这段代码中,您似乎使用的是 -Im 而不是 -lm ?应该是小写的L
  • 为什么你在命令行中有-Im而不是-lm? (并不是说这与您的编译错误有关)。
  • 您需要在第 65 - 75 行附近发布代码,并显示您拥有的#includes
  • 感谢您的快速回复,但那是我输入的,我更正了。 (我什至试过 -Im,以防我误读了帖子......)

标签: linux linker g++


【解决方案1】:

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

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

【讨论】:

  • @Framester 老实说,这并没有那么令人反感,尽管您应该包含 cmath 而不是 math.h
  • 我收到错误消息template_gold.cpp:69: error: ‘abs’ is not a member of ‘std’
  • @Framester 你include &lt;cmath&gt;了吗?
猜你喜欢
  • 2013-11-02
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多