1

I've just started to learn the C++ programming language by reading the book "C++ Primer Plus 5th addition" but I'm coming across a problem. The book just started to go into detail with functions, function prototypes, function headers and such.I decided to try and make a KG --> Pounds converter as practice, but my problem is that when I try to build it (Im using CLion) I get a build error.

The code:

#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;

}

First Build error:

"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

After removing the #include cmath line as it wasn't needed nor being used in my program, I attempted to build it yet again but received a different error which was:

"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
Nadim
  • 47
  • 7
  • Is there a reason you have `#include`? I know it doesn't fix the underlying issue but you don't need it in this program. – NathanOliver May 27 '15 at 12:09
  • Ah, my mistake. Removed it, rookies mistake :) – Nadim May 27 '15 at 12:10
  • The program is correct, see https://ideone.com/ESZi6G it seems like some of the basic math functions aren't included correctly through clion/mingw. – AliciaBytes May 27 '15 at 12:12
  • Either the linker doesn't have permissions to write to your build folder (unlikely) or some other process has an old version of `newproject.exe` still open. – sjdowling May 27 '15 at 12:36

2 Answers2

0

There is some sort of problem with your mingw setup, because your compiler is failing to find a function used in the c:\mingw\include\math.h file. However you don't actually need that header for your code. You can remove the line #include and it's going to compile and run just fine. (tested it)

You do need to solve your mingw setup however if you wish to use that header in other programs.

If you still get an error it would be most likely due to the other header that you have used. Reinstall MinGW, check which files should contain the missing functions and try to find them on your machine.

XapaJIaMnu
  • 1,408
  • 3
  • 12
  • 28
  • How exactly do I got about fixing it if you don't mind me asking, because I setup mingw a long time ago and forgot the process. I've removed the #include line. – Nadim May 27 '15 at 12:15
  • Sorry, I don't use windows ;/ Post the next error you are getting so that we can see what causes it. – XapaJIaMnu May 27 '15 at 12:16
  • Edit your post with the new error and what you have gone through so far so more people can see. I see a "Permission denied" when the linker tries to link (or create) your executable. I don't know how to debug that on Windows. – XapaJIaMnu May 27 '15 at 12:20
  • Why is this an accepted answer? It does not answer the question. No, the compiler is ***not*** failing to find a function used in ``; it is refusing to accept a declared prototype for `_hypot( double, double )`, because that prototype violates the level of ANSI conformity checking which the OP has requested -- unnecessarily, IMO. (This is a known MinGW bug, which may be fixed as described [here](http://stackoverflow.com/questions/29450016)). – Keith Marshall May 28 '15 at 08:16
0

Your first problem, (with <cmath>), is a duplicate of this question; you can fix the underlying issue as described therin, (but why are you stipulating compiler options to invoke strict ANSI conformity checking so early in your learning process anyway?)

The second problem certainly merits further investigation; it could be due to any of a number of causes, amongst them:

  • You don't have write permission in the directory where you are trying to save newproject.exe; may seem unlikely, but check anyway.

  • An already existing newproject.exe, in this directory, is marked as "read only", or is owned by another user, and you have not been granted write permission.

  • Another process has a write lock on newproject.exe, which is preventing ld.exe from gaining write access.

  • newproject.exe already exists as some entity which is not a file, (a directory perhaps).

  • Some other reason, which doesn't spring to mind right now.

This doesn't look to me like a MinGW installation problem, but might I suggest that you eliminate this possibility by removing CLion from the equation, and simply issue the appropriate build commands directly from the command line; as a trainee programmer, it's always good to learn how to do that, before you muddy the waters with any higher level build system.

Community
  • 1
  • 1
Keith Marshall
  • 1,980
  • 15
  • 19