How to solve the compiling problems when I use armadillo library' - c++

I'm new in C++ and I tried to use Armadillo to find the eigenvalues of a symmetrical matrix (eig_sym is the function's name). I used 'g++ -std=c++14 main.cpp -o app -O2 -larmadillo -llapack -lbas' and the exit was
error: ‘eig_sym’ was not declared in this scope
eig_sym( eigval, A );
^
eigval_sym.cpp:44:21: note: suggested alternative:
In file included from /usr/include/armadillo:443:0,
from main.cpp:6:
/usr/include/armadillo_bits/fn_eig_sym.hpp:118:1: note: ‘arma::eig_sym’
,but in the main.cpp file I wrote #include . When is the failure? Library path is not found maybe?

just #include <armadillo> is enough to get all functionality from the armadillo library, but do note that everything in armadillo is in the arma namespace. Therefore, you need to either add using namespace arma; in your cpp file or prepend all functions and classes in armadillo with arma::. That is, use arma::eig_sym instead of just eig_sym or add the using directive and continue to write just eig_sym.
This is the same thing you need to do with the standard library, which is in the namespace std. You need to write std::cout and not just cout, unless you add using namespace std;.

Related

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

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

'stoi' was not declared in this scope after using -std=c++11

Most probably this is weird, but when I got this error that stoi wasn't declared in this scope, I smiled because I am familiar with this error and it's solution.
I checked this option have g++ follow the c++11 ISO c++ language standard [-std=c++11] in compiler settings of Code Blocks (16.01, with MinGW) and tried recompiling it, but surprisingly it didn't work and the same error persisted. I tried re-installing CodeBlocks but that didn't work.
Also, I tried with windows power shell and command prompt with g++ math_handler.cpp -std=c++11 but got the same error.
What am I doing wrong?
the code is here:
#include<string>
using namespace std;
int main()
{
string body="456";
int i=stoi(body);
}
Note:
I tried with -std=c++0x and g++ too.
the same problem with to_string() function.
gcc version 4.9.2 (tdm -1)
Okay, I found that it is a known bug in MinGW bundled with CodeBlocks. I found the solution here.
Download mingw-to-string-gcc47.zip which contains three patched
header files. (Original patches: wchar.h, stdio.h, os_defines.h)
Copy wchar.h and stdio.h from the include directory in the zip file
to the following directory (overwrite): C:\mingw\include (replace
C:\mingw\ with the appropriate directory)
Copy os_defines.h to the following directory (overwrite):
C:\mingw\lib\gcc\mingw32\4.7.0\include\c++\mingw32\bits (replace
C:\mingw\ with the appropriate directory) (replace 4.7.0 with the
correct version number)
Did you include the required header file?
#include <string>
stoi is also in the std namespace so:
std::stoi()
or:
using namespace std;

geany: C++ Including libraries and headers

I'm very new in Ubuntu and programming C++ on Ubuntu using Geany.
The problem I have here is that:
the classes i want to iclude to my project will receive an error,
I type,
#include <vector>
the error given here is,
fatal error: vector: No such file or directory
also I cannot use namespace std,
typing using namespace std returns the following error,
error: unknown type name 'using'
Here is the code:
#include <stdio.h> //no problem here
#include "stdlib.h" //no problem here
#include <vector> //this is a problem (lets say it returns error 1)
using namespace std; //this is a problem (lets say it returns error 2)
int main(int argc, char **argv)
{
return 0;
}
This sounds like you are using the wrong compiler to compile your C++ code. For example, by invoking gcc test.cpp the C++ file is actually compiled as C and you receive errors such as the one you posted - there is no vector header in C and there is also no using keyword.
If you are using gcc, the correct way to invoke the compiler to compile C++ is via the g++ symlink, i.e. g++ test.cpp
If you are using clang, the executable is called clang++ instead.
Both compilers support the -x parameter to manually change the language to C++, although in that case you also have to specify that the compiler needs to link your files with the C++ standard library. For example: gcc -x c++ test.cpp -lstdc++

Getting fatal error for iostream

I wanted to write a simple clang plug-in. So I just executed a "PrintFunctionNames" plug-in provided in llvm-clang. But when i tried to execute a command :
" clang -cc1 -load ../../Debug+Asserts/lib/libPrintFunctionNames.so
-plugin print-fns some-input-file.c "
it gives me 1 fatal error :
fatal error: 'iostream.h' file not found
#include<iostream.h>
^
1 error generated.
I also tried using -I option providing a path for include directory of 'iostream' but it's still gives me the same error.
I tried it like:
'clang++ -I//usr/include/c++/4.6 -cc1 -load
../../../../Release+Asserts/lib/libPrintFunctionNames.so -plugin
print-fns ak.cpp '
So how do I make this work?
Don't add .h at the end.
#include <iostream>
That should fix it.
iostream belongs to C++ not C. So you should include it as
#include <iostream>
Additionally since you are programming in C++ you should name your source file ending with .cpp not .c to make it clear to the compiler and everyone else, that you want to use C++. Also you might need to invoke clang++ in your first compiler call (but I am not sure about that in context of plugins)
After the C++ language was standardized by the ISO, the header file named iostream.h was renamed to iostream. Change your program to use #include <iostream> instead and it should compile.
You will also need to add using namespace std; statement after each include (or prefix each reference to an iostream function/object with std::).
You can start by using this
#include <iostream>
using namespace std;
Once you are more comfortable with namespaces, you can remove the using statement & instead either use std::cout, std::cin etc or have a
using std::cout;
using std::cin;
etc.
I meet the same question,
template.cpp
g++ template.cpp
compare<int>com1(3,7);
List item
compare<double>com2(12.34,56.78);
compare<char>com3('a','x');
cout<<",the max value:"<<com1.max()<<endl;
cout<<",the max value:"<<com2.max()<<endl;
cout<<",the max value:"<<com3.max()<<endl;
return 0;
the question is up code segment composing not OK, use the shift + table typing next time.

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.