How to use external library in a LLVM pass - c++

I want to use boost graph library in one of the LLVM passes I am working on. Boost graph library is header only. So usually I can use it as -I<path to boost source>. My question is how to use it inside LLVM source tree (inside lib/Transforms/MyPass).
I tried the following.
First I tired adding include directory to my pass's CMakeLists.txt like so,
target_include_directories(LLVMMyPass "<path to boost headers>")
Next I tried to change the CPP_FLAGS,
set(CPP_FLAGS "${CPP_FLAGS} -I<path to boost headers> ")
Both didn't work. I keep getting the error.
fatal error: boost/graph/graph_traits.hpp: No such file or directory
#include "boost/graph/graph_traits.hpp"
Update : I tried using -DCMAKE_CXX_FLAGS=-I<path to boost> in cmake command. That also did not help.

system-wide installation of boost solved the problem
sudo apt-get install libboost-all-dev

Related

ROS getPath() command problematic when using ROS as a stand alone library

As the title mentions, I am using ROS as a stand-alone library in C++.
That means I import ROS as a lib and catkin is not used.
Now, I want to read a .yaml config file. One would expect this to work:
std::string pkg_loc = ros::package::getPath("sample_name");
It just seems to be problematic with ROS being used as a lib.
Directions to add catkin-related lines to my CMAKE file don't work and I keep getting this error:
error while loading shared libraries: librospack.so: cannot open shared object file: No such file or directory
Any ideas on a better way for me to do this?
Things I've tried so far:
I did include roslib in my set(ROS_LIBS ...) line of CMAKE.
I have this in my .bashrc export LD_LIBRARY_PATH=/opt/ros/noetic/lib
I do of course have #include <ros/package.h>
I'm using ROS Noetic, CMAKE 3.16, Ubuntu 20.

Why does CMake Fail to Link Libbitcoin C++?

I have recently installed CMake in order to write code to make use of Libbitcoin in C++ but I am having a hard time, I was trying to build the example code on GitHub here. And it haters been going terribly. I can't manage to link the library right in CMake, here is my code. I read and people were saying that I should try Autoconf but I have no idea how to even start that as I know nothing about Autoconf. I have CMake 3.16, and installed Libbitcoin with brew but alias were made in /usr/local/include for the library, I am on Mac OS X 10.15. The CMake runs fine but when running "make", it responds with:
Scanning dependencies of target CreateAddr
main.cxx:1:10: fatal error: bitcoin/bitcoin.hpp: No such file or directory
1 | #include <bitcoin/bitcoin.hpp>
| ^~~~~~~~~~~~~~~~~~~~~
Here is my CMake text:
Please all help is appreciated I am beyond lost.
It is hard to be sure without knowing the specifics of your installation, but it appears that your include directory paths may be overlapping with what is specified for the header in main.cxx. The include_directories() call tells the compiler to include headers from this directory:
/usr/local/include/bitcoin
Then, in main.cxx, you're including the file with bitcoin/bitcoin.hpp. Combining these suggests the file is located here:
/usr/local/include/bitcoin/bitcoin/bitcoin.hpp
The error states the header could not be found, so perhaps you meant to locate it here:
/usr/local/include/bitcoin/bitcoin.hpp
In that case, just remove the relative directory path from the main.cxx file, like this:
#include <bitcoin.hpp>
Also, you want to link to your libbitcoin library correctly. Using link_directories() is not recommended. Instead, you can specify the full path to your libbitcoin library directly in the call to target_link_libraries(). The library may not be located in /usr/local/include/bitcoin. With these changes, the last few lines in your CMake would look something more like this:
include_directories(/usr/local/include/bitcoin)
add_executable(CreateAddr main.cxx)
target_link_libraries(CreateAddr PUBLIC /your/path/to/libs/libbitcoin.so)

Compiling and Running CGAL Triangulation Demo

I am trying to use the CGAL library to display a 2D Delaunay triangulation, like in the example here
My code looks like this:
#include <iostream>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Delaunay_triangulation_2.h>
#include <CGAL/draw_triangulation_2.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Delaunay_triangulation_2<K> Triangulation;
typedef Triangulation::Point Point;
int main(void){
Point a(1,1), b(2,1), c(2,2), d(1,2);
Triangulation T;
T.insert(a);
T.insert(b);
T.insert(c);
T.insert(d);
CGAL::draw(T);
return 0;
}
When I try to compile this code with g++ -o cgalTest.exe cgalTest.cpp -lCGAL -lgmp the program compiles successfully, but on runtime I get Impossible to draw because CGAL_USE_BASIC_VIEWER is not defined
By searching on Google, I found someone that suggested using g++ -o cgalTest.exe cgalTest.cpp -lCGAL -lgmp -DCGAL_USE_BASIC_VIEWER which produces the following error on compile time: /usr/include/CGAL/Qt/Basic_viewer_qt.h:30:10: fatal error: QApplication: No such file or directory #include <QApplication>
I am using ubuntu 19.04, so I installed CGAl using sudo apt-get install libcgal-dev and sudo apt-get install libcgal-qt5-dev
I tried to install sudo apt-get install libqt5svg5-dev libqt5opengl5-dev as well to solve the error, but to no avail.
Do I need to install additional libraries? Maybe the compilation must be done differently?
Thank you
Ok, for anyone facing the same problem, here is how I solved it:
First, I used the locate QApplication command to find the location of the QApplication header file on my system. Be sure to run sudo updatedb before using locate. If locate doesn't find the location of QApplication then you are missing qt libraries. Try sudo apt-get install qt5-default and the other libraries I mentioned in my question, run sudo updatedb and try locate QApplication again.
When you find the path to QApplication just use the -I option to instruct the compiler to use it. Here is an example g++ -o delaunayTest delaunayTest.cpp -lCGAL -lgmp -lCGAL_Qt5 -DCGAL_USE_BASIC_VIEWER -I/usr/include/x86_64-linux-gnu/qt5/QtWidgets/ (because in my case, QApplication was inside the directory /usr/include/x86_64-linux-gnu/qt5/QtWidgets/)
Trying to compile with this, you will probably get another header file error. Repeat the same process using locate until you get no more header file errors.
At that point, you will likely encounter an undefined reference to symbol error.
To solve this, use locate again to find the location of the file that caused the error (for example libQt5OpenGL.so.5) and add the path to the compilation command as is (for example g++ -o delaunayTest delaunayTest.cpp /usr/lib/x86_64-linux-gnu/libQt5OpenGL.so.5) along with all the previous options.
You will probably get several undefined reference to symbol errors as well. Just keep using the same method until you don't get any.
At this point the program should compile properly and run properly.
Note that if you have multiple versions of qt installed, then the above might not work properly (If for example you have software that uses qt like MATLAB or anaconda installed in your system. You will know because locate will produce many paths for each file on the steps above). In such a case, I suggest building a Virtual Machine, downloading the CGAL libraries and qt5-default and following the above steps there, since it is very likely this won't work in a system with multiple qt installations.
Another option (maybe the easiest), using CMake, is to generate the file using the builtin script:
From the source file directory, run cgal_create_CMakeLists -c Qt5
Edit the generated CMakeLists.txt adding the line add_definitions(-DCGAL_USE_BASIC_VIEWER)
My generated and edited CMakeLists.txt:
# Created by the script cgal_create_CMakeLists
# This is the CMake script for compiling a set of CGAL applications.
cmake_minimum_required(VERSION 3.1...3.15)
project( tmp_cgal )
# CGAL and its components
find_package( CGAL QUIET COMPONENTS Qt5 )
if ( NOT CGAL_FOUND )
message(STATUS "This project requires the CGAL library, and will not be compiled.")
return()
endif()
add_definitions(-DCGAL_USE_BASIC_VIEWER) # <==== I've added this
# Boost and its components
find_package( Boost REQUIRED )
if ( NOT Boost_FOUND )
message(STATUS "This project requires the Boost library, and will not be compiled.")
return()
endif()
# include for local directory
# include for local package
# Creating entries for all C++ files with "main" routine
# ##########################################################
create_single_source_cgal_program( "b.cpp" )
Create a build directory: mkdir build
Change directory: cd build
Generate the build files: cmake ..
Build: make
Run the binary file. For me it's ./b because my source file was b.cpp
Environment:
These instructions should work for an Ubuntu 20.04.1 (or similar) with the packages libcgal-dev, libcgal-qt5-dev and qtbase5-dev installed (and, of course, cmake, make and g++).
Main references:
doc1 and doc2

error while loading shared libraries: libgsl.so.0: cannot open shared object file: No such file or directory

I use gsl.
After I compiled my .cpp file and run it, I faced with below error:
error while loading shared libraries: libgsl.so.0: cannot open shared object file: No such file or directory
I found same as this problem in:
https://groups.google.com/forum/#!topic/cortex_var/6vluX7pP0Sk
&
Linux error while loading shared libraries: cannot open shared object file: No such file or directory
&
http://www.gnu.org/software/gsl/manual/html_node/Shared-Libraries.html
And I have done as in the above links wrote but the error is still remained.
Can anyone help me?
To make it work do the following steps
Start Borne Shell
$LD_LIBRARY_PATH= path to your gsl lib folder inside the gsl installation folder
$export LD_LIBRARY_PATH
now run your executable
It should work fine.
First, you need to locate the file (libgsl.so.0). You can do this, for example, by using the find command:
sudo find / -name "libgsl.so.0"
Let us assume, the file is located in /usr/local/lib.
(If the file has not been found, install the corresponding package or download the source, build it and install it.)
Now, you have two options:
(1) Quick & Dirty:
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
export LD_LIBRARY_PATH
This adds the path of the library to an environment variable. The disadvantage of this option is, that it is only valid for the current session. It will not work for other users. It will not work once you log off and on again.
(2) Permanent:
Review your /etc/ld.so.conf. If /usr/local/lib is not listed there, add it. Now, run ldconfig to detect the shared object file and add it to some system-wide index.
I got the same error with Krita on Arch Linux. I made a symlink with
ln /usr/lib/libgsl.so /usr/lib/libgsl.so.0
and that fixed it.
In my experience, fastStructure depends on gsl 1.6 but not the latest version.
wget http://gnu.mirror.vexxhost.com/gsl/gsl-1.6.tar.gz
tar -zxvf gsl-1.6.tar.gz
cd gsl-1.16
./configure
make
sudo make install
Add these lines to your .bashrc file on your home directory.
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
export CFLAGS="-I/usr/local/include"
export LDFLAGS="-L/usr/local/lib"
then, run source ~/.bashrc to set these environment variables.
It works fine when I change the version from the latest to the 1.6.
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/shg047/software/gsl/lib/
such as: to-mr: error while loading shared libraries: libgsl.so.19: cannot open shared object file: No such file or directory
Have you tried updating your library? The program I was trying to run simply needed a newer version of gsl (I had 1.9.5 while it needed 2.0.0 or newer).
If you are on arch you can run:
yaourt gsl
and select the appropriate one.
You can use gsl-config --libs in you makefile or in the command line when you link the gsl library. Just type gsl-config you can find the options it offers to you. Choose the options you need, you will find compile and link process much easier than before. As a result, when I type gsl-config --libs in my terminal, I get -L/usr/local/lib -lgsl -lgslcblas -lm. Although it is very simple, first you should know where you gsl is installed. You can add the directory to the PATH environment variable or use the absolute path to execute gsl-config .
I needed libgsl.so.19:
/snap/inkscape/current/bin/inkscape: error while loading shared libraries: libgsl.so.19: cannot open shared object file: No such file or directory
I solved it with:
Installing Anaconda
searched for libgsl.so.19 and found it in ~/anaconda3/lib
run LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/anaconda3/lib (best add it to ~/.basrc)

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.