0

running on version 11.1.0 of gcc and g++. Every time I run this code I run into issues it says std::numbers was not declared. I tried running g++ randomCodeWhileReading.cpp -o main-std=c++20 within my terminal (im running ubuntu linux) and still no change. Here is the code in question:

#include <iostream>
#include <numbers>
int main()
{
    const long double pi {0};
    const long double pi2 {0};

    pi  = std::numbers::pi_v<long double>;
    pi2 = std::numbers::pi_v<long double>;

    std::cout << pi << std::endl << pi2;

}

Just wanted to see the numbers module in action nothing else. (is it even called a module or is it a header file?)

EDIT 10/6/21: The modifying a constant variable has been fixed. However, this code still wont run on my computer. Namely, the #include <numbers> does not seem to work on my machine it throws an error even when using -std=c++20. I am running gcc and g++ version 11.1 See error below:

gcc ex2_03.cpp -o -std=c++20
ex2_03.cpp: In function ‘int main()’:
ex2_03.cpp:22:65: error: ‘std::numbers’ has not been declared
   22 |     const double pond_diameter {2.0 * std::sqrt(pond_area/ std::numbers::pi)}; //find diameter by finding radius & multiplying by 2
      |    

however I was unable to replicate using godbolt.org (similar program not the same but uses as well). Clearly, it seems that this is an issue with my machine. How would I go about fixing this?

EDIT 10/8/21: I ran the code again using more flags and changing -std=c++20 to -std=c++2a this was what was returned:

chris@chris-Aspire-E5-576G:~/Desktop/programming/c++/Learning$ ls
ex2_02      HelloWorld          randomCodeWhileReading      textbookExample1
ex2_02.cpp  HelloWorld.cpp      randomCodeWhileReading.cpp  textbookExample1.cpp
ex2_02.o    HelloWorld.o        randomCodeWhileReading.o    textbookExample1.o
ex2_03      main                textbookDebug               textbookOutputNameAndAge.cpp
ex2_03.cpp  outputNameAndAge    textbookDebug.cpp
ex2_03.o    outputNameAndAge.o  textbookDebug.o
chris@chris-Aspire-E5-576G:~/Desktop/programming/c++/Learning$ g++ -g -Wall -pedantic -std=c++2a -o randomCodeWhileReading.cpp
g++: fatal error: no input files
compilation terminated.

added the ls output to show I was in the correct directory.

EDIT 10/8/21 v2: I used the following command and did not receive an error.

g++ randomCodeWhileReading.cpp -o main -std=c++20

Now just confused where the output went. By @nate's responses I assume it was sent to main? Just wanted to see a cout using std::numbers::pi

EDIT 10/8/21 v3: All clear nate explained program can be ran by using ./main

EDIT 10/8/21 v4: ... I repeated the earlier command and got a error:

g++ randomCodeWhileReading.cpp -o main -std=c++20
cc1plus: fatal error: randomCodeWhileReading.cpp: No such file or directory
compilation terminated.

can someone explain what went wrong this time? (I am still in the same directory). After using ls it seems that the file is no longer in the directory seems to be deleted?

EDIT 10/8/21 v5: I think the file got deleted when I was explaining the error to a friend and the wrong ways I was running the command lol. All good :D !

ExZerminator
  • 65
  • 2
  • 6
  • 2
    There is supposed to be a space between `-o main` and `-std=c++20`. Is that really your command line, or was it miscopied? – Nate Eldredge Oct 05 '21 at 00:04
  • It works fine here (after removing `const`): https://godbolt.org/z/65obeK4MP – Nate Eldredge Oct 05 '21 at 00:05
  • You might want to double check if you have libstdc++ of correct version installed. By default it should match your g++ version, but it is possible that you have an older version that doesn’t include the `` header yet. – Ranoiaetep Oct 05 '21 at 03:16
  • 1
    This question is a duplicate: [How to reinstall gcc and g++ on ubuntu linux](https://stackoverflow.com/a/69458400/421195), We still need to know 1) the EXACT error (copy/paste the ACTUAL text message - don't give us a "summary"!), 2) your g++ version, 3) your EXACT g++ command line. Look at your post: you ran "main-std=c++20" together above. Was that a typo? Or your actual command? Please [Edit] your post and provide this information. – paulsm4 Oct 06 '21 at 05:19
  • @NateEldredge see edit with error code I used -std=c++20 without main didn't work looks an issue isolated to my machine – ExZerminator Oct 06 '21 at 21:27
  • 3
    No, your command line is still wrong. The next argument following `-o` is always treated as the name of the output file (the executable), never as an option. With your original command it was writing the output to a file named `main-std=c++20` and not treating `-std=c++20` as an option. With your new command it is writing the output to a file named `-std=c++20` and still not treating it as an option. If you check your directory you'll see a lot of oddly named files. Try `g++ ex2_03.cpp -o main -std=c++20`. Paste it exactly including all the spaces. – Nate Eldredge Oct 06 '21 at 22:37
  • 1
    @ExZerminator: 1) thank you for updating your post. Having EXACT information is ESSENTIAL to resolving the problem., 2) I'm unable to reproduce .... because I don't have a g++ 10 or 11 compiler handy. I'm installing one now. 3) Please DON'T jump to the conclusion "there's something wrong with your install" yet. 4) Please DO fix your command line (per Nate Eldredge) and [Edit] your post with the results. – paulsm4 Oct 06 '21 at 23:23
  • @ExZerminator: did any of my suggestions or Nate Eldredge's suggestions help? Did you look at the alternatives in my response: a) try different `-std=xxx` options, b) try the "simplest possible hello world"code I posted, c) try [alternatives](https://linuxconfig.org/how-to-switch-between-multiple-gcc-and-g-compiler-versions-on-ubuntu-20-04-lts-focal-fossa) (on the off chance you have multiple versions, and your "compile" was inadvertantly picking up the wrong version)? – paulsm4 Oct 07 '21 at 20:08
  • You seem to be confused as to how the `-o` option works. Once again: whatever comes after `-o` is used as the **output file**. When you run a command like `gcc -o textbookExample1.cpp`, you are telling it to send the **output** (executable code) to `textbookExample1.cpp`, and you aren't specifying an input (source file) at all. The error message is perfectly correct. – Nate Eldredge Oct 08 '21 at 20:37
  • Three comments above I suggested a command that works for me. It does not sound like you have tried it yet. You are instead trying lots of other variations that are also wrong. – Nate Eldredge Oct 08 '21 at 20:38
  • (By the way, `gcc -o textbookExample1.cpp` may have erased or overwritten your source file `textbookExample1.cpp`. I hope you have a backup copy somewhere.) – Nate Eldredge Oct 08 '21 at 20:39
  • I am not sure if you are reading these comments, but I will say it once more and then stop: **DO NOT PUT YOUR SOURCE FILENAME IMMEDIATELY AFTER THE `-o` OPTION.** Put the desired **executable** filename there instead. Like, one more time: `g++ ex2_03.cpp -o main -std=c++20`. The `c++20` versus `c++2a` is not the issue here, gcc 11 should treat them exactly the same (with `c++20` preferred). – Nate Eldredge Oct 08 '21 at 20:54
  • 1
    @NateEldredge I'm sorry I clearly dont understand what im doing lol. Tried it your way I found no error message when using numbers! But there was no output either. I assume thats because it was sent to main? (I just want it to shoot out a cout statment) – ExZerminator Oct 08 '21 at 21:00
  • It creates an executable file called `main`, but doesn't run the program. To run it, you do `./main`. – Nate Eldredge Oct 08 '21 at 21:01
  • 1
    @NateEldredge OHHH so the command line prompt opens the file sends the executable code to main and then I run the executable code by using ./main correct? It worked just want to understand why . – ExZerminator Oct 08 '21 at 21:07
  • 1
    @NateEldredge never mind I thought I had it and I am now confused again. Tried the command that worked again and it threw an error. – ExZerminator Oct 08 '21 at 21:21
  • 1) `"no input files"` means you forgot the output filename after `-o`. 2) The output from g++ is the executable file "main". You can run it with the command `./main`. – paulsm4 Oct 08 '21 at 22:13

2 Answers2

4

You need to compile with the extra flag -std=c++20.

Moreover, there is an error in your code: pi and pi2 are declared const, hence you cannot modify them after they are initialized. Use this instead:

#include <iostream>
#include <numbers>

int main()
{
    const long double pi = std::numbers::pi_v<long double>;
    const long double pi2 = std::numbers::pi_v<long double>;
    std::cout << pi << std::endl << pi2;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
francesco
  • 7,189
  • 7
  • 22
  • 49
  • As stated in the post, I tried the -std=c++20 flag while compiling and still says std::numbers was not declared. The textbook said it was a module & to use import ; but that doesn't work at all. Thanks for the suggestion on the const modification didn't see that. – ExZerminator Oct 04 '21 at 23:13
  • @ExZerminator [cannot reproduce](https://godbolt.org/z/Kszzb3rc3) – francesco Oct 05 '21 at 08:35
  • See edit. Cannot reproduce either seems like a issue on my machine. Any suggestions to fix this? – ExZerminator Oct 06 '21 at 21:26
  • @ExZerminator: It's unlikely this is an "issue on your machine". I'm glad you copied/pasted the version info, exact error message and command line. Now please fix your command line (per Nate Eldredge's most recent comment above), and [Edit] your post again with what happened. – paulsm4 Oct 07 '21 at 00:32
2

Please try this code and this "compile" command on your machine with your version of g++:

/* 
 * TEST ENVIRONMENT: MSYS2 (Windows 10)
 * COMPILER: 
 *   g++ --version
 *   g++.exe (Rev5, Built by MSYS2 project) 10.3.0
 * BUILD COMMAND: 
 *   g++ -g -Wall -pedantic -std=c++2a -o x x.cpp
 *   <= NOTE: the correct command for C++ 20 compatibility in g++ 10.x is "-std=c++2a"
 * RUN COMMAND:
 *   ./x
 *   2pi=6.28319
 */
#include <iostream>
#include <numbers>

int main()
{
    auto two_pi = 2*std::numbers::pi;
    std::cout << "2pi=" << two_pi << '\n';
}

If it works, great. If it's the same, try "-std=c++20" and/or "-std=gnu++20". Look here for details: https://gcc.gnu.org/projects/cxx-status.html

See also:

Definitely "Update" your post to report back what happens. Be sure to copy/paste your commands and any error messages EXACTLY.

'Hope that helps.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Im running ubuntu linux. I ran the command as it appears and it returned "no input files" (im in the correct directory). I will edit the post with the exact input and error message – ExZerminator Oct 08 '21 at 20:45
  • To answer other questions you asked in the comments: I can run non c++20 features just fine through code blocks but through command line it says file doesn't exist – ExZerminator Oct 08 '21 at 20:54
  • `no input files` was a typo: you forgot the output filename. `it says file doesn't exist ` is too vague - that can mean any of several completely different things. Q: What exactly happens if you simply type the command (with `-o SOMEFILE` from a terminal window (no Codeblocks, no Godbolt: just g++)? Please [Edit] your post again, with a) copy/paste of the command, b) copy/paste of the error. Just like you did for the "no input files" error. – paulsm4 Oct 08 '21 at 22:11
  • Yea I was writing things out of order messing things up. I understand now that its g++ filename -o outputfilename -std=c++20. It ran fine it just ran wrong the second time because I was running the statement In different orders showing my friend not what to do lol. – ExZerminator Oct 08 '21 at 22:26
  • OK: so everything's working, correct? Please "upvote" this and francesco's answers, and please "accept" one of them. – paulsm4 Oct 08 '21 at 22:44