Pybind11 and Boost from C++ - c++

I want to use functions from Boost C++ Libraries (like gauss_kronrod.hpp) in my Pybind11 project to optimize python using C++.
Using C++ functions from standard libraries (like numeric) is straightforward:
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <numeric>
namespace py = pybind11;
.
.
.
PYBIND11_MODULE(_test, m) {
m.def("test_func", &test_func);
}
How can I access all the functions from Boost? I downloaded it and saved it in the folder thirdparty of my Pybind11 project. I can include a single function from Boost using to complete path to this function:
# include <thirdparty/boost/boost/math/quadrature/gauss_kronrod.hpp>
However func.hpp loads other functions from Boost which can not be found. I feel like I need to add the Path to Boost in my file CMakeLists.txt.
Under PyBind11: boost::multiprecision::cpp_int to Python is an example where cpp_int.hpp from Boost was successfully used within Pybind11. Unfortunately, its not clear how they made the command
#include <boost/multiprecision/cpp_int.hpp>
work properly

Related

Files/directories to include in Visual Studio C++ to use CUDA?

I want to use CUDA/GPU in OpenCV in Visual Studio. For example, cuda::GpuMat. I successfully build OpenCV with the extra modules with CUDA enabled
I tried the following code
#include <string>
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/photo/cuda.hpp>
#include <opencv2/photo.hpp>
using namespace std;
using namespace cv;
int main(){
string imageName("input.bmp");
//CPU version
Mat image = imread(imageName.c_str(), IMREAD_GRAYSCALE);
//CUDA version
cuda::GpuMat imageGPU;
cuda::GpuMat downloadGPU;
Mat buff;
imageGPU.upload(image);
downloadGPU.download(buff);
imwrite("gpu.bmp", buff);
return 0;
}
But I get an unhandled exception error.
I originally downloaded OpenCV in C:\Users\me\Downloads\opencv
I then downloaded and installed the latest OpenCV extra modules with CUDA on in
In Property Pages->C/C++->General->Additional Include Directories, I have:
C:\Users\me\Downloads\opencv\build\include\opencv
C:\Users\me\Downloads\opencv\build\include\opencv2
C:\Users\me\Downloads\opencv\build\include\
In Property Pages->Linker->General->Additional Library Directories, I have:
C:\Users\me\Downloads\opencv\build\x64\vc15\lib
and in Property Pages->Linker->Input->Additional Dependencies, I have:
opencv_world343d.lib
opencv_world343.lib
what else am I supposed to include so I can get GpuMat to work properly?
Most of the cases, yes, but you need to know which library you need to add, it may be cufft.lib, cublas.lib, cudnn.lib, etc. It depends of the function you use inside your code.
Opencv includes a cmake include file that would set all of this up for you if you use cmake to build you VS test project. This file will be in the root of the opencv install directory, i.e. after building opencv running cmake --install or the equivalent in VS. The file is OpenCVConfig.cmake, and it would be included in the CMakeLists.txt file for your project. Then you would call FindPackage(OpenCV), which would locate the OpenCV install, setup a few variables, which you would then use to link against your app.
I can post a sample CMakeList.txt file if you feel that would help.

Using boost library in Julia with Cxx.jl on Windows

I'm using Cxx with Julia 1.3.1 to use a function of the boost library, it works properly on Linux after the installation of such library, but in Windows i have never make it work. This is the module i wrote:
module Airyzero
#Returns zeros of Airy's function
using Cxx;
export airyzero
cxx"""
#include<iostream>
#include <boost/math/special_functions/airy.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
typedef boost::multiprecision::cpp_dec_float_50 float_type;
"""
cxx"""
double airyzero1(int y) {
return boost::math::airy_ai_zero<double>(y);
}
"""
airyzero(ind) = #cxx airyzero1(ind)
end
So i can use the function airy_ai_zero from boost as airyzero in my code. I need that works also in Windows because none of the computers of my lab uses Linux (neither my collegues).
I added to the header directory the path where boost was installed:
if Sys.iswindows()
const pathboost = "C:\\boost_1_73_0";
addHeaderDir(pathboost,kind=C_System);
end

C++ Boost and ImageMagick++ API issue

While developing Raspberry Pi library compiled by g++4.9.2 I have met compability issue between boost (1.6.2) and ImageMagick++ API (7.0). When compiling this code:
#include <Magick++.h>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
I got these boost errors:
__assert_fail was not declared in this scope (path_trails.hpp)
...
__assert_fail was not declared in this scope (path.hpp)
...
__assert_fail was not declared in this scope (shared_ptr.hpp)
When deleting #include <Magick++.h> line everything runs fine. Unfortunately I need boost and ImageMagick as well in this source file. How to solve this issue?
A simple workaround is to include Magick++.h after boost.
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <Magick++.h>
I don't have a proper explanation for this problem but it seems to come from a clash when using different headers including assert.h (I had the same problem using the assimp library and ImageMagick) but I don't get why the order matters.
Reproducing your case, assert.h is included in:
cassert for boost
MagickCore/pixel-accessor.h for Magick++.h
If someone has an explanation for this problem feel free to edit.

How to #include hash with ext, tr1, or __gnu_cxx in XCode, C++

I'm trying to work with the google-sparsehash library and I'd like to include the hash library described in the link,
using ext::hash; // or __gnu_cxx::hash, or maybe tr1::hash, depending on your OS
and I've tried one of each:
#include <ext/hash>
#include <ext>
#include <__gnu_cxx>
#include <tr1>
which none worked with XCode. I've also "using", where I was told that __gnu_cxx does not contain "hash". How do I describe this library to XCode (3.2.6) on OS X (10.6.8)?
Or more generally, where is this hash function described in a Mac / XCode?
In C++11:
#include <functional>
using std::hash;
In C++03 with TR1:
#include <tr1/functional>
using std::tr1::hash;
So far as I can tell, it doesn't seem possible to get at the hash functors without also pulling in definitions for the various hash tables. At least not without fooling around with library internal headers.
Try:
#include <ext/hash_map>
using __gnu_cxx::hash;
or:
#include <tr1/unordered_map>
using std::tr1::hash;

C++ search directories and files

Can someone tell me if there is a library built in C++ that I can use to get a list of files and directories? I've looked around and I see people using dirent.h but I need to download it(i think).
Thanks
P.S. I've looked at the fstream, but thats only for reading and outputting files as far as i know.
FORGOT TO MENTION. I DO NOT WANT TO DOWNLOAD ANYTHING, I JUST WANT TO SEE IF THERE IS A LIBRARY THAT IS BUILT WITHIN C++ THAT I CAN USE STRAIGHT OFF THE BAT. THIS IS FOR WINDOWS ASWELL
How about Boost::Filesystem? Supports directory iteration and is portable.
You can use Boost Filesystem library.
http://www.boost.org/doc/libs/1_31_0/libs/filesystem/doc/index.htm
Some nice samples are also provided at the link.
EDIT:
Without downloading a 3rd party library, there is no portable way to do it. For windows you can use CFileFind class from MFC.
Using namespace std::filesystem is available in visual studio 2017 you have in #include
#include <iostream>
#include <iostream>
#include <fstream>
#include <filesystem>
#include <chrono>
#include <thread>
#include <functional>
namespace fs = std::filesystem;
void Files_in_Directory();
fs::path path_Copy_Directory = "E:\Folder";
fs::path path_Paste_Directory = "E:\Folder1";
int main()
{
Files_in_Directory()
return 0;
}
void Files_in_Directory()
{
for (const auto & entry : fs::directory_iterator(path_Copy_Directory))
{
std::cout << entry.path() << std::endl;
}
}
Since others already mentioned boost::filesystem there are also other alternatives. Almost every C++ framework has some way to list directories and files. Like wxWidgets or poco and there are many more.
Regarding dirent.h. It is a standard C Posix library so on Posix compatible systems it should be available. For Windows you can also get it here and it includes instructions on how to use it.
After your edit:
On Windows you can use things like FindFirstFile (example here) and then you don't have to download anything. But it only works on Windows. It is not build in C++.