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.
Related
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
Consider the code below. No error is shown when I compile and run it with address sanitizer. But there should be an error right i.e assigning/accessing out of bounds memory location? Why doesn't address sanitizer detect it?
int arr[30];
int main(){
arr[40] = 34;
printf(ā%dā, arr[40]);
}
Thanks!
clang -fsanitize=address -fno-omit-frame-pointer test.c
./a.out
This is described by the following entry in FAQ:
Q: Why didn't ASan report an obviously invalid memory access in my code?
A1: If your errors is too obvious, compiler might have already optimized it
out by the time Asan runs.
A2: Another, C-only option is accesses to global common symbols which are
not protected by Asan (you can use -fno-common to disable generation of
common symbols and hopefully detect more bugs).
I'm trying to compile some code using MPI with mpif90. So I have several subroutines like :
subroutine name()
include "mpif.h"
...
and I have a huge number of warnings from this mpif.h like :
Warning: Unused parameter 'mpi_win_disp_unit' declared at (1)
mpif.h:243.27:
Included at main.f90:29:
INTEGER MPI_WIN_NULL
The problem is that I can't see the warnings that come from my actual code.
My current compiler options are :
-O3 -Wall -Wextra -Wtabs -ffixed-line-length-132
I would like to know if there is a flag that disable warnings from a specific file, or from included files.
Thank you in advance.
I got this error below and I can not find a solution. Does anybody know how
to fix this error?
rafael#ubuntu:~/avr/projeto$ clang -fsyntax-only -Os -I /usr/lib/avr/include -D__AVR_ATmega328P__ -DARDUINO=100 -Wno-ignored-attributes -Wno-attributes serial_tree.c
In file included from serial_tree.c:3:
In file included from /usr/lib/avr/include/util/delay.h:43:
/usr/lib/avr/include/util/delay_basic.h:108:5: error: invalid output
constraint
'=w' in asm
: "=w" (__count)
^
1 error generated.
util/delay.h is a Header from avr-libc, and the feature which you are using (some of the delay stuff) is using inline asm to implement it. avr-libc is written to be used with avr-gcc and uses features from avr-gcc like machine dependent constrains in inline asm. llvm does not recognize constraint "w", thus you have to use a different approach like using avr-gcc or porting that code to llvm.
avr-gcc also implements built-in function __builtin_avr_delay_cycles to implement wasting a specified number of clock cycles. If llvm is properly mimicking avr-gcc, then you can use that function as a starting point.
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.