Rcpp compile issue in Rstudio - macos

I face an error when compiling an simple rcpp code.
Rcpp::sourceCpp(code = '
#include <RcppArmadillo.h>
#ifdef _OPENMP
# include <omp.h>
#endif
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::plugins(openmp)]]
// [[Rcpp::export]]
void omp_test()
{
#ifdef _OPENMP
Rprintf("OpenMP threads available: %d\\n", omp_get_max_threads());
#else
Rprintf("OpenMP not supported\\n");
#endif
}
')
When I compile the same code in R, not in Rstudio, it works well.
However, when I compile it in Rstudio, it shows an error like below:
[![enter image description here][1]][1]
> Rcpp::sourceCpp("R/utils/cpp_utils.cpp")
ld: warning: directory not found for option '-L/opt/R/arm64/lib'
ld: warning: dylib (/Library/Frameworks/R.framework/Resources/lib/libRlapack.dylib) was built for newer macOS version (20.0) than being linked (12.0)
ld: warning: dylib (/Library/Frameworks/R.framework/Resources/lib/libRblas.dylib) was built for newer macOS version (20.0) than being linked (12.0)
ld: warning: dylib (/opt/R/arm64/gfortran/lib/libgfortran.dylib) was built for newer macOS version (12.1) than being linked (12.0)
ld: warning: dylib (/Library/Frameworks/R.framework/R) was built for newer macOS version (20.0) than being linked (12.0)
Error in dyn.load("/private/var/folders/bm/_jr48kjs1ss7gk_vwbcxc7nr0000gn/T/RtmpFzxtqw/sourceCpp-aarch64-apple-darwin20-1.0.8/sourcecpp_6e64632dce2/sourceCpp_4.so") :
unable to load shared object '/private/var/folders/bm/_jr48kjs1ss7gk_vwbcxc7nr0000gn/T/RtmpFzxtqw/sourceCpp-aarch64-apple-darwin20-1.0.8/sourcecpp_6e64632dce2/sourceCpp_4.so':
dlopen(/private/var/folders/bm/_jr48kjs1ss7gk_vwbcxc7nr0000gn/T/RtmpFzxtqw/sourceCpp-aarch64-apple-darwin20-1.0.8/sourcecpp_6e64632dce2/sourceCpp_4.so, 0x0006): Library not loaded: #rpath/libgfortran.5.dylib
Referenced from: /private/var/folders/bm/_jr48kjs1ss7gk_vwbcxc7nr0000gn/T/RtmpFzxtqw/sourceCpp-aarch64-apple-darwin20-1.0.8/sourcecpp_6e64632dce2/sourceCpp_4.so
Reason: tried: '/usr/lib/libgfortran.5.dylib' (no such file)
To install rcpp, I followed the instructions in this page: Configuring compilers on Mac M1 (Big Sur, Monterey) for Rcpp and other tools
Moreover, as I want to debug the rcpp with lldb, I installed R from the website: https://mac.r-project.org/
I tried to link the libgfortran.5.dylib to usr/lib but permission was denied even with sudo.
I am using Mac monetery with M1,
Thanks for your solutions to the issue.

See this answer: Configuring compilers on Mac M1 (Big Sur, Monterey) for Rcpp and other tools
The issue is that your library path configured is invalid - it starts with "-L". This issue could be in a makefile for the project.
Check your RStudio build flags if you are specifying them in the command line. To specify the library path, you may need to use a lower-case L based on the documentation.
-l, --library=LIB library directory used for test installation
Alternatively, this site details the commands to check and set the library paths in RStudio.

Related

Error "fatal error: 'sys/sysmacros.h' file not found"

I have a compilation error on macOS v11 (Big Sur) with the standard Clang compiler (version 13.0.0).
I am trying to include the sys/sysmacros.h to use the makedev() function which surprisingly is mentioned on the Apple developer website and should be compatible with macOS 15.5+.
Including sys/types.h also gives me an error, however sys/stat.h works. Sadly it still doesn't give me the makedev(), major() and minor() functions that I need.
The Linux manual page of makedev states there were some changes in the glibc library, but as far as I know, macOS does not use the glibc library.
There should be a simple way of installing glibc on macOS using Homebrew (brew) as described here, but I get the same error as was mentioned in this question. So apparently currently there is no proper way of doing it and then I am not sure if this will solve my problem.
Is there a solution to this?
The macro makedev is defined in sys/types.h. Just add #include <sys/types.h> into your files. sys/types.h is a header file of Kernel.framework. You should set it in the Clang invocation, like clang -framework Kernel ....
You can also define these macros as they are defined in sys/types.h:
#define major(x) ((int32_t)(((u_int32_t)(x) >> 24) & 0xff))
#define minor(x) ((int32_t)((x) & 0xffffff))
#define makedev(x, y) ((dev_t)(((x) << 24) | (y)))

OSX go (lang) QML static compile failing due to version difference

I am trying to statically compile a go application using Qt and run it on a computer without Go or Qt installed.
At the top of my main.go I have:
// #cgo LDFLAGS: -static -L/Users/$USER/Qt/5.7.0_static_osx/lib -lgb
//go:generate genqrc ui
I have static compiled my application using:
go generate && go build -a -installsuffix cgo
The go generate compiles the .qml files into the binary while as I understand it go build -a -installsuffix cgo statically compiles all the .go and .a files from Qt.
I am using go version go1.8 darwin/amd64 and I have 5.7.0_static_osx Qt in my /Users/$USER/Qt folder and in the /lib folder I have all the .a files
I am using OSX 10.11.6 (El Capitan)
Now the issue is when I move it to another mac I get the error:
​/Users/$USER/Downloads/PACKAGED.app/Contents/Resources
AppSettings.plist
MainMenu.nib
appIcon.icns
PACKAGEDAPP
script
dyld: Library not loaded: /usr/local/opt/qt5/lib/QtWidgets.framework/Versions/5/QtWidgets
Referenced from: /Users/$USER/Downloads/PACKAGED.app/Contents/Resources/./encryptoclient
Reason: Incompatible library version: encryptoclient requires version 5.7.0 or later, but QtWidgets provides version 5.4.0
/Users/$USER/Downloads/PACKAGED.app/Contents/Resources/script: line 5: 9987 Trace/BPT trap: 5 ./encryptoclient
Which makes me think that Qt wasn't statically compiled, is this so? And if so, what do I need to do to compile properly?
P.S (FYI) Its in a .app because I have wrapped it in a Platypus app to make moving and running on other computer an easy 'double-click' off the USB stick

Building a cross compile of binutils on OS X Yosemite

I'm trying to build binutils for generating MIPS code on Mac OS X.
I found this site (http://www.theairportwiki.com/index.php/Building_a_cross_compile_of_GCC_for_MIPS_on_OS_X) from How to build GCC 4.8.x on Mac OS X host for MIPS target, and followed the instructions.
I install the gcc-4.8 from brew, and installed source code of binutils and gcc. This is the compilation option setup.
$ export CC=/usr/local/bin/gcc-4.8
$ export CXX=/usr/local/bin/g++-4.8
$ export CPP=/usr/local/bin/cpp-4.8
$ export LD=/usr/local/bin/gcc-4.8
$ export PREFIX=/opt/cross/gcc-mips
$ export CFLAGS=-Wno-error=deprecated-declarations
Then I configure, and make the bintuils.
The issue is that after building static libraries I have an error message that archive is not built for x86_64, and then I have a bunch of undefined symbol error.
ignoring file ./../intl/libintl.a, file was built for archive which is not the architecture being linked (x86_64): ./../intl/libintl.a
Undefined symbols for architecture x86_64:
"__bfd_abort", referenced from:
_fix_new_internal in write.o
_size_seg in write.o
Googling to find that I need to setup AR (archive) variable also from https://github.com/tpoechtrager/osxcross/issues/11. I added export AR=/usr/local/bin/gcc-ar-4.8, but I have another error message because gcc-ar-4.8 doesn't work.
/usr/local/bin/gcc-ar-4.8
/usr/local/bin/gcc-ar-4.8: Cannot find plugin 'liblto_plugin.so'
Googling again to find that gcc-ar doesn't work with Mac OS X (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56893).
gcc-ar is for use with newer GNU binutils only as that is the only ar which supports plugins.
Apple's ar does not support plugins (though it could be made to; it will be a different plugin interface than the GNU BFD
plugin interface which GCC supports right now).
I just created a dumb 'liblto_plugin.so' file in /usr/local/Cellar/gcc48/4.8.4/libexec/gcc/x86_64-apple-darwin14.3.0/4.8.4 directory to suppress the error message, but in this case it looks like that /usr/ar is invoked when I use /usr/bin/gcc-ar-4.8 to get the same architecture and undefined symbols error.
How to solve these issues? How to build cross compiler tools (gcc and binutils) on Mac OS X?
The static library generator for Mac OS X is not ar, but libtool -static. There is another SO post about this - Static library link issue with Mac OS X: symbol(s) not found for architecture x86_64.
The binutils has multiple libraries that are linked statically. So I replaced all the ar rc command with libtool -static -o to get the static libraries that do not cause errors.
In doing so, I had to make two modifications also.
Some library generate libtool script to conflict the Mac OS X's libtool, I had to rename the script.
Some object files do not contain symbols, I had to remove the objects.
Then I could get the binaries without any issue.

Missing ___emutls_get_address with gcc-4.7 and openMP

I am trying to compile a program using a self-compiled GCC-4.7.1 on Mac OS 10.8.2. The program uses openMP and the compilation succeeds; however, when I try to run the program, the dynamic linker complains with
dyld: lazy symbol binding failed: Symbol not found: ___emutls_get_address
Referenced from: /usr/local/gcc-4.7.1/lib/libgomp.1.dylib
Expected in: /usr/lib/libSystem.B.dylib
dyld: Symbol not found: ___emutls_get_address
Referenced from: /usr/local/gcc-4.7.1/lib/libgomp.1.dylib
Expected in: /usr/lib/libSystem.B.dylib
This issue is constantly present in any program compiled with -fopenmp, including the MWE
#include <stdio.h>
int main() {
#pragma omp parallel
printf("Hallo!\n");
return 0;
}
Note that the solution suggested in What is the "___emutls_get_address" symbol?, namely adding -lgcc_eh in the linking phase, does not work (I still get the same dyld error message).
I had exactly the same problem too.
And I am new to openMP, and my skill is not that good to use the solution offered by Michal Fapso.
I solve this problem by using
brew link --overwrite gcc
and the problem is solved!
Maybe you can try to reinstall gcc to your Mac.
I had exactly the same problem. In my case it was caused by linking against a library (I don't remember which one) of an older version of gcc installed by XCode, which was in /usr/lib. When I corrected it to link against the newer gcc library of the version I used for compiling (in /usr/local) this error was fixed.
So just check your built executable and all the libraries it is linked with using
otool -L EXECUTABLE_OR_DYLIB
And if you find anything linked with a library of an older gcc, fix that.

OpenCV compilation error with gcc 4.3 or above in MAC OS X

I want to use BRIEF and ORB in OpenCV and in order to use SSE4.2 (to speed up matching) in OpenCV, we need to compile OpenCV with gcc 4.3 or above and SSE4.2 option enabled.
I have no problem compiling OpenCV with gcc4.2.1 which is the default version in my Mac OS X 10.6.8.
Then, I have tried using Macports to install gcc4.3, 4.4, 4.5, 4.6, 4.7, 4.8 and switched gcc to the new one. I first tried installing OpenCV with default settings like instruction in OpenCV website. But I couldn't compile the code like when I used gcc4.2.1. Please note that, no problem with gcc4.2.1. Problems happen with gcc4.3 or above.
Could you please give me an advice where the problems come from? It seems that the errors come from compiler compatibility.
Some errors are:
OpenCV-2.4.0/modules/highgui/src/window_cocoa.mm: In function 'void cvDestroyAllWindows()':
OpenCV-2.4.0/modules/highgui/src/window_cocoa.mm:198:21: error: expected ';' before 'in'
OpenCV-2.4.0/modules/highgui/src/window_cocoa.mm:198:21: error: 'in' was not declared in this scope
OpenCV-2.4.0/modules/highgui/src/window_cocoa.mm:198:24: error: expected ';' before 'list'
In file included from /opt/local/include/libavformat/avformat.h:42:0,
from OpenCV-2.4.0/modules/highgui/src/ffmpeg_codecs.hpp:78,
from OpenCV-2.4.0/modules/highgui/src/cap_ffmpeg_impl.hpp:56,
from OpenCV-2.4.0/modules/highgui/src/cap_ffmpeg.cpp:45:
/opt/local/include/libavcodec/avcodec.h:554:1: internal compiler error: Segmentation fault
Please submit a full bug report,
The issue is that for(NSString *key in list) in window_cocoa.mm is an Objective-C 2 construct and is compiled as Objective-C++
gcc from GNU does not support the version 2 syntax and so cannot compile this. gcc4.2 from Apple includes Apple extensions including Objective C 2 but pure gcc does not.
You'll have to try the clang compiler.

Resources