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 !
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;
}
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:
https://stackoverflow.com/a/67453352/421195
https://stackoverflow.com/a/67406788/421195
Definitely "Update" your post to report back what happens. Be sure to copy/paste your commands and any error messages EXACTLY.
'Hope that helps.
Related
I'm attempting to write a simple program that calls a function written in a pair of Header and CPP files.
I'm doing this on a Raspberry Pi 3 Model B, and the Geany IDE v1.37.1.
Compile Command:
g++ -Wall -c "%f" -c test.cpp
Build Command:
g++ -Wall -o "%e" "%f" -o test test.cpp
main.cpp:
#include "test.h"
int main()
{
test_function();
return 0;
}
test.h:
#ifndef _test_h_
#define _test_h_
#include <iostream>
void test_function();
#endif
test.cpp:
#include "test.h"
void test_function()
{
std::cout << "hello world";
}
The code above compiles & builds fine, however attempting to run it yields the following error:
./main: not found
(program exited with code: 127)
Perhaps I am messing something up with the Compile & Build Commands?
Thank you for reading my post, any guidance is apprecaited!
Notice the compile command:
-o test
This means that the output binary will be test, so you can execute the application in your terminal or shell via ./test.
I have been trying to compile and run the following very simple bit of code
#include <thread>
using namespace std;
void thread_c() {
for (int i = 0; i < 11; ++i) {
cout << i;
}
}
int main()
{
thread t(thread_c);
t.join();
system("pause");
return 0;
}
I am compiling on Windows 10 with MinGW 6.3.0 (target x86_64-w64-mingw32) and the thread model (obtained using g++ -v) is POSIX.
I am not getting any compilation error with g++ -Wall -g test.cpp -o test.exe, however I am getting a runtime error when trying to run the exe (entry point of _ZNSt6thread15_M_start_threadESt10unique_ptrINS_3_StateESt14default_deleteIS1_EEPFvve cannot be found).
I also tried compiling with the -pthreador -lpthread flags, but I am getting the same runtime error.
Obviously this seems to be related to the use of std::thread, but I didn't get how to fix this. Am I missing a compilation flag or additional library enabling POSIX thread support on Windows?
Edit: I managed to get it working by changing the compilation command to
g++ -Wall -g test.cpp -o test.exe -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,Bdynamic
I tried your code after making the necessary fixes:
#include <thread>
#include <iostream>
using namespace std;
void thread_c() {
for (int i = 0; i < 11; ++i) {
std::cout << i;
}
}
int main()
{
thread t(thread_c);
t.join();
system("pause");
return 0;
}
and it worked fine.
The build command was the same as yours:
g++ -Wall -g test.cpp -o test.exe
The output was:
012345678910Press any key to continue . . .
But I did use MinGW-w64 GCC 12.1.0 (fro https://winlibs.com/) instead of the very old version 6.3.0.
Maybe the MinGW-w64 (or was it still the old MinGW) library you were using was simply too old...
I'm trying to build a write of software with the Tensor module provided as unsupported from eigen3. I've written a simple piece of code that will build with a simple application of VectorXd (just printing it to stdout), and will also build with an analogous application of Tensor in place of the VectorXd, but WILL NOT build when I do not throw an optimization flag (-On). Note that my build is from within a conda enviromnent that is using conda-forge compilers, so the g++ in what follows is the g++ obtained from conda forge for ubuntu. It says its name in the error messages following, if that is perceived to be the issue.
I have a feeling this is not about the program I'm trying to write, but just in case I've included an mwe.cpp that seems to produce the error. The code follows:
#include <eigen3/Eigen/Dense>
#include <eigen3/unsupported/Eigen/CXX11/Tensor>
#include <iostream>
using namespace Eigen;
using namespace std;
int main(int argc, char const *argv[])
{
VectorXd v(6);
v << 1, 2, 3, 4, 5, 6;
cout << v.cwiseSqrt() << "\n";
Tensor<double, 1> t(6);
for (auto i=0; i<v.size(); i++){
t(i) = v(i);
}
cout << "\n";
for (auto i=0; i<t.size(); i++){
cout << t(i) << " ";
}
cout << "\n";
return 0;
}
If the above code is compiled without any optimizations, like:
g++ -I ~/miniconda3/envs/myenv/include/ mwe.cpp -o mwe
I get the following compiler error:
/home/myname/miniconda3/envs/myenv/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/bin/ld: /tmp/cc2q8gj4.o: in function `Eigen::internal::(anonymous namespace)::get_random_seed()':
mwe.cpp:(.text+0x15): undefined reference to `clock_gettime'
collect2: error: ld returned 1 exit status
If instead I ask for 'n' optimization level, like the following:
g++ -I ~/miniconda3/envs/loos/include/ -On mwe.cpp -o mwe
The program builds without complaint and I get expected output:
$ ./mwe
1
1.41421
1.73205
2
2.23607
2.44949
1 2 3 4 5 6
I have no clue why this little program, or the real program I'm trying to write, would be trying to get a random seed for anything. Any advice would be appreciated. The reason why I would like to build without optimization is so that debugging is easier. I actually thought all this was being caused by debug flags, but I realized that my build tool's debug setting didn't ask for optimization and narrowed that down to the apparent cause. If I throw -g -O1 I do not see the error.
Obviously, if one were to comment out all the code that has to do with the Tensor module, that is everthing in main above 'return' and below the cwiseSqrt() line, and also the include statement, the code builds and produces expected output.
Technically, this is a linker error (g++ calls the compiler as well as the linker, depending on the command line arguments). And you get linker-errors if an externally defined function is called from somewhere, even if the code is never reached.
When compiling with optimizations enabled, g++ will optimize away uncalled functions (outside the global namespace), thus you get no linker errors. You may want to try -Og instead of -O1 for better debugging experience.
The following code should produce similar behavior:
int foo(); // externally defined
namespace { // anonymous namespace
// defined inside this module, but never called
int bar() {
return foo();
}
}
int main() {
// if you un-comment this line, the
// optimized version will fail as well:
// ::bar();
}
According to man clock_gettime you need to link with -lrt if your glibc version is older than 2.17 -- maybe that is the case for your setup:
g++ -I ~/miniconda3/envs/myenv/include/ mwe.cpp -o mwe -lrt
I am getting the following strange error:
> sourceCpp( "comp.Cpp" )
Warning message:
In sourceCpp("comp.Cpp") :
No Rcpp::export attributes or RCPP_MODULE declarations found in source
when I use sourceCpp. The "comp.Cpp" file looks like this:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp:export]]
RcppExport SEXP comp(int n){
int i;
Rcpp::NumericVector product(n);
for(i=0;i<n;i++){
product[i]=i;
}
return(product);
}
I tried updating my operating system to Maverick (and then had to reinstall Xcode command line tools and some other things) but this error predates that. I can make the test package and install it and run the hello world it provides, so the Rcpp package is mostly working. I also get another error from running in R:
cppFunction( "
int useCpp11() {
auto x = 10;
return x;
}
", plugins=c("cpp11" ) )
which is
llvm-g++-4.2 -arch x86_64 -I/Library/Frameworks/R.framework/Resources/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -std=c++11 -fPIC -mtune=core2 -O3 -c file69810a85a0d.cpp -o file69810a85a0d.o
Error in sourceCpp(code = code, env = env, rebuild = rebuild, showOutput = showOutput, :
Error 1 occurred building shared library.
cc1plus: error: unrecognized command line option "-std=c++11"
make: *** [file69810a85a0d.o] Error 1
I don't know if these two things are related. I think something is happening with my compiler not playing well with attributes, but hunting around the internet hasn't educated me sufficiently to understand that.
Any help would be greatly appreciated.
Change "[[Rcpp:export]]" by "[[Rcpp::export]]".
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
SEXP comp(int n){
int i;
Rcpp::NumericVector product(n);
for(i=0;i<n;i++){
product[i]=i;
}
return(product);
}
Your compiler is too old for the C++11 flag. And the error message is very clear about it.
Try -std=c++0x as well as upgrading to Xcode 5 (which has its own set of issues -- but those are well documented here).
I'm having trouble getting the library working on macosx. First off, I tried to compile the following code, saved as rand.cpp, taken from the c++ website
#include <iostream>
#include <random>
int main()
{
const int nrolls=10000; // number of experiments
const int nstars=100; // maximum number of stars to distribute
std::default_random_engine generator;
std::normal_distribution<double> distribution(5.0,2.0);
int p[10]={};
for (int i=0; i<nrolls; ++i) {
double number = distribution(generator);
if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
}
std::cout << "normal_distribution (5.0,2.0):" << std::endl;
for (int i=0; i<10; ++i) {
std::cout << i << "-" << (i+1) << ": ";
std::cout << std::string(p[i]*nstars/nrolls,'*') << std::endl;
}
return 0;
}
Upon running this with g++ rand.cpp -o rand i get the following errors
rand.cpp:9: error: ‘default_random_engine’ is not a member of ‘std’
rand.cpp:10: error: ‘normal_distribution’ is not a member of ‘std’
Searching around it seems to be suggested that the issue is the compiler, apparently thus library is only available to gcc11. I found a way to update gcc using the macport package as shown here Update GCC on OSX but I still don't know how to use this new compiler. Running g++ rand.cpp -o rand returns the same errors even when I change the compiler with sudo port select --set gcc gcc40 or sudo port select --set gcc mp-gcc46. I also tried using g++ -std=c++11 rand.cpp -o rand which just returns
cc1plus: error: unrecognized command line option "-std=c++11"
Does anyone know what I am doing wrong?
Try it with Clang++, which should be installed in your mac, or a new version of GCC.
gcc42: I had this version installed, it didn't work, and didn't recognize -std=c++0x and -std=c++11.
gcc49: Installed this with brew, it gave the same error but -std=c++11 made it work.
Clang++: Worked like a charm without even specifying the standard (it probably defaults to c++11).
Also, check if you have the latest version of the command line tools, if not, download them from the Downloads for Apple Developers website.
What you're doing wrong
The version you installed doesn't have the -std=c++11 option, but it should work with -std=c++0x or -std=gnu++0x, that's what it says in the manual for the 4.6 version.