Issues including Eigen for simple C++ TensorFlow Lite test program - c++

I compiled the library for the C++ API for TensorFlow Lite (r1.97) using the script ${TENSORFLOW_ROOT}/tensorflow/lite/tools/make/build_rpi_lib.sh following the steps suggested at this official page (Native Compiling, downloading the necessary libraries), where ${TENSORFLOW_ROOT} is the root folder where I cloned the repository.
I am trying to compile this simple test.cpp program:
#include <memory>
#include "tensorflow/lite/interpreter.h"
int main(void)
{
std::unique_ptr<tflite::Interpreter> interpreter(new tflite::Interpreter);
}
using the command:
gcc-6 test.cpp -I${TENSORFLOW_ROOT} -I${TENSORFLOW_ROOT}/tensorflow/contrib/makefile/downloads/eigen -I${TENSORFLOW_ROOT}/tensorflow/contrib/makefile/downloads/protobuf/src -I${TENSORFLOW_ROOT}/tensorflow/contrib/makefile/downloads -L${TENSORFLOW_ROOT}/tensorflow/lite/tools/make/gen/rpi_armv7l/lib -lstdc++ -ldl -ltensorflow-lite
The list of includes was suggested in the Integrating TensorFlow libraries page (specifically from the section iOS). Compilation fails with the following error related to the inclusion of Eigen:
${TENSORFLOW_ROOT}/third_party/eigen3/unsupported/Eigen/CXX11/Tensor:1:42: fatal error: unsupported/Eigen/CXX11/Tensor: No such file or directory
#include "unsupported/Eigen/CXX11/Tensor"
I found several links where an apparently similar problem is discussed (such as this one), but the proposed solutions involve using references to the TensorFlow python package which is something that is not possible in my case (and it feels quite patchy - I am not considering using python for this project).
I also tried using a different include path to Eigen (e.g. ${TENSORFLOW_ROOT}/third_party/eigen3):
gcc-6 test.cpp -I${TENSORFLOW_ROOT} -I${TENSORFLOW_ROOT}/third_party/eigen3 -I${TENSORFLOW_ROOT}/tensorflow/contrib/makefile/downloads/protobuf/src -I${TENSORFLOW_ROOT}/tensorflow/contrib/makefile/downloads -L${TENSORFLOW_ROOT}/tensorflow/lite/tools/make/gen/rpi_armv7l/lib -lstdc++ -ldl -ltensorflow-lite
and also this causes Eigen related compilation errors of this sort:
...
${TENSORFLOW_ROOT}/third_party/eigen3/unsupported/Eigen/CXX11/Tensor:1:42: error: #include nested too deeply
#include "unsupported/Eigen/CXX11/Tensor"
...
${TENSORFLOW_ROOT}/third_party/eigen3/Eigen/Core:1:22: error: #include nested too deeply
#include "Eigen/Core"
...
Any suggestions on how to solve this issue? What is the right set of include paths?

Turns out I was including the wrong folder. Instead of ${TENSORFLOW_ROOT}/tensorflow/contrib/makefile/downloads/eigen or ${TENSORFLOW_ROOT}/third_party/eigen3, the right one is ${TFLITE_ROOT}/tensorflow/lite/tools/make/downloads/eigen.
I am still puzzled by the number of eigen folders inside the repository:
find . -name "eigen*" -type d
./third_party/eigen3
./tensorflow/lite/tools/make/downloads/eigen

Related

OpenCV on C++/Namespace issue?

I am new to C++ so I get some trouble to use openCV on my C++ project. I'm using Xcode as an IDE.
So I used brew to install opencv using the two command lines:
brew install opencv3 --with-ffmpeg --with-tbb --with-contrib
brew reinstall opencv3 --HEAD --with-python3 --with-ffmpeg --with-tbb --with-contrib
I checked the path to add to my project to load the library using recursivity, so I added on Xcode the path for header path and library:
/usr/local/Cellar/**
I also tried to install it another way, but still got the same issue:
brew install opencv
And adding the path to:
/usr/local/include/**
Everything seems to work since the library is detected, but import is not working because I got namespace errors in the openCV files, for instance:
No type named 'unique_ptr' in namespace 'std'
No member named 'allocator_traits' in namespace 'std'; did you mean 'allocator_arg_t'?
I checked on the internet and maybe it should be due to the the C++ language dialect or standard library, but I use GNU ++ 14 and libc++ . From what I found it should be working in that config, but I still got the issues. Do you have any ideas ?
EDIT: I don't even try to use it yet, I just used the include and print an hello:
#include <iostream>
#include "cv.h"
int main(int argc, char *argv[]){
std::cout<<"hello";
}
I also tried cv.hpp instead of cv.h, still not working
Thanks a lot !
I don't think you are using the correct #include paths, if you look at the OpenCV Example, you need the following for OpenCV 3.0 to open an image:
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
None of these are like the headers that you have, which are likely for older versions.
This tutorial looks like a very sensible one to get up and running with xcode, and the example at the bottom looks like a better start

<glad/glad.h>: No such file or directory

I'm following this tutorial to learn OpenGL, but I'm having trouble compiling since the compiler can't find one of the header files.
This is the file I'm trying to compile:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
int main() {
return 0;
}
To compile, I'm using
$ gcc -o sandbox sandbox.cpp -lGL -lGLU -lglut
and I get the following error:
sandbox.cpp:1:23: fatal error: glad/glad.h: No such file or directory
#include <glad/glad.h>
^
compilation terminated.
I followed the first two sections of this wiki
to install OpenGL and libraries.
I think the problem is either the wrong compile command or a flaw in my OpenGL installation.
GLAD is a function loader for OpenGL. This tutorial explains how to set it up.
The tutorial explains the purpose of GLAD:
Since there are many different versions of OpenGL drivers, the location of most of its functions is not known at compile-time and needs to be queried at run-time.
Setup of GLAD involves using a web server to generate source and header files specific to your GL version, extensions, and language. The source and header files are then placed in your project's src and include directories.
If you are looking at this simple GLFW example you can remove the glad/gl.h include, and the
gladLoadGL(glfwGetProcAddress);
line further down.
If you are on linux, ubuntu for example,
you don't need glad, just add these 2 headers instead:
#include <GLES2/gl2.h>
#include <EGL/egl.h>
If the example is saved as glfw_ex2.c you can compile it at the command line like this:
g++ glfw_ex2.c -lglfw -lGLESv2
Of course, linmath.h must be present in the same directory for this example.
If anything is missing, you can install it like this and try compiling again:
sudo apt install libglfw3-dev libgles2-mesa-dev libegl1-mesa-dev
sudo apt install build-essential
then run it like this:
./a.out

c++: How to remove libstdc++.so.6 dependencies

I have 2 program I wrote on my windows computer using Visual Studio 2013. They run fine and work perfectly on my computer, but when I brought them over to my school account that is on a Linux machine, a problem arose. They compile and 1 ran, but the other did not. The one that did not run gave me an error:
.../lib/compat/libstdc++.so.6: version CXXABI_1.3.2 required by...
I have been doing research and I can't seem to find out what in my program would be using libstdc++.so.6, I'm not even really sure what it is or does. Since I am on a student account I can't go installing it using sudo, and it is a homework so I can't submit it using my own libraries.
Any Idea on what my program might be using that would require libstdc++.so.6?
I have 3 files: main.cpp, LinkedList.cpp and LinkedList.h.
I think it might be in main.cpp because I think it stems from a library I am including and main.cpp is the only one that uses outside libraries. Here is the list of libraries it uses:
#include <iomanip>
#include <stdio.h>
#include <fstream>
#include <ctype.h>
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <bitset>
#include <algorithm>
#include "LinkedList.h"
Thanks in advance!
You are trying to run a program linked against one version of the libraries under another set. That should work fine as long as the library versions aren't too far apart. In your case, the difference between libraries is just too large.
GCC (C++ in particular) has changed quite a bit lately, some programs that used to compile and run fine now blow up or don't compile at all (due to language changes, compiler bugs accepting broken code, ...), and the library ABI has also changed. Your best bet is to carry source code around, and make sure you got compatible language versions on both ends. If that is inconvenient, a solution is to make sure you have the same compiler (and other environment) at both places. The easiest way to get this is to install the same distribution and version.
First you can't remove the dependencies of libstdc++.so.6, because it's a standard C++ library.
To solve your problem you have to check whether your libstdc++.so have the right version
strings /usr/lib64/libstdc++.so.6|grep GXXABI_1.3.1
if there have no matching version, you will have 2 methods like these:
update your gcc on your school's linux OS
yum intsall gcc
download a matching libstdc++.so from this website:
download gcc || download matching libstdc++
then replace the libstdc++.so to /usr/lib64/libstdc++.so.6.*
SOLUTION
I went through a few steps to find my solution. Originally I could compile my program but could not run it.
1) My first step to solve the issue was to change my method of compiling. Originally I compiled my program with the following: g++ main.cpp LinkedList.cpp -o output. I changed it to: g++ -static main.cpp LinkedList.cpp -o output which allowed me to compile and run. This worked but static is a method to dynamically link libraries. This prevents linking with the shared libraries. This is not a good solution because it takes a lot longer and increases the file size of the executable, so I wanted to improve.
2) The second thing I did was remove using namespace std. Yes, I cheated and used it. So I went through my program and added std:: to the appropriate places.
3) The last thing I did was clean up my code. I was using a lot of libraries because my program was a large and complicated program. I was using all of the libraries I had listed in my original post. I went through my code and anywhere I was using a function from a library I would try and write my own code that would do the same thing which would result in my program not depending on those libraries. I was able to replicate a decent amount of these dependent foreign functions with my own which added lot of code, but it allowing me to remove some of these includes. My list of includes is now:
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include "LinkedList.h"
#include <math.h>
I am not sure exactly which step resolved my issue, but now I can compile with my preferred method, g++ main.cpp LinkedList.cpp -o output, and my program runs fine.
I hope this helps someone.

Compile C++ with static lib

this will probably a dumb question for you guy's but I have no experience in C++ what so ever. I'm using an open source project osrm (which is awesome). Still to request a route, you have make an http request. To reduce the running time, I would like to build a wrapper around the code and call it using the command line. So I googled a bit and found that osrm already creates a static lib (.a file) when compiling the project. I also found a piece of code that points me in the right directions for building a wrapper. So to begin I build a simple hello world program (see below) that includes some files from that static lib. To compile I followed this tutorial.
My directory structure looks like this:
./helloWorld.cpp
./libs/libOSRM.a
And the command to compile is this:
gcc –static helloworld.cpp –L ./libs –l libOSRM.a
The code it selve:
#include "Router.h"
#include "boost/filesystem/path.hpp"
#include "ServerPaths.h"
#include "ProgramOptions.h"
#include <InternalDataFacade.h>
#include <viaroute.hpp>
#include <iostream.h>
main()
{
cout << "Hello World!";
return 0;
}
the exact error I got:
fatal error: ServerPaths.h: No such file or directory #include "ServerPaths.h"
Add the -IPathToTheHeaderFiles to the compiler options. So it will find the files to be included. Replace PathToTheHeaderFiles with the path where your file ServPaths.h resides.
Edit: Add as many -I as you need for further header files.
Additionally it would be worth to read a book about C++ or/and the GCC manual1
1 Section 3.11 will help.

Trying to install and use LAPACK++, problems with loading shared libraries

I am new to using libraries and I am having some trouble with lapack++ and getting it to work. I will explain what I have done and tried so far.
First I installed BLAS and LAPACK and that went fine. I now have installed LAPACK++ version 2.5.2 (http://lapackpp.sourceforge.net/) so I can call various linear algebra routines in C/C++. After I configure, make and then make install it places all the C/C++ header files in /usr/local/include/lapackpp/ some of which are..
arch.h
bmd.h
gmf.h
lapackc.h
lautil.h
spdmd.h
ultgmd.h
bfd.h
...
and also the following files in /usr/local/lib
liblapackpp.la
liblapackpp.so
liblapackpp.so.14
liblapackpp.so.14.2.0
Now if I try to compile using g++ the simple code of
#include <lapackpp/lapackpp.h>
using namespace std;
int main(int argc, char** argv) {
return 0;
}
I get the following output...
In file included from /usr/local/include/lapackpp/lapackc.h:14,
from /usr/local/include/lapackpp/lapack.h:10,
from /usr/local/include/lapackpp/lapackpp.h:16,
from test.cpp:1:
/usr/local/include/lapackpp/lacomplex.h:45:23: error: laversion.h: No such file or directory
/usr/local/include/lapackpp/lacomplex.h:48:17: error: f2c.h: No such file or directory
In file included from /usr/local/include/lapackpp/lapackpp.h:47,
from test.cpp:1:
/usr/local/include/lapackpp/latmpl.h:36:22: error: lafnames.h: No such file or directory
I solved this problem by writing the location of the header file explicitly in the header file that was causing trouble.
Eg. I replaced
#include
with
#include
After doing this my code compiles fine.
Now if I try to compile the code
#include <cstdlib>
#include <iostream>
#include <lapackpp/lapackpp.h>
using namespace std;
int main(int argc, char** argv) {
LaGenMatDouble A(5,5);
cout << "This is a test." << endl;
return 0;
}
by typing
g++ test.cpp -o test -I usr/local/include/lapackpp
I get the following errors
/tmp/ccAq6nkP.o: In function `main':
test.cpp:(.text+0x22): undefined reference to `LaGenMatDouble::LaGenMatDouble(int, int)'
test.cpp:(.text+0x4f): undefined reference to `LaGenMatDouble::~LaGenMatDouble()'
test.cpp:(.text+0x67): undefined reference to `LaGenMatDouble::~LaGenMatDouble()'
collect2: ld returned 1 exit status
(Info on LaGenMatDouble is here )
which suggests I may be linking to the library wrong?
After some googling I realised that I needed to link to the header files using -I and the shared library by -L and the library itself by -llapackpp, so then I typed
g++ test.cpp -o test -I usr/local/include/lapackpp -L usr/local/lib -llapackpp
which compiled the code, now when I ran the program by typing ./test I go the error
./test: error while loading shared libraries: liblapackpp.so.14: cannot open shared object file: No such file or directory
and now I am confused.
I am unsure if this has anything to do with the problem but when I type
pkg-config lapackpp --libs
I get
Package lapackpp was not found in the pkg-config search path.
Perhaps you should add the directory containing `lapackpp.pc'
to the PKG_CONFIG_PATH environment variable
No package 'lapackpp' found
The same happens for lapack and blas too.
I am unsure what to do. Any help would be very much appreciated, thanks!
Linking goes fine because you tell to the linker where the library is, but execution failed because the loader doesn't know anything about the location of your libraries (you can check that performing ldd yourapp, which shows the library needed by your application).
Usually, you can solve that by telling to the loader where the library is through the variable LD_LIBRARY_PATH, but it is a crude tool. A different solution is to encode that instruction directly in the executable, as described here, or simply to link statically your application using the switch -static
If you're after a C++ library that wraps LAPACK (and/or BLAS), you might be better off using a more modern library such as Armadillo. Besides using LAPACK as a backend for solvers and matrix factorizations, it uses expression templates to speed up operations.