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).
Related
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.
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 trying this code on gedit and compiling by g++ compiler on terminal.
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double sumC(NumericVector x) {
int n = x.size();
double total = 0;
for(int i = 0; i < n; ++i) {
total += x[i];
}
return total;
}
// [[Rcpp::export]]
double meanC(NumericVector x) {
return sumC(x) / x.size();
}
Error occurred for the header file.
fatal error: Rcpp.h: No such file or directory
I have compiled like this: g++ -I /usr/ r1.cpp -o c0 -L /usr/ -lRcpp
Also i tried :g++ -I /usr/lib/R/site-library/Rcpp/include/ r1.cpp -o c0 -L /usr/lib/R/site-library/Rcpp/libs/ -lRcpp . THen got error like fatal
error: R.h: No such file or directory #include <R.h>
Locations:
locate Rcpp.h:/usr/lib/R/site-library/Rcpp/include/Rcpp.h
locate R.h:/usr/share/R/include/R.h
I have tried with make file also.
My make file:
all:
g++ rcpp.cpp -o obj
compile:
I have attached all the depending header files in a single folder. Still getting the errors for Rcpp.
Any one knows how to compile this through terminal?
You can compile this file with
g++ -I/usr/share/R/include -I/usr/lib/R/site-library/Rcpp/include -c rcpp.cpp -o rcpp.o
However, I do not understand why you want to do this. In order to make such C++ functions callable from R, several additional steps are necessary:
C++ wrapper functions that translate to an interface based on R's SEXP.
R wrapper functions that call the C++ wrapper functions via .Call().
Linking of all the object files into a dynamic library that R can load.
Loading the library and the R wrapper functions into R.
All this is automated via sourceCpp() or when using Rcpp::compileAttributes() in the context of packages using Rcpp, c.f. the vignettes on attributes and packages.
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.
I've been learning C++ and using the Terminal for the last couple of months. My code was compiling and running fine using g++ and C++11, but in the last couple of days it started giving errors and I have had problems compiling since. The only programs I can compile and run depend on older C++ standards.
The errors I first got related to #include < array > in the header file. Not sure why this happened, but I got around it by using boost/array instead. Another error I can't solve is with std::stoi. Both array and stoi should be in the C++11 standard library. I made the following simple code to demonstrate what's going on:
//
// stoi_test.cpp
//
// Created by ecg
//
#include <iostream>
#include <string> // stoi should be in here
int main() {
std::string test = "12345";
int myint = std::stoi(test); // using stoi, specifying in standard library
std::cout << myint << '\n'; // printing the integer
return(0);
}
Try to compile using ecg$ g++ -o stoi_trial stoi_trial.cpp -std=c++11
array.cpp:13:22: error: no member named 'stoi' in namespace 'std'; did you mean
'atoi'?
int myint = std::stoi(test);
~~~~~^~~~
atoi
/usr/include/stdlib.h:149:6: note: 'atoi' declared here
int atoi(const char *);
^
array.cpp:13:27: error: no viable conversion from 'std::string' (aka
'basic_string') to 'const char *'
int myint = std::stoi(test);
^~~~
/usr/include/stdlib.h:149:23: note: passing argument to parameter here
int atoi(const char *);
^
2 errors generated.
I also get these errors at compilation when using gcc or clang++ and with -std=gnu++11 (I guess they all depend on the same file structure). I also get the same error whether I specify std:: in the code, or if I specify using namespace std;
I worry that these issues arose because of the September Command Line Tools update via Xcode or because I installed boost and this somehow messed up my C++11 libraries. Hopefully there is a simple solution.
My system:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-> dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.76) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix
Thanks for any insight you can offer.
clang has a weird stdlib, you need to add the following flag when you compile
-stdlib=libc++
your snippet works on my mac with
g++ -std=gnu++11 -stdlib=libc++ test.cpp -o test
This answer describes the problem