【问题标题】:Kg to pounds converter build error (CLion)公斤到磅转换器构建错误 (CLion)
【发布时间】:2015-05-27 12:05:00
【问题描述】:

我刚刚通过阅读“C++ Primer Plus 5th add”一书开始学习 C++ 编程语言,但我遇到了一个问题。这本书刚开始详细介绍函数、函数原型、函数头等。我决定尝试制作一个 KG --> 磅转换器作为练习,但我的问题是当我尝试构建它时(我使用 CLion ) 我得到一个构建错误。

代码:

#include <cmath> // [EDIT] Removed this line as it isnt being used
#include <iostream>
using namespace std;

double pounds_converter(double);
int main()
{
    cout << "Welcome to the kg to pounds convertor" << endl;
    cout << "Enter the desired kg's to change to pounds: ";
    double my_kg;
    cin >> my_kg;
    double pounds = pounds_converter(my_kg);
    cout << my_kg << " in pounds is: " << pounds << endl;
    cin.get();
    return 0;
}

double pounds_converter(double n)
{
    return 2.2046226218 * n;

}

首次构建错误:

"C:\Program Files (x86)\JetBrains\CLion 1.0.3\bin\cmake\bin\cmake.exe"
--build C:\Users\Admin\.clion10\system\cmake\generated\23677da2\23677da2\Release
--target newproject -- -j 8 Scanning dependencies of target newproject [100%] Building CXX object CMakeFiles/newproject.dir/main.cpp.obj In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\cmath:44:0,
                 from C:\Users\Admin\ClionProjects\newproject\main.cpp:1: c:\mingw\include\math.h: In function 'float hypotf(float, float)': c:\mingw\include\math.h:635:30: error: '_hypot' was not declared in this scope  { return (float)(_hypot (x, y)); }
                              ^ mingw32-make.exe[3]: *** [CMakeFiles/newproject.dir/main.cpp.obj] Error 1 mingw32-make.exe[2]:
*** [CMakeFiles/newproject.dir/all] Error 2 mingw32-make.exe[1]: *** [CMakeFiles/newproject.dir/rule] Error 2 mingw32-make.exe: *** [newproject] Error 2 CMakeFiles\newproject.dir\build.make:53: recipe for target 'CMakeFiles/newproject.dir/main.cpp.obj' failed CMakeFiles\Makefile2:59: recipe for target 'CMakeFiles/newproject.dir/all' failed CMakeFiles\Makefile2:71: recipe for target 'CMakeFiles/newproject.dir/rule' failed Makefile:108: recipe for target 'newproject' failed

删除#include cmath 行后,因为它不需要也没有在我的程序中使用,我尝试再次构建它,但收到了一个不同的错误:

"C:\Program Files (x86)\JetBrains\CLion 1.0.3\bin\cmake\bin\cmake.exe" --build C:\Users\Admin\.clion10\system\cmake\generated\23677da2\23677da2\Release --target newproject -- -j 8
Linking CXX executable newproject.exe
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: cannot open output file newproject.exe: Permission denied
collect2.exe: error: ld returned 1 exit status
CMakeFiles\newproject.dir\build.make:86: recipe for target 'newproject.exe' failed
CMakeFiles\Makefile2:59: recipe for target 'CMakeFiles/newproject.dir/all' failed
CMakeFiles\Makefile2:71: recipe for target 'CMakeFiles/newproject.dir/rule' failed
Makefile:108: recipe for target 'newproject' failed
mingw32-make.exe[3]: *** [newproject.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/newproject.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/newproject.dir/rule] Error 2
mingw32-make.exe: *** [newproject] Error 2

【问题讨论】:

  • 你有#include&lt;cmath&gt;有什么原因吗?我知道它不能解决根本问题,但您在此程序中不需要它。
  • 啊,我的错。删除它,菜鸟错误:)
  • 程序是正确的,见ideone.com/ESZi6G似乎一些基本的数学函数没有通过clion/mingw正确包含。
  • 链接器无权写入您的构建文件夹(不太可能),或者某些其他进程的旧版本 newproject.exe 仍处于打开状态。

标签: c++ mingw clion


【解决方案1】:

您的 mingw 设置存在某种问题,因为您的编译器无法找到 c:\mingw\include\math.h 文件中使用的函数。但是,您的代码实际上并不需要该标头。您可以删除 #include 行,它会编译并运行得很好。 (测试过)

如果您希望在其他程序中使用该标头,您确实需要解决您的 mingw 设置问题。

如果您仍然收到错误,很可能是由于您使用了其他标头。重新安装 MinGW,检查哪些文件应该包含缺少的功能,并尝试在您的机器上找到它们。

【讨论】:

  • 如果你不介意我问,我究竟是如何修复它的,因为我很久以前设置了 mingw,忘记了这个过程。我删除了#include 行。
  • 对不起,我不使用 windows ;/ 发布您遇到的下一个错误,以便我们了解导致它的原因。
  • 编辑您的帖子,添加新错误以及到目前为止您所经历的事情,以便更多人可以看到。当链接器尝试链接(或创建)您的可执行文件时,我看到“权限被拒绝”。我不知道如何在 Windows 上调试它。
  • 为什么这是一个公认的答案?它没有回答这个问题。不,编译器不是未能找到&lt;math.h&gt; 中使用的函数;它拒绝接受 _hypot( double, double ) 的声明原型,因为该原型违反了 OP 要求的 ANSI 符合性检查级别——不必要地,IMO。 (这是一个已知的 MinGW 错误,可以按照here 的描述进行修复。
【解决方案2】:

您的第一个问题(&lt;cmath&gt;)与this question 重复;您可以按照上面的描述解决根本问题,(但是为什么要在学习过程中这么早地规定编译器选项来调用严格的 ANSI 一致性检查呢?)

第二个问题当然值得进一步研究;这可能是由多种原因造成的,其中包括:

  • 您在尝试保存newproject.exe 的目录中没有写权限;可能看起来不太可能,但还是要检查一下。

  • 一个已经存在的newproject.exe,在这个目录中,被标记为“只读”,或者被另一个用户拥有,而你没有被授予写权限。

  • 另一个进程在newproject.exe 上有一个写锁,这会阻止ld.exe 获得写访问权限。

  • newproject.exe 已经作为某个不是文件的实体存在(可能是目录)。

  • 其他一些原因,目前还没有想到。

这在我看来不像是 MinGW 安装问题,但我是否建议您通过从等式中删除 CLion 来消除这种可能性,并直接从命令行发出适当的构建命令;作为一名实习程序员,学习如何做到这一点总是很好,你用任何更高级别的构建系统搅浑水之前。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-23
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    相关资源
    最近更新 更多