Rcpp error, learning Rcpp and C++ on mac - c++

I've decided to start learning Rcpp and C++ so I can make aspects of my R code faster. For a start I'm using the tutorial hadley has in the devtools wiki. I have a c++ compiler on this machine in that it's a mac and I have xcode installed on it - I'm told that puts the c++ compiler on the machine. I try to run the first example:
cppFunction('
int one(){
return 1;
}
')
However, what happens is:
sh: make: command not found
Error in sourceCpp(code = code, env = env, rebuild = rebuild, showOutput = showOutput, :
Error 1 occurred building shared library.
I'm guessing I have a setup issue, but what to do I'm not sure. For a general C++ knowlege I've started reading Absolute C++ by Savitch, which so far does not actually tell you anything about setting up a machine with compiler etc, because you're instructed to use something called MyProgrammingLab which just tells you if you got the answer right or not and gives output, you don't go through the compilation or anything like that.
Thanks,
Ben W.

Errors of the type sh: foo: command not found are pretty obvious. You are lacking the make command. Install it, and try again. Or if Xcode installs it outside of the path, add it to the path.

Related

i don't know what is wrong with vs code in running cpp

i just started to code in cpp and nothing is working idk if i didn't install the gcc properly or what but i already set the path of the bin file idk why it refuses to run
the code:
#include<iostream>
int main()
{
std::cout<<"hello";
}
and the problem is that when I try to use the "code runner extension" it is not working so I just press f5 and then when I get the error messages which says at first "could not find the task file'c/c++:g++.exe build active file' " and I get three options 1:debug anyway
2:configure task
3:cancel
when I choose debug anyway I get this error here
Since you're on windows consider installing Visual Studio or CLion. They're more beginner friendly - VSCode can get tricky to run c++ on windows. Either way, looks like you're trying to run your project without building it first. There should be a build option right next to the run option. Try building it, then running. The build is what compiles and creates the project.exe file, which is what the compiler is saying isn't there.
The referenced IDE's always auto-build on run, so there's that
If you're familiar with using the command line, these commands will do what you want, assuming the .cpp file is in your current directory.
g++ <FILENAME>
./a.out
There are wonderful flags you can add to the first command (the compiling command) like -Wall, -Wextra, -o, and many more. But, since it seems like you're just starting out, there's no need to worry about those!
If you're not familiar with the command line, I would encourage you to become familiar! It is an extremely powerful tool for a programmer. Here is a YouTube video that talks about it.

Codeblocks C 'Hello World' : Does not run, No such file or directory [duplicate]

I'm new to programming so I feel there is something simple I'm missing here.
I'm using the latest version of Mac and I've just installed Code::Blocks along with Xcode so I can use the gcc compiler.
I created a new "console application" project and the code for a hello world program shows up.
I built this code and it gave me 0 errors and 0 warnings so I thought it was off to a good start.
But when I go to run this code all I get is the following in my Terminal window:
*/Applications/CodeBlocks.app/Contents/MacOS/cb_console_runner DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:. /Users/Ryan/Documents/C++ book stuff/SayHello/bin/Debug/SayHello
Ryan-McMullens-iMac:~ Ryan$ /Applications/CodeBlocks.app/Contents/MacOS/cb_console_runner DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:. /Users/Ryan/Documents/C++ book stuff/SayHello/bin/Debug/SayHello
sh: /Users/Ryan/Documents/C++: No such file or directory
Process returned 127 (0x7F) execution time : 0.003 s
Press ENTER to continue.*
I've checked to make sure its using the gcc compiler. I've also tried creating a C++ directory in my documents folder but that didn't help.
Like I said, I feel like I'm just missing a step somewhere so any help would be really appreciated!
Thanks!
Try avoiding using '+' and spaces in the name of directories.
Many program don't handle escaping of pathnames when running external tools like compiler, linker or when calling the final executable.
Remove spaces from your path. It seems that codeblocks does not quote the paths, so every space will be introducing a new command or parameter. The problem here is this part:
C++ book
because it has a whitespace.

Calling a c++ function on Rstudio on a MAC and getting (clang: error: unsupported option '-fopenmp')

I know how to code but I really do not know my way around a computer.
I have a program that I have to run for my master thesis. It is a code with multiple collabs and runs perfectly on Linux. However, it is a very complex simulational code and therefore it takes time to run for multiple parameters. I've been using my Linux at the university to run it but would like to run some of it on my personal computer (MAC OS). It works by using the R language to call upon c++ functions as follows (being filename a code on c++).
On a Rstudio script:
Sys.setenv("PKG_CPPFLAGS" = "-fopenmp -DPARALLEL")
system("rm filename.so")
system("rm filename.o")
system ("R CMD SHLIB filename.cpp")
dyn.load("filename.so")
After system ("R CMD SHLIB filename.cpp") I get error:
clang: error: unsupported option '-fopenmp'
make: *** [filename.o] Error 1
I've researched on the subject and found this
Enable OpenMP support in clang in Mac OS X (sierra & Mojave)
I've Installed LLVM, yet I do not know how to use it in this case.
How do I use it in this case?
Thank you in advance.
"Don't do it that way." Read up on R and Rcpp and use the proper tools (especially for packaging and/or compiling) which should pick up OpenMP where possible. In particular,
scan at least the Rcpp Introduction vignette
also look at the Rcpp Attributes vignette
"Just say no" to building the compilation commands by hand unless you know what you are doing with R and have read Writing R Extensions carefully a few times. It can be done, I used to show how in tutorials and workshops (see old slides from 12-15 years ago on my website) but we first moved to package inline which helps here, and later relied on the much better Rcpp Attributes.
Now, macOS has some extra hurdles in which tools work and which ones don't. The rcpp-devel mailing list may be of help, the default step is otherwise to consult the tutorial by James.
Edit: And of course if you "just want the above to work" try the obvious step of removing the part causing the error, i.e. use
Sys.setenv("PKG_CPPFLAGS" = "")
as your macOS box appears to have a compiler but not OpenMP (which, as I understand it, is the default thanks to some "surprising" default choices at Apple -- see the aforementioned tutorial for installation help.)

Code::Blocks isn't executing properly

I'm new to programming so I feel there is something simple I'm missing here.
I'm using the latest version of Mac and I've just installed Code::Blocks along with Xcode so I can use the gcc compiler.
I created a new "console application" project and the code for a hello world program shows up.
I built this code and it gave me 0 errors and 0 warnings so I thought it was off to a good start.
But when I go to run this code all I get is the following in my Terminal window:
*/Applications/CodeBlocks.app/Contents/MacOS/cb_console_runner DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:. /Users/Ryan/Documents/C++ book stuff/SayHello/bin/Debug/SayHello
Ryan-McMullens-iMac:~ Ryan$ /Applications/CodeBlocks.app/Contents/MacOS/cb_console_runner DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:. /Users/Ryan/Documents/C++ book stuff/SayHello/bin/Debug/SayHello
sh: /Users/Ryan/Documents/C++: No such file or directory
Process returned 127 (0x7F) execution time : 0.003 s
Press ENTER to continue.*
I've checked to make sure its using the gcc compiler. I've also tried creating a C++ directory in my documents folder but that didn't help.
Like I said, I feel like I'm just missing a step somewhere so any help would be really appreciated!
Thanks!
Try avoiding using '+' and spaces in the name of directories.
Many program don't handle escaping of pathnames when running external tools like compiler, linker or when calling the final executable.
Remove spaces from your path. It seems that codeblocks does not quote the paths, so every space will be introducing a new command or parameter. The problem here is this part:
C++ book
because it has a whitespace.

Creating R package containing C++ on Windows

My goal is to create a package in R with C++ code: So my questions is how?
I am following the tutorial http://www.stat.columbia.edu/~gelman/stuff_for_blog/AlanRPackageTutorial.pdf on creating an R package containing C++ code. The specific code Im trying to compile and package is exactly as described in the tutorial.
R CMD SHLIB seems to be working creating .dll file.
I can load in R using dyn.load() and test it on simulated data (as described in tutorial)
R CMD INSTALL is where the problem begins. I have done two things encountering two different errors supposedly related:
1) The tutorial says the NAMESPACE file is supposed to contain the code:
useDynLib(XDemo)
export(XDemoAutoC)
When it does R CMD INSTALL fail resulting in error:
Error in inDL(x,as.logical(local), as.logical(now),...): unable to
load shared object 'C:/.../libs/i386/XDemo.dll': Loadlibrary failure:
1% is not a valid Win32-program
2) Removing the above mentioned lines in NAMESPACE file will result in installation of package. I can succesfully load it in R but when I try to use the R function that makes a .C() call to the C++ written function I another error:
library(newpackage)
ls(package:newpackage)
[[1]] "XDemoAutoC"
Warning message:
In ls(package:newpackage) :
‘package:newpackage’ converted to character string
XDemoAutoC(c(1,2,3,4))
Error in .C("DemoAutoCor", OutVec = as.double(vector("numeric", OutLength)), :
C symbol name "DemoAutoCor" not in load table
Im running version R2.15.2 on windows 64-bit and using R64 bit.
I read the following post with a similar problem:
http://r.789695.n4.nabble.com/Include-C-DLL-error-in-C-symbol-name-not-in-load-table-td3464021.html
Except they mention nothing about the NAMESPACE-matter.
Also I read this post:
Problem with loading compiled c code in R x64 using dyn.load
So I am thinking: that based on the fact that I am able to use dyn.load() in Rx64 means that I have succesfully created x64 .dll. Assuming that the NAMESPACE file is supposed to be left as in the tutorial - hopefully fixing the >>not in load table<< error - this would mean I should focus on fixing problem one. This problem seems to be caused by something related to 32-bit. I have used Dependency Walker on the .dll file but I am not sure how to interpret the results
I really don't have any ideas on how to fix this problem so any suggestion on what to do would be welcome?
I think you are doing it wrong. Two quick suggestions:
Read the Writing R Extensions manual written to explain just this: writing R extensions including those with compiled code
Have a look at Rcpp which makes R and C++ extensions, including package building so much easier. Or so we think. Writing a package is as easy as calling Rcpp.package.skeleton(). The documentation in 1) still help.
That said, if R CMD INSTALL fails you may have some mixup in your $PATH. Never ever mix MinGW and Cygwin. Make sure no Cygwin DLLs are found when you build or call R. Path order matters greatly. See the manual for details.