How to edit include path in g++ when using 'R CMD Install --library=**my path** myfile.tar.gz'? - c++

I have an R package built in Rstudio, which contains a lot of c++ files. After successfully building the R package, I try to install it using the command R CMD INSTALL --library=**my path** myfile.tar.gz in the terminal.
Since I have a lot of c++ code in my R package, g++ tries to compile this code but could not find certain header files.
Is there a way to point the g++ compiler to the location of my header files, I tried using PKG_CXXFLAGS but in vain.

Related

Installing opencv From Command Line (Windows)

I am trying to use opencv in a project, and am running into problems 'installing' it. I have extracted the opencv files and have created a small test program:
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
int main(int argc, char **argv){
cv::Mat im=cv::imread((argc==2)? argv[1]: "testing.jpg",1);
if (im.empty()){
std::cout << "Cannot open image." << std::endl;
} else {
cv::imshow("image",im);
cv::waitKey(0);
}
return 0;
}
To compile the program I have used the command below:
g++ -I"../../PortableGit/opt/opencv/build/include/" -L"../../PortableGit/opt/opencv/build/x64/vc15/lib" main.cpp -lopencv_core -lopencv_highgui -o main
I get the errors below:
In file included from ../../PortableGit/opt/opencv/build/include/opencv2/core.hpp:3293:0,
from ../../PortableGit/opt/opencv/build/include/opencv2/highgui.hpp:46,
from ../../PortableGit/opt/opencv/build/include/opencv2/highgui/highgui.hpp:48,
from main.cpp:1:
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:714:14: error: 'recursive_mutex' in namespace 'std' does not name
a type
typedef std::recursive_mutex Mutex;
^~~~~~~~~~~~~~~
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:715:25: error: 'Mutex' is not a member of 'cv'
typedef std::lock_guard<cv::Mutex> AutoLock;
^~
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:715:25: error: 'Mutex' is not a member of 'cv'
../../PortableGit/opt/opencv/build/include/opencv2/core/utility.hpp:715:34: error: template argument 1 is invalid
typedef std::lock_guard<cv::Mutex> AutoLock;
I believe that it has something to do with mingw binaries no longer being included with opencv. I am missing the opencv/build/x86/mingw directory.
My questions are:
How do I 'install' opencv and use it without also installing some sort of IDE and/or CMake? (I prefer to use vim and the command line.)
Once installed, what command do I use to compile and link a program with opencv?
Any help is appreciated.
Edit:
This appears to be a problem with GCC's implementation of threads on windows. Using mingw-w64 instead of mingw fixed the std::recursive_mutex issue, but now the linker cannot find the proper files.
/i686-w64-mingw32/bin/ld.exe: cannot find -lopencv_core
/i686-w64-mingw32/bin/ld.exe: cannot find -lopencv_highgui
After quite a bit of trying things out, this is what I got to work. Oddly, following the LINUX guide to install opencv worked better than the WINDOWS guide, even though I have a windows computer.
Guide to Installing OpenCV on Windows Without VS
Heads-up: This is a multi-step process, 3 separate tools are required. Be prepared for this to take a while.
Part 1: Get everything ready
Download MinGW-w64.
On the downloads page, click on the "MinGW-w64-builds" option. Do not click on the "win-builds" option.
The reason MinGW-w64 has to be used is because it is a newer version of the MinGW compiler suit that has been improved for windows. This means that it supports the posix thread system, where as the standard MinGW compiler only supports the win32 thread system. OpenCV relies on the posix thread system, necessitating the MinGW-w64 compiler.
Extract the MinGW-w64 zip folder to a directory. In my case its PortableGit/opt/MinGW-w64
At this point, you can add the MingGW-w64/mingw32/bin folder to your path. (Assuming that this won't cause any conflicts.) If you do so, you will not have to constantly specify the g++ executable directory to run it. This is up to your discretion.
Download an opencv release.
Do not download the package for windows, click the button that says "sources"
Extract the opencv sources zip folder to a directory. In my case its PortableGit/opt/opencv-4.3.0
Also download the opencv_contrib source files directly from the repository.
Extract that folder and place it inside the top level opencv folder: PortableGit/opt/opencv-4.3.0/opencv_contrib in my case.
Download CMake.
I downloaded the zip folder, but you can download the installer if you wish.
Extract the CMake zip folder if you downloaded that, or run the installer. I put my CMake folder here: PortableGit/opt/cmake-3.17.1-win32-x86
At this point, you can add the cmake-3.17.1-win32-x86/bin folder to your path. (Assuming that this won't cause any conflicts.) If you do so, you will not have to constantly specify the cmake executable directory to run it. This is up to your discretion.
Part 2: Build OpenCV
Navigate to the opencv directory and create a build folder and cd into it.
mkdir build && cd build
Run the following export commands.
export CC=/PortableGit/MinGW-w64/mingw32/bin/gcc.exe
export CXX=/PortableGit/MinGW-w64/mingw32/bin/g++.exe
This is to make sure the next cmake command uses the proper compilers.
Run the following cmake command from within that folder:
PortableGit/opt/cmake-3.17.1-win32-x86/cmake.exe -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release -DOPENCV_VS_VERSIONINFO_SKIP=1 -DOPENCV_EXTRA_MODULES_PATH="/PortableGit/opt/opencv-4.3.0/opencv_contrib/modules/" ..
The -G flag specifies that we are creating build files for the MinGW compiler
The -DCMAKE_BUILD_TYPE=Release specifies that we are building the release version of opencv and not the debug version.
The DOPENCV_EXTRA_MODULES_PATH needs to be set to the modules folder inside the opencv_contrib folder. For me it was PortableGit/opt/opencv-4.3.0/opencv_contrib/modules
The DOPENCV_VS_VERSIONINFO_SKIP specifies to not include version info. If not set, the compiler will throw an error complaining about not having version files. (Shown below for reference.)
gcc: error: long: No such file or directory
mingw32-make[2]: *** [modules\core\CMakeFiles\opencv_core.dir\build.make:1341:
modules/core/CMakeFiles/opencv_core.dir/vs_version.rc.obj] Error 1
If successful, the cmake command will finish like this:
Now run this command, again from the build folder: /PortableGit/opt/MinGW-w64/mingw32/bin/mingw32-make.exe -j7
mingw32-make.exe is the windows equivalent of the Linux make command.
The -j7 option run the process with a maximum of 7 threads.
This will take a while! It took my laptop ~20 minutes to complete
If the make command ends in an error, make sure to reset your build directory before continuing any troubleshooting. This is done through this series of commands
rm -rf build
mkdir build
cd build
Part 3: Using OpenCV
To use the opencv library that you just compiled in a project of your own, compile the project with these flags from your projects main directory.
Remember that your compiler now has to be set to the mingw-w64 compiler for opencv support.
I added indentation and newlines for readability, but when entering this in the terminal do not include the newlines or indents.
The number at the end of the linker options may change depending on the version of opencv you downloaded. I downloaded opencv-4.3.0, making my number 430, but yours may be different.
PortableGit/opt/MinGW-w64/bin/g++.exe
-I../../PortableGit/opt/opencv-4.3.0/include/
-I../../PortableGit/opt/opencv-4.3.0/build/
-I../../PortableGit/opt/opencv-4.3.0/modules/core/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/calib3d/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/dnn/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/features2d/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/flann/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/gapi/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/highgui/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/imgcodecs/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/imgproc/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/ml/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/objdetect/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/photo/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/stitching/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/ts/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/video/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/videoio/include/
-I../../PortableGit/opt/opencv-4.3.0/modules/world/include/
-L../../PortableGit/opt/opencv-4.3.0/build/lib/
*.hpp
*.cpp
-lopencv_calib3d430
-lopencv_core430
-lopencv_dnn430
-lopencv_features2d430
-lopencv_flann430
-lopencv_highgui430
-lopencv_imgcodecs430
-lopencv_imgproc430
-lopencv_ml430
-lopencv_objdetect430
-lopencv_photo430
-lopencv_stitching430
-lopencv_video430
-lopencv_videoio430
-o
main
Or you could download VS. Up to you. Hope this helps.
Correction for JackCamichael's answer
those 2 commands won't work in Windows
export CC=/PortableGit/MinGW-w64/mingw32/bin/gcc.exe
export CXX=/PortableGit/MinGW-w64/mingw32/bin/g++.exe
This should be
setx -m CC C:\msys64\mingw64\bin\gcc.exe
setx -m CXX C:\msys64\mingw64\bin\g++.exe
C:\msys64\mingw64\bin is mingw64 path on my machine

Compilation failed for package with Rcpp

I have a .cpp file which can be compiled by sourceRcpp() successfully. Now I use Rcpp.package.skeleton() for it, and it generates the related directory. Then I run the code R CMD Install on the command line, but it showed a warning with an ERROR:
compilation failed for package...
I'm Windows user and use RStudio. I've followed some advice and found the file Makevars.win, it has only one line
PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
Maybe I need to add Rcpp:::LdFlags() or something. I've already install Rtools before because I can compile and install my own packages without using Rcpp.
Could you give me some advice how to deal with it?
If you are in RStudio already do this:
File -> New Project -> New Directory -> R Package
Important: Switch the toggle in 'Create Package' from 'R Package' to 'R Package w/Rcpp'.
Build the package. It will work, just like Rcpp.package.skeleton().
Copy your source file into src/.
Build the package again. This will automagically run compileAttributes() for you which is what you failed to do manually.
All this is documented.

mingw32-make opencv cmake 3rdparty,the pathname of adler32.c.obj was unfound

I want to Install OpenCV for codeblocks for C++
I follow this tuto:
http://kevinhughes.ca/tutorials/opencv-install-on-windows-with-codeblocks-and-mingw/
After the build of openCV with cmake, I use the cmd command.
when I do:
C:\opencv\buid\x86\mingw>mingw32-make.exe
there is a problem in the:
C:\opencv\build\x86\mingw\3rdparty\zlib\CMakeFiles\zlib.dir
==> the file "adler32.c.obj is unfounded.
For sure, this file doesn't exist.
How can I get this file ?
your answer could be really helpfull, I hope there is a solution to solve it

Libusb and how to use its packages in Ubuntu

I have installed libusb by using the following command. I am not sure if it was right or not and the command was
sudo apt-get install libusb-dev
Once I have installed (and I am not sure if it has installed or not because I am a novice user of Ubuntu), I want to know how would I use the library, because I write some sample code which uses <libusb.h>, but when I compile that C++ file using
g++ test_libusb.cpp
that throws the following error,
test_libusb.cpp:2:20: fatal error: libusb.h: No such file or directory compilation terminated.
I am clueless what to do. I can't find any source on the Internet to get to the bottom of this...
I want to know two things here:
How do I add the libusb library in C/C++ so I can use <libusb.h>?
What would some sample code be? Only a few lines to see if libusb is working...
Try including it like so:
#include <libusb-1.0/libusb.h>
and then compile it like so:
g++ main.cpp -o main -lusb-1.0
Have a look at http://packages.debian.org/wheezy/i386/libusb-dev/filelist: The file you want to include is usb.h. Also, you'll have to tell the compiler where it can find the compiled library functions: Add -lusb to the compiler command line to make it load libusb.so.
Actually at least in Debian 7.4 (wheezy), and probably in Ubuntu also, there are two distinct libusb packages: libusb-dev (0.1.12-20+nmu1) and libusb-1.0-0-dev (1.0.11-1). Confusingly, they can both be installed concurrently and provide header files in different locations:
$ dpkg -L libusb-dev|grep /usr/include
/usr/include
/usr/include/usb.h
$ dpkg -L libusb-1.0-0-dev|grep /usr/include
/usr/include
/usr/include/libusb-1.0
/usr/include/libusb-1.0/libusb.h
Try #include <usb.h>. The "lib" is part of the Linux naming convention, i.e. library "foo" has header foo.h and is called libfoo-dev in the Debian package structure, and linked as -lfoo, and the compiled library files are called libfoo.a and libfoo.so.

Makefile which link to library in C++

Recently, I try to create an makefile as below:
all:
g++ test.cpp -o Test
It creates an runnable Test. However, If I try to link it to an library (as LibCurl to use SSL connetion):
all:
g++ test.cpp -o Test -lcurl
It goes wrong (I already installed libcurl4-openssl-dev package as standard library)!
What does I miss? Can you give any solution? Thanks!!
Edit: The error is below:
fatal error: curl.h: No such file or directory
I guess, you need fix your sources:
#include <curl/curl.h>
The header files are missing in the include search path.
You will have install the development package of libcurl.
For example, install libcurl-devel-7.19.0-11.1.x86_64.rpm for Open Suse 11.1.
To know the list of files that would be installed by the rpm, download the rpm, type the following command in the downloaded dir.
rpm -qlp <rpmname>
As per the example above, the command would be
rpm -qlp libcurl-devel-7.19.0-11.1.x86_64.rpm
This will list the files along with the path in which it would be installed to.
In our example, the result would look like..
/usr/include/curl /usr/include/curl/curl.h /usr/include/curl/curlbuild.h
/usr/include/curl/curlrules.h
/usr/include/curl/curlver.h and so on...
and
Install the development libcurl package for the version of linux you use and include #include <curl/curl.h> in your code.
Refer http://curl.haxx.se/download.html to download the right one