Compiler and linker flags in Rcpp: Magick++ - c++

I have an R script, which loads an Rcpp file. The Rcpp file needs the magick++ library, so I used:
#include <Rcpp.h>
#include <Magick++.h>
However when I run the script, I get:
fatal error: Magick++.h: No such file or directory
The library libmagick++-dev is installed.
How can I include Magick++ in an Rcpp file? How can I compile it with flags? For instance:
-DHAVE_CONFIG_H, -DMAGICKCORE_QUANTUM_DEPTH=16, -DMAGICKCORE_HDRI_ENABLE=0
A minimal working example:
mwe.r
library(Rcpp)
sourceCpp("./mwe.cpp")
mwe.cpp
#include <Rcpp.h>
#include <Magick++.h>
//[[Rcpp::plugins(cpp11)]]

As #hrbrmstr mentions, there is a CRAN package magick that provides ImageMagick functionality already; maybe that avoids the need to do anything yourself?
From these ImageMagick instructions you can see that a C++ program needs to know about compiler flags (the output of Magick++-config --cppflags --cxxflags) and linker flags (Magick++-config --ldflags --libs).
The best way to compile Rcpp code with complicated compilation and linkage steps is (I'm no expert here...) to create an Rcpp package
/tmp$ Rdev --vanilla -e "Rcpp::Rcpp.package.skeleton('Magick')"
/tmp$ cd Magick
I edited Magick/src/rcpp_hello_world.cpp to
#include <Rcpp.h>
#include <Magick++.h>
using namespace Rcpp;
// [[Rcpp::export]]
LogicalVector rcpp_hello_world()
{
Magick::InitializeMagick("RcppMagick");
return true;
}
And added package pre-processing, compilation, and linkage commands (following Writing R Extensions, available via RShowDoc("R-exts")) to a new file Magick/src/Makevars like
PKG_CPPFLAGS = `Magick++-config --cppflags`
PKG_CXXFLAGS = `Magick++-config --cxxflags`
PKG_LIBS = `Magick++-config --ldflags --libs`
I then updated the attributes of my package, installed it, and verified that it could be used
/tmp/Magick master$ Rdev --vanilla -e "Rcpp::compileAttributes()"
> Rcpp::compileAttributes()
>
>
/tmp/Magick master$ Rdev --vanilla CMD INSTALL .
* installing to library ...‘/home/mtmorgan/R/x86_64-pc-linux-gnu-library/3.5-Bioc-3.7’
* installing *source* package ‘Magick’ ...
** libs
g++ -I"/home/mtmorgan/bin/R-devel/include" -DNDEBUG `Magick++-config --cppflags` -I"/home/mtmorgan/R/x86_64-pc-linux-gnu-library/3.5-Bioc-3.7/Rcpp/include" -I/usr/local/include `Magick++-config --cxxflags` -fpic -g -O3 -Wall -pedantic -c RcppExports.cpp -o RcppExports.o
g++ -I"/home/mtmorgan/bin/R-devel/include" -DNDEBUG `Magick++-config --cppflags` -I"/home/mtmorgan/R/x86_64-pc-linux-gnu-library/3.5-Bioc-3.7/Rcpp/include" -I/usr/local/include `Magick++-config --cxxflags` -fpic -g -O3 -Wall -pedantic -c rcpp_hello_world.cpp -o rcpp_hello_world.o
g++ -shared -L/usr/local/lib -o Magick.so RcppExports.o rcpp_hello_world.o -L/usr/lib -L/usr/lib/X11 -lGraphicsMagick++ -lGraphicsMagick -ljbig -lwebp -llcms2 -ltiff -lfreetype -ljasper -ljpeg -lpng12 -lwmflite -lXext -lSM -lICE -lX11 -llzma -lbz2 -lxml2 -lz -lm -lgomp -lpthread
installing to /home/mtmorgan/R/x86_64-pc-linux-gnu-library/3.5-Bioc-3.7/Magick/libs
...
/tmp/Magick master$ Rdev --vanilla -e "Magick::rcpp_hello_world()"
> Magick::rcpp_hello_world()
[1] TRUE
I had to figure out what system package to install to get Magick++-config. When I first tried to install my package R complained that it didn't know about -lwebp, so I installed that system dependency too (libwebp-dev). The package would be good enough for personal use, but would need to be more robust for cross-platform or production use. There could be many problems encountered while trying to integrate this complicated library into R.

Related

Issues with libgcc_s_dw2-1.dll and libstdc++-6.dll on build

I know that these are required to compile a C++ app but what I don't know is how do I build my app so that other users won't need them. I tried to use -static flags to build but it still won't work when I remove mingw\bin\ and msys2\usr\bin\ from my path or when my friends who don't have a C++ compiler try to run it. For the record, I did include every library for the project when I asked for friends to run it.
Here's my Makefile :
rtx.exe: base.o objects.o rtx.o
g++ -O3 base.o objects.o rtx.o -o rtx -pthread -Lsrc\lib -lsfml-graphics -lsfml-window -lsfml-system -ljsoncpp -static -static-libgcc -static-libstdc++
rtx.o: rtx.cpp
g++ -Isrc\include -O3 -c rtx.cpp -static -static-libgcc -static-libstdc++
objects.o: objects.cpp objects.hpp
g++ -Isrc\include -O3 -c objects.cpp -static -static-libgcc -static-libstdc++
base.o: base.cpp base.hpp
g++ -Isrc\include -O3 -c base.cpp -static -static-libgcc -static-libstdc++
clean:
-rm *.o $(objects) rtx.exe
And here's one of the pop-op I (and my friends withour a C++ compiler) get :
There are 4 pop-ups, 2 of them being this one and the other says the same thing for libstdc++-6.dll.
I tried a bunch of things, including compiling objects.o and base.o into a library using the ar ru command but it gives the same pop-ups.
My guess is that one of the libraries I'm using, either jsonCpp or SFML is not built statically, but I couldn't find anything about how to fix it.
If you want to get rid of shared runtime libraries, you have to ensure that all your dependencies have linked the runtime statically.
SFML is by default not built with static runtime libraries, so you'll certainly have to rebuild SFML with SFML_USE_STATIC_STD_LIBS set in CMake. Similarly I assume for jsoncpp.
So, as advised by HolyBlackCat, I fully reinstalled MSYS2 and downloaded SFML and jsonCpp with mingw and after a bit of research and trial and error I ended up with this makeFile :
rtx.exe: base.o objects.o rtx.o
g++ -O3 base.o objects.o rtx.o -o rtx -pthread -lsfml-graphics-s -lsfml-window-s -lsfml-system-s -lopengl32 -lwinmm -lgdi32 -ljsoncpp -static
rtx.o: rtx.cpp
g++ -Isrc\include -O3 -c rtx.cpp -DSFML_STATIC -static
objects.o: objects.cpp objects.hpp
g++ -Isrc\include -O3 -c objects.cpp -DSFML_STATIC -static
base.o: base.cpp base.hpp
g++ -Isrc\include -O3 -c base.cpp -DSFML_STATIC -static
clean:
-rm *.o $(objects) rtx.exe
And now, it does make a static build.

Error linking IRAF library relocation R_X86_64_32 against can not be used

I'm trying to compile a program called DAOSPEC written in Fortran. It gives me the following error (among similar others):
/usr/bin/ld: /home/osboxes/iraf/bin.linux64//libimfort.a(imakwc.o): relocation R_X86_64_32 against `.bss' can not be used when making a PIE object; recompile with -fPIC
See the full log here.
How do I fix it?
My Makefile
FCOMP = gfortran
FFLAGS = -Wall -Wextra -fPIC -fmax-errors=1 -O3 -march=native -ffast-math -funroll-loops
.SUFFIXES: .o .f
.f.o:
$(FCOMP) -c $(FFLAGS) $<
default : daospec
daospec: daospec.o lnxsubs.o iosubs.o mathsubs.o bothsubs.o
$(FCOMP) -o daospec daospec.o lnxsubs.o iosubs.o mathsubs.o bothsubs.o -L/usr/local/lib/ -lcfitsio -lplotsub -ldevices -lutils -L/usr/lib/x86_64-linux-gnu/ -lX11 -L/home/YOUR_USERNAME/iraf/bin.linux64/ -limfort -lsys -lvops -L/home/YOUR_USERNAME/iraf/unix/bin.linux64/ -los -lf2c -lcurl
clean:
rm -rf daospec *.o
The same Makefile works on a different PC with Ubuntu 16.04 gfortran 5.4, but breaks on Ubuntu 18.04 gfortran 7.3. In both cases the IRAF library files are the same.
I have managed to solve the problem, with help from Vladimir F. Ubuntu 18.04 uses PIE, position independent executables (source), and thus it requires libraries to be built with -fPIC option. The libraries in the official IRAF distribution that I used were not build with -fPIC, and that's what caused my errors.
Fortunately, one can now install IRAF libraries from the iraf-dev package on Ubuntu 18.04:
sudo apt-get install iraf-dev
Alternatively, one can compile IRAF from Github's iraf-community/iraf repository with -fPIC option.
Lastly, I modified the Makefile to use the new locations of IRAF library files: /usr/lib/iraf/bin/ and /usr/lib/iraf/unix/bin/.
FCOMP = gfortran
FFLAGS = -Wall -Wextra -fPIC -fmax-errors=1 -O3 -march=native -ffast-math -funroll-loops
.SUFFIXES: .o .f
.f.o:
$(FCOMP) -c $(FFLAGS) $<
default : daospec
daospec: daospec.o lnxsubs.o iosubs.o mathsubs.o bothsubs.o
$(FCOMP) -o daospec daospec.o lnxsubs.o iosubs.o mathsubs.o bothsubs.o -L/usr/local/lib/ -lcfitsio -lplotsub -ldevices -lutils -L/usr/lib/x86_64-linux-gnu/ -lX11 -L/usr/lib/iraf/bin/ -limfort -lsys -lvops -L/usr/lib/iraf/unix/bin/ -los -lf2c -lcurl
clean:
rm -rf daospec *.o

Building and using a shared library from source files that use another shared library. (RInside)

I am beginner with building c++ applications and RInside (A library that provides embedded R interpreter access from C++ program) and need some help. I want to keep all code using RInside separated in a class/module and provide just a function (take some input, do some task using RInside and return the output) to other programs who need these data. I am experimenting if i can use this functionality in another project/module (Omnet++ veins if anyone wants specifically) which has so many other source files and separate makefiles. if possible i would like to not touch that modules and their compilation process. So i tried a simple example and have a doubt regarding building. I have RInside code in Test1 class and want to use it in Test2 class. As far as i understand i am using RInside as a shared library. So i need to build a shared library using Test1 and include or reference it in Test2.
Directory Shared_1:Test1.h , Test1.cc : class Test1
Directory shared_2: Test2.h , Test2.cc : class Test2
Test1.h -> Includes “Rinside.h”
Test1.cc -> includes “Test1.h”
-> main(): Create a Test1 object and call test1Function1()
->test1Function1() : Creates an embedded R instance and does some functionality
Test2.h -> Includes “Test1.h”
Test2.cc -> includes “Test2.h”
-> main() : Create a Test2 object and call test2Function1()
-> test2Function1() : Create a Test1 object and call test1Function1()
I Have created libTest1_1.so from Test1.cc like this.
g++ -I/usr/share/R/include -I/home/aelab/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include -I/home/aelab/R/x86_64-pc-linux-gnu-library/3.4/RInside/include -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g -Wall ./shared_1/Test1.cc -Wl,--export-dynamic -fopenmp -Wl,-Bsymbolic-functions -Wl,-z,relro -L/usr/lib/R/lib -lR -lpcre -llzma -lbz2 -lz -lrt -ldl -lm -lblas -llapack -L/home/aelab/R/x86_64-pc-linux-gnu-library/3.4/RInside/lib -lRInside -Wl,-rpath,/home/aelab/R/x86_64-pc-linux-gnu-library/3.4/RInside/lib -fPIC -shared -o ./shared_1/libTest1_1.so
I want to use test1Function1() from Test1.cc in Test2.cc and if possible compile Test2.cc with different options to g++. When i compile Test2.cc using All libraries(Libraries used for building Test1.cc and also libTest1_1.so), it works well.
g++ -I ./shared_1/ -I/usr/share/R/include -I/home/aelab/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include -I/home/aelab/R/x86_64-pc-linux-gnu-library/3.4/RInside/include -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g -Wall ./shared_2/Test2.cc -Wl,--export-dynamic -fopenmp -Wl,-Bsymbolic-functions -Wl,-z,relro -L/usr/lib/R/lib -lR -lpcre -llzma -lbz2 -lz -lrt -ldl -lm -lblas -llapack -L/home/aelab/R/x86_64-pc-linux-gnu-library/3.4/RInside/lib -lRInside -Wl,-rpath,/home/aelab/R/x86_64-pc-linux-gnu-library/3.4/RInside/lib -L./shared_1/ -lTest1_1 -fPIC -o ./shared_2/Test2.o
But when i compile it by just compiling it by providing include dir of Test1.h and the libTest1.so, it gives me error. If i understand correctly, g++ is trying to compile Test1 when i am building Test2. So it says it can not find RInside.h even though i am not including it directly in Test2.
g++ -I ./shared_1/ ./shared_2/Test2.cc -L./shared_1/ -lTest1_1 -fPIC -shared -o ./shared_2/Test2_3.o
In file included from ./shared_2/Test2.h:11:0,
from ./shared_2/Test2.cc:8:
./shared_1/Test1.h:12:74: fatal error: RInside.h: No such file or directory
#include
^
compilation terminated.
What i want to understand: 1) how to separate the code using RInside from another project/ module. 2) What i am doing wrong here.
I Have included some file here in google drive.
drive
I tried searching online but could not understand. I am definitely missing to understand something. Can anyone please help.
When you include test1.h from test2.h, you also include Rinside.h because test1.h includes it.
Do not include Rinside.h in test1.h but include it in test1.cc, this way you will have constructed your system in such a way that the only source file that is dependent on the Rinside library is test1.cc. Any other source that uses functionality from test1 will only be using whatever is inside test1.h, effectively creating the layer you need to abstract RInside.

FLTK - Can't find headers even if I include the correct directory [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
I am trying to compile a FLTK project from linux, using the output of the commands:
fltk-config --use-gl --use-images --ldflags
And:
fltk-config --use-gl --use-images --cxxflags
I am using this makefile to compile the project:
I installed the latest FLTK version by just downloading it from fltk.org, and then I compiled and installed it. I also installed X11 and OpenGL. But I still get these errors:
I checked the file system and these headers are in /usr/local/include/FL even if they are not recognized, although I included the directory (-I/usr/local/include) and then in the .cpp and .h files:
#include <FL/Fl_PNG_Image.H>
#include <FL/Fl_Box.H>
etc...
What could be the problem? do I need to install more libraries?
EDIT
I tried to change the makefile:
CXXFLAGS=-I/usr/local/include -I/usr/local/include/FL/images -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT -I/home/ramy/boost_1_63_0 -std=c++11
LDFLAGS=-L/usr/local/lib -lfltk_images -lfltk_png -lz -lfltk_jpeg -lfltk_gl -lGLU -lGL -lfltk -lXrender -lXcursor -lXfixes -lXext -lXinerama -lpthread -ldl -lm -lX11 -L/home/ramy/boost_1_63_0/lib
SOURCES=Car.cpp Map.cpp CarState.cpp Utilities.cpp CarCollection.cpp TableView.cpp MapView.cpp
OBJECTS=Car.o Map.o CarState.o Utilities.o CarCollection.o TableView.o MapView.o
make:
g++ $(LDFLAGS) $(CXXFLAGS) -c $(SOURCES)
g++ $(LDFLAGS) $(CXXFLAGS) -o ../Evolution table-example.cpp $(OBJECTS)
clean:
rm -f $(OBJECTS) ../Evolution
But I still get the same errors.
Those errors don't mean that you are lacking headers, but libraries.
Here you can read the basis (go down to "Compiling Programs with Standard Compilers").
Basically add -L/usr/local/lib -lfltk -lXext -lX11 -lm to your app compiler command; or use fltk-config --cxxflags.

R package with c++ code failed installation, No DLL was created

I'm currently working on an R package which uses C++ code and includes external libraries (dlib, boost and an optimization library developed in the group). We're using Rcpp to integrate R and C++, but the problem is the package always fails to compile, and none of the similar questions I've found out there have worked for me.
The report generated by R CMD check is:
* installing *source* package 'IRTppExperimental' ...
** libs
*** arch - i386
c:/Rtools/mingw_32/bin/g++ -std=c++0x -I"C:/PROGRA~1/R/R-33~1.1/include" -DNDEBUG -I"C:/Users/Camilo/Documents/R/win-library/3.3/Rcpp/include" -I"d:/Compiler/gcc-4.9.3/local330/include" -I../src/include -O2 -Wall -mtune=core2 -c RcppExports.cpp -o RcppExports.o
c:/Rtools/mingw_32/bin/g++ -std=c++0x -I"C:/PROGRA~1/R/R-33~1.1/include" -DNDEBUG -I"C:/Users/Camilo/Documents/R/win-library/3.3/Rcpp/include" -I"d:/Compiler/gcc-4.9.3/local330/include" -I../src/include -O2 -Wall -mtune=core2 -c rcpp_hello_world.cpp -o rcpp_hello_world.o
c:/Rtools/mingw_32/bin/g++ -std=c++0x -I"C:/PROGRA~1/R/R-33~1.1/include" -DNDEBUG -I"C:/Users/Camilo/Documents/R/win-library/3.3/Rcpp/include" -I"d:/Compiler/gcc-4.9.3/local330/include" -I../src/include -O2 -Wall -mtune=core2 -c test_multi.cpp -o test_multi.o
c:/Rtools/mingw_32/bin/g++ -shared -s -static-libgcc -o IRTppExperimental.dll tmp.def RcppExports.o rcpp_hello_world.o test_multi.o -LC:/PROGRA~1/R/R-33~1.1/bin/i386 -lRlapack -LC:/PROGRA~1/R/R-33~1.1/bin/i386 -lRblas IRTppExperimental.dll -L../src/include -Ld:/Compiler/gcc-4.9.3/local330/lib/i386 -Ld:/Compiler/gcc-4.9.3/local330/lib -LC:/PROGRA~1/R/R-33~1.1/bin/i386 -lR
g++.exe: error: IRTppExperimental.dll: No such file or directory
no DLL was created
ERROR: compilation failed for package 'IRTppExperimental'
* removing 'C:/Users/Camilo/Documents/UNAL/MIRT/Tests/Temps/IRTppExperimental.Rcheck/IRTppExperimental'
And the Makevars/Makevars.win files are as follows:
INCFOLDER = ../src/include
PKG_LIBS += $(LAPACK_LIBS) $(BLAS_LIBS) $(FPICFLAGS) $(SHLIB)
PKG_LIBS += -L$(INCFOLDER)
PKG_CXXFLAGS+=-I$(INCFOLDER)
CXXFLAGS+="-fno-stack-protector"
CXX_STD = CXX11
Lastly, the NAMESPACE file reads:
exportPattern("^[[:alpha:]]+")
importFrom(Rcpp,sourceCpp)
useDynLib(IRTppExperimental)
All the c++ source files are inside the src folder, including the external libraries. The package was created using the Rcpp.package.skeleton function, and used the compileAtributes function to create the RcppExports source files.
EDIT: As pointed out by Coatless, here is github link of the project: https://github.com/SICSresearch/IRTpp/tree/Uni-Multi-Merging