After including RcppArmadillo.h, errors occur when compiling code - r

I would like to use some of the functionalities included in RcppArmadillo. As I read in another post on SO, if RcppArmadillo.h is included, Rcpp.h should not be included. I did just that, but when trying to compile the .cpp file, I got some error messages. EDIT. Per Dirk's suggestion, I only included RcppArmadillo.h, which significantly reduced the number of error messages: The minimally reproducible code is below:
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
using namespace arma;
template <class RandomAccessIterator, class StrictWeakOrdering>
void sort(RandomAccessIterator first, RandomAccessIterator last, StrictWeakOrdering comp);
struct val_order{
int order;
double value;
};
bool compare(const val_order & a, const val_order & b){return (a.value<b.value);}
// [[Rcpp::export]]
IntegerVector order(NumericVector x){
int n=x.size();
std::vector<int> output(n);
std::vector<val_order> index(n);
for(int i=0;i<x.size();i++){
index[i].value=x(i);
index[i].order=i;
}
std::sort(index.begin(), index.end(), compare);
for(int i=0;i<x.size();i++){
output[i]=index[i].order;
}
return wrap(output);
}
The error message is below:
Error in sourceCpp("functions.cpp") :
Error 1 occurred building shared library.
ld: warning: directory not found for option '-L/usr/local/lib/gcc/i686-apple-darwin8/4.2.3/x86_64'
ld: warning: directory not found for option '-L/usr/local/lib/x86_64'
ld: warning: directory not found for option '-L/usr/local/lib/gcc/i686-apple-darwin8/4.2.3'
ld: library not found for -lgfortran
collect2: ld returned 1 exit status
make: *** [sourceCpp_44748.so] Error 1
Just to reiterate, this code has no problem compiling when I use Rcpp.h.

A few things:
Your post is not helpful. We do not need dozens of lines of error messages, but we do need reproducible code. Which you did not included.
You include several C++ headers and several R headers. Don't.
Include one header only: RcppArmadillo.h and if that fails, post a reproducible example.
Edit: Thanks for your update. Your code now compiles, your error is a linker error. You simply need to install the Fortran compiler under OS X.

Related

Clang static analyzer (clazy) ignores NOLINT and similar meta instructions in Qt code

I need to switch off some warnings that the Clang static analyzer (clazy) flags in some Qt code that I work with.
Appending '// NOLINT' as a comment to code lines that get flagged by clazy does not work, apparently because Qt is transformed to C++ code before clazy gets to see it, dispensing with all comments and pragmas. Appending '// clazy:exclude...' does not work either, and likewise with
#if defined(__clang__) // if that is even the right define to look for
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-W..."
#endif
...
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
Is there any elegant way of getting rid of clazy warnings in Qt code? I would prefer it to be applicable to individual lines as opposed to disabling warnings on entire files.
You should use
<your code> // clazy:exclude=<warning>,<another warning>
For example, I get clazy-strict-iterators warning in my code.
I've added // clazy:exclude=strict-iterators in the end of line, and the warning is gone.
Note that you should not add clazy- prefix to the warning name.
Source: https://blogs.kde.org/2016/04/25/clazy-suppressing-warnings

Error in including boost tokenizer.hpp file when building R package on MacOS/CRAN

I'm running into issues publishing my R package to CRAN, because of a specific error when including boost libraries. The top of my one .cpp file in the package is
#include <Rcpp.h>
#include <boost/tokenizer.hpp>
#include <boost/algorithm/string.hpp>
#include <algorithm>
#include <string>
#include <unordered_map>
#include <omp.h>
#include <vector>
// [[Rcpp::depends(BH)]]
// [[Rcpp::plugins(openmp)]]
When running through the check on MacOS (via rhub::check(platform = "macos-highsierra-release-cran"), I get the following error:
In file included from wgt_jaccard.cpp:6:
/Users/user2suimGYX/R/BH/include/boost/tokenizer.hpp:63:9: error: field of type 'std::_1::wrap_iter<const char *>' has private constructor
: first(c.begin()), last(c.end()), f(f) { }
^
wgt_jaccard.cpp:117:19: note: in instantiation of function template specialization 'boost::tokenizer<boost::char_separator<char, std::__1::char_traits >, std::__1::__wrap_iter<const char *>, std::__1::basic_string ::tokenizer<Rcpp::internal::string_proxy<16, PreserveStorage> >' requested here
tokenizer tokens(y(i), sep);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/iterator:1420:31: note: declared private here
_LIBCPP_INLINE_VISIBILITY __wrap_iter(iterator_type __x) _NOEXCEPT_DEBUG : __i(__x) {}
^
My Makevars file's contents are
PKG_CXXFLAGS = $(SHLIB_OPENMP_CXXFLAGS) -DBOOST_NO_AUTO_PTR
PKG_LIBS = $(SHLIB_OPENMP_CXXFLAGS)
CXX_STD = CXX11
I've tried searching around, but can't find much on this error. The full package is located here. Any help is much appreciated.
Relative to the repo you kindly supplied, we found a need for two changes:
First, to no (unconditionally) have an #include <omp.h> as OpenMP can be optional, esp. on macOS. A simpled #ifdef OPENMP does the job.
Second, the (arguably near-incomprehensible) compiler message had to do with the fact that the Boost type / class for tokenizer was puzzled by the Rcpp object you gave it by directly indexing from an Rcpp::CharacterVector. Been there, done that -- a more conservative approach is to first assign to std::string and to then pass that on.
With those two changes, it's all roses and it compiles on macOS under clang++ as well.
The (by now merged, thanks) PR #2 has the gory details, but is short.

Error during compile of Megablink demo using freeRTOS for atmega2560 using WinAVR

The following RTOS .c files compile fine during 'make':
task.c, queue.c, tasks.c, croutines.c, list.c
Then I get the following error:
serial.c: In function 'xSerialPortReInit'
serial.c: 665:error: 'ulWantedBaud' undeclared (first use in this
function)
serial.c: 665:error: (Each undeclared identifier is reported only
once for each function it appears in)
make: ***[serial.o]Error 1
I have checked that all include files identified in serial.c are available
Why do you think it is an include file problem? ulWantedBaud is more likely to be a function parameter, or file scope variable, that is misspelled. The AVR demo in the official FreeRTOS download (whic is very old and probably not the best reference) contains a serial port init function with such a parameter, although not a re-init function as per your post:
xComPortHandle xSerialPortInitMinimal( uint32_t ulWantedBaud, uint8_t uxQueueLength );
(This snippet is from http://www.freertos.org/a00098.html)

RcppZiggurat unable to compile example code

I am trying to employ the Ziggurat sampler in R, however actually wanted to use it directly in my C++ code. I installed the GSL library, RcppGSL and RcppZiggurat and using zrnorm() in R works just fine. I thought ok, lets try to compile the code sample provided in the RcppZiggurat.pdf, and go from there to implement the Ziggurat sampler directly in my C++ code... the following happens though...
From the pdf file I thought I can simply utilize:
#include <Rcpp.h>
#include <Ziggurat.h>
static Ziggurat::Ziggurat::Ziggurat zigg;
// [[Rcpp::export]]
Rcpp::NumericVector zrnorm(int n) {
Rcpp::NumericVector x(n);
for (int i=0; i<n; i++) {
x[i] = zigg.norm();
}
return x;
}
// [[Rcpp::export]]
void zsetseed(unsigned long int s) {
zigg.setSeed(s);
return;
}
Error:
official_zigg_code.cpp:2:10: fatal error: 'Ziggurat.h' file not found
#include <Ziggurat.h>
^
1 error generated.
make: *** [official_zigg_code.o] Error 1
clang++ -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include -
I/usr/local/include/freetype2 -I/opt/X11/include -
I"/Library/Frameworks/R.framework/Versions/3.1/Resources/library/Rcpp/include" -fPIC -Wall -
mtune=core2 -g -O2 -c official_zigg_code.cpp -o official_zigg_code.o
Error in Rcpp::sourceCpp("official_zigg_code.cpp") :
Error 1 occurred building shared library.
I have absolutely no clue how to proceed from here. I desperately tried to find answers on stack exchange but nothing could help me to solve this. From what I understand the RcppZiggurat package actually uses the above function so how can I fail to compile it, when I am able to use zrnorm() directly?
The error is fairly obvious:
fatal error: 'Ziggurat.h' file not found
This means that you did not tell R / the compiler about RcppZiggurat.
The fix is easy. In the case of an Rcpp-driven compilation via sourceCpp(), add
this one line
// [[Rcpp::depends(RcppZiggurat)]]
which does just that. All this is documented with Rcpp, and you are more or less expected to read at least some of its documentation.
If you want to build outside of Rcpp, you need to make sure the compiler find the header file(s). One commonly uses the -I flag for that, this is typically discussed where compiler are introduced.

problems compiling and running Nachos

First issue: I am trying to import stdlib.h in order to use functions like malloc() and rand(). I am working in threadtest.cc , folder threads. This is the error I get after
#include <stdlib.h>
In file included from ../threads/threadtest.cc:18:0:
/usr/include/stdlib.h:146:33: error: declaration of ‘double atof(const char*) throw ()’ has a different exception specifier
In file included from ../threads/utility.h:57:0,
from ../threads/system.h:12,
from ../threads/threadtest.cc:13:
../machine/sysdep.h:62:8: error: from previous declaration ‘double atof(const char*)’
In file included from ../threads/threadtest.cc:18:0:
/usr/include/stdlib.h:149:33: error: declaration of ‘int atoi(const char*) throw ()’ has a different exception specifier
In file included from ../threads/utility.h:57:0,
from ../threads/system.h:12,
from ../threads/threadtest.cc:13:
../machine/sysdep.h:61:5: error: from previous declaration ‘int atoi(const char*)’
In file included from ../threads/threadtest.cc:18:0:
/usr/include/stdlib.h:771:60: error: declaration of ‘int abs(int) throw ()’ has a different exception specifier
In file included from ../threads/utility.h:57:0,
from ../threads/system.h:12,
from ../threads/threadtest.cc:13:
../machine/sysdep.h:63:5: error: from previous declaration ‘int abs(int)’
../threads/threadtest.cc: In function ‘void shout(int)’:
../threads/threadtest.cc:83:25: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
What can be a reason for this?
Second issue: I am trying to give a forked new thread a name.
Thread * t = new Thread ("my id");
t->Fork(shout, S);
And testing it in function shout:
printf ("%s", currentThread->getName());
This example works fine.
However, if I want to give the thread a "dynamic" id, I use:
sprintf(id, "%d", i);
Thread * t = new Thread (id);
With the same printf for currentThread->getName() it gets me junk:
x�ӿ����_ ���
I have used example from the Nachos documentation as well as tried different ways to transmit a dynamic id using string functions, but nothing worked, only junk.
Thank you for any answers.
I had the same problem in my Nachos homework.
My roommate said maybe it's a incompatibility problem, and suggested that I delete "stdlib.h".
It worked!

Resources