Armadillo installation - c++

Hi I installed Armadillo3.0.1 in my own working directory /home/me/package/armadillo3.0.1/ as the README.txt said. But when I try an example:
g++ -I /home/me/package/armadillo3.0.1/usr/include/ example.cpp -o example -O1
It always shows the error:
/tmp/ccZAE9pj.o: In function void arma::gemm<false, false, false, false>::apply_blas_type<double>(arma::Mat<double>&, arma::Mat<double> const&, arma::Mat<double> const&, double, double)':
example.cpp:(.text._ZN4arma4gemmILb0ELb0ELb0ELb0EE15apply_blas_typeIdEEvRNS_3MatIT_EERKS5_S8_S4_S4_[void arma::gemm<false, false, false, false>::apply_blas_type<double>(arma::Mat<double>&, arma::Mat<double> const&, arma::Mat<double> const&, double, double)]+0x75e): undefined reference towrapper_dgemm_'
/tmp/ccZAE9pj.o: In function void arma::glue_times_redirect2_helper<true>::apply<arma::Mat<double>, arma::Mat<double> >(arma::Mat<arma::Mat<double>::elem_type>&, arma::Glue<arma::Mat<double>, arma::Mat<double>, arma::glue_times> const&)':
example.cpp:(.text._ZN4arma27glue_times_redirect2_helperILb1EE5applyINS_3MatIdEES4_EEvRNS3_INT_9elem_typeEEERKNS_4GlueIS5_T0_NS_10glue_timesEEE[void arma::glue_times_redirect2_helper<true>::apply<arma::Mat<double>, arma::Mat<double> >(arma::Mat<arma::Mat<double>::elem_type>&, arma::Glue<arma::Mat<double>, arma::Mat<double>, arma::glue_times> const&)]+0xe69): undefined reference towrapper_dgemv_'
example.cpp:(.text._ZN4arma27glue_times_redirect2_helperILb1EE5applyINS_3MatIdEES4_EEvRNS3_INT_9elem_typeEEERKNS_4GlueIS5_T0_NS_10glue_timesEEE[void arma::glue_times_redirect2_helper::apply, arma::Mat >(arma::Mat::elem_type>&, arma::Glue, arma::Mat, arma::glue_times> const&)]+0x1175): undefined reference to `wrapper_dgemv_'
collect2: ld returned 1 exit status
However if I add the -larmadillo option like:
g++ example.cpp -o example -O1 -larmadillo
It works by calling the old version. If I want to use new features in 3.0.1 like Mat.t(), it will also show the error:
example.cpp: In function ‘int main(int, char**)’:
example.cpp:20: error: ‘struct arma::mat’ has no member named ‘t’
This shows that by adding -larmadillo, it calls the old version armadillo from /usr/include/armadillo_bits/.
Any idea of that? Thanks.

In the first command, you are not linking with libarmadillo. Have you considered using the -L gcc option to add a link directory ? For example:
g++ -I/home/me/package/armadillo3.0.1/usr/include/
-L/home/me/package/armadillo3.0.1/usr/lib example.cpp -o example -O1 -larmadillo

I've also run into this issue. I've fixed it by uninstalling any previous versions of Armadillo, as they can interfere with the new version.
Uninstallation can be done via your package manager. You can also manually remove the old version via the following commands (in a terminal window):
rm /usr/include/armadillo
rm -rf /usr/include/armadillo_bits
You'll need to run the above commands as a super-user (root). On Ubuntu it should be sufficient to preface them with "sudo".
You may also need to remove the old library files, which could be in /usr/lib/ or /usr/lib64/, or something along these lines.
Once the old version has been removed, you can re-install the new Armadillo, via the cmake based installation.

I removed the old one and reinstalled the new one in default directory. It worked!
So it seems when I installed the old version with:
make install DESTDIR=my/own/dir/
it was install incorrectly.
Hence for newbies like me, install by default makes more sense.

Related

OpenCV: undefined reference to `cv::imread when using cmake

I have to use OpenCV on two systems, both Linux. My setup works on the first system, but fails on the second. For both systems I have installed OpenCV in a custom location using:
cd myBuildDir
cmake -DWITH_VTK=OFF -DCMAKE_BUILD_TYPE=Release
-DCMAKE_INSTALL_PREFIX=~/Software/OpenCVLibs/opencv-3.1.0/release
~/Software/opencv-3.1.0
make && make install
and similar for debug. The directories are different. One is with my user on my laptop, the other with a different user on a multiuser system.
I then used the example given in the OpenCV documentation to test my setup:
http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html
To build the example using my custom OpenCV location I used
cmake -DCMAKE_BUILD_TYPE=Release -DOpenCV_DIR=~/Software/OpenCVLibs
/opencv-3.1.0/release/share/OpenCV exampleSourceDir
This works on the first system (gcc version 5.3.0) but fails on the second (gcc version 4.6.3). Don't think the gcc version matter here, but stating them for completeness. I think it might be related to that on the first system there is no OpenCV in the system path installed, while in the second system there is. However the build fails with:
/usr/bin/cmake -E cmake_link_script CMakeFiles/DisplayImage.dir/link.txt --verbose=1
/usr/bin/c++ -O3 -DNDEBUG CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o -o DisplayImage -rdynamic /home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_videostab.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_videoio.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_video.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_superres.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_stitching.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_shape.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_photo.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_objdetect.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_ml.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_imgproc.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_imgcodecs.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_highgui.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_flann.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_features2d.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_core.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_calib3d.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_features2d.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_ml.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_highgui.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_videoio.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_imgcodecs.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_flann.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_video.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_imgproc.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_core.so.3.1.0 -Wl,-rpath,
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib
CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o: In function `main':
DisplayImage.cpp:(.text.startup+0xb8): undefined reference to `cv::imread(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
DisplayImage.cpp:(.text.startup+0x1d7): undefined reference to `cv::namedWindow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
DisplayImage.cpp:(.text.startup+0x1f4): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
DisplayImage.cpp:(.text.startup+0x223): undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
collect2: ld returned 1 exit status
From this call, I figure that the libraries in the custom location should have been used. Why does it fail then? I ran opencv_test_imgcodecs on both machines, which works fine.
Any ideas how to fix my problem? Is it because of the other OpenCV installation on the second system?
Thank in advance for any help!
I could solve my problem by changing the CMakeList.txt. I know have
find_package( OpenCV HINTS "~/Software/OpenCVLibs/opencv-3.1.0/release/share/OpenCV/" )
in it. Before it was just
find_package( OpenCV Required )
Now I also do not need to specify OpenCV_DIR any longer and can build with:
cmake -DCMAKE_BUILD_TYPE=Release exampleSourceDir
make
Most of the time though I think this a more general CMake mistake.
I just got the same error:
undefined reference to 'cv::imshow(cv::String const&, cv::_InputArray const&)'
And the fix that worked for me was changing my CMakeLists.txt from:
add_executable(rekf
src/rekf/rekf_node.cpp
src/rekf/ros_rekf.cpp
src/rekf/rekf.cpp)
add_dependencies(rekf ${catkin_EXPORTED_TARGETS})
target_link_libraries(rekf ${catkin_LIBRARIES})
To:
add_executable(rekf
src/rekf/rekf_node.cpp
src/rekf/ros_rekf.cpp
src/rekf/rekf.cpp)
add_dependencies(rekf ${catkin_EXPORTED_TARGETS})
target_link_libraries(rekf ${OpenCV_LIBS} ${catkin_LIBRARIES})

Boost:;program_options 1.49 - can't link with -lboost_program_options

I'm attempting my first use of Boost anything so I thought I'd start with program_options. I'm developing on a Raspberry Pi running Debian Wheezy. I started by "apt-get install libboost1.49-all" and everything seemed to install correctly. I can see .a and .so libraries in /usr/lib.
/usr/lib/libboost_program_options.a
/usr/lib/libboost_program_options-mt.a -> libboost_program_options.a
/usr/lib/libboost_program_options-mt.so -> libboost_program_options.so.1.49.0
/usr/lib/libboost_program_options.so -> libboost_program_options.so.1.49.0
/usr/lib/libboost_program_options.so.1.49.0
I can compile some example source I found here by using
g++ boost_program_options.cpp -c
but I cannot get anything to link. I've tried explicitly specifying the library path using no -l and got nothing but several pages of undefined reference errors. I tried another example code and got a compile problem that indicated to me that I wasn't using "g++ -std=c++0x" but that's not the problem either. I'm stuck. I've also tried
g++ -std=c++0x boostme.cpp -o boostme -L/usr/lib -lboost_program_options
I'm just banging my head against the wall at this point. Among the stackoverflow posts I've seen so far, I'm doing everything correctly. My head hurts. ;-)
Just some sample error messages below. Still poking around.
/tmp/ccTbmurt.o: In function `boost::program_options::error_with_option_name::~error_with_option_name()':
boostme.cpp:(.text._ZN5boost15program_options22error_with_option_nameD2Ev[_ZN5boost15program_options22error_with_option_nameD5Ev]+0x118): undefined reference to `vtable for boost::program_options::error_with_option_name'
/tmp/ccTbmurt.o: In function `boost::program_options::validation_error::validation_error(boost::program_options::validation_error::kind_t, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)':
boostme.cpp:(.text._ZN5boost15program_options16validation_errorC2ENS1_6kind_tERKSsS4_i[_ZN5boost15program_options16validation_errorC5ENS1_6kind_tERKSsS4_i]+0x30): undefined reference to `boost::program_options::validation_error::get_template(boost::program_options::validation_error::kind_t)'
Because the signature of that destructor really should be
~error_with_option_name() throw() {}
I'm going to look into my crystal ball and say that - maybe - somewhere you might have #define throw() or similar hidden in your codebase.
That, or you might have conflicting version of the header files in your include paths, which do not correspond to the version of the libraries found at link time

Google RE2 library cannot compile with 'make testinstall' in ubuntu

Gurus!
I am using Ubuntu 13.10 64-bit to compile latest Google RE2 library, but 'make testinstall' failed to compile, here is the log:
kevin#ubuntu:~/re2$ make testinstall
cp testinstall.cc obj
(cd obj && g++ -I/usr/local/include -L/usr/local/lib testinstall.cc -lre2 -pthread -o testinstall)
/tmp/ccSsaSXS.o: In function main':
testinstall.cc:(.text+0xce): undefined reference tore2::FilteredRE2::FirstMatch(re2::StringPiece const&, std::vector > const&) const'
/usr/local/lib/libre2.so: undefined reference to pthread_rwlock_rdlock'
/usr/local/lib/libre2.so: undefined reference topthread_rwlock_wrlock'
/usr/local/lib/libre2.so: undefined reference to pthread_rwlock_destroy'
/usr/local/lib/libre2.so: undefined reference topthread_rwlock_init'
/usr/local/lib/libre2.so: undefined reference to `pthread_rwlock_unlock'
collect2: error: ld returned 1 exit status
make: * [testinstall] Error 1
I tried to replace -pthread with -lpthread, still failed, then I dumped libre2.so and found that pthread_xxx is in it.
Here is the issue tracking in RE2 forum: https://code.google.com/p/re2/issues/detail?id=100
Anyone here have ever complied RE2 successfully ? Thank you!
See this comment:
Adding -pthread to LDFLAGS seems to fix make test (all tests are
passing), but not make testinstall.
That will get you to the next error
Depending on what you build it for 'make testinstall' might not be necessary.
I just needed to get python re2 port working, and this can be installed after running make install.
I encounter this problem before. Modify the makefile and use -lpthread instead of -pthread.
So I tried looking for the lines in testinstall.cc that were causing the symbol errors and I found out that the only line was on line 18:
18 - f.firstMatch(:abbccc:, ids);
I commented this line out (so that the FullMatch function below is still called) and just ran g++ testinstall.cc -lre2 -pthread -o testinstall (basically what the Makefile does) and I was able to get a binary successfully. Although this might not really solve the problem, its good to know that we can still use the RE2::Fullmatch and partial match functions
If I were to guess, maybe there is a dependency somewhere inside the filtered_re2 module?
I had the same problem. But if you compile with -static everything goes well.
nm -C shows that the "missing" symbol exists in both .a and .so files.

How to compile a c++ program in Linux?

I made a file hi.cpp and I wrote the command given below:
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! ";
cout << "I'm a C++ program";
return 0;
}
then I ran it in my RHEL 6 machine with the following command
gcc hi.cpp
and I got some errors which are as follows:
[chankey#localhost ~]$ gcc hi.cpp
/tmp/cc32bnmR.o: In function `main':
hi.cpp:(.text+0xa): undefined reference to `std::cout'
hi.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, const char*)'
hi.cpp:(.text+0x19): undefined reference to `std::cout'
hi.cpp:(.text+0x1e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, const char*)'
/tmp/cc32bnmR.o: In function `__static_initialization_and_destruction_0(int, int)':
hi.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::Init()'
hi.cpp:(.text+0x51): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cc32bnmR.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
[chankey#localhost ~]$
What do these errors denote? My code is correct then why am I getting errors?
Use g++
g++ -o hi hi.cpp
g++ is for C++, gcc is for C although with the -libstdc++ you can compile c++ most people don't do this.
As the other answers say, use g++ instead of gcc.
Or use make: make hi
You have to use g++ (as mentioned in other answers). On top of that you can think of providing some good options available at command line (which helps you avoid making ill formed code):
g++ -O4 -Wall hi.cpp -o hi.out
^^^^^ ^^^^^^
optimize related to coding mistakes
For more detail you can refer to man g++ | less.
Try this:
g++ -o hi hi.cpp
gcc is only for C
For a simple hello-world project, calling the compiler directly with g++ command or creating a make file are good options as already answered:
g++ -o hi hi.cpp
or
# After creating the makefile
make hi
For serious projects, however, the usage of a project manager is required. At the time I write this answer, the most used and open-source is cmake (an alternative could be QT qmake ).
Following is a simple CMake example:
Make sure you installed cmake on your linux distribution apt-get install cmake or yum install cmake.
Create a file CMakeLists.txt (the name is important) together with your source hi.cpp
project("hi")
add_executable( hi hi.cpp )
Then compile and run as:
cmake -B <path_to_build_folder> -S <path_to_source_folder>
cmake --build <path_to_build_folder>
cd <path_to_build_folder>; ./hi
This allows the project to scale easily with libraries, sources, unit-tests, and much more. It also makes most IDEs to understand the project properly (Most IDEs accept CMake natively, like kdevelop, qtCreator, etc..)
You could also generate Visual-Studio or XCode projects from CMake, in case you decide to port the software to other platforms in the future.
cmake -G Xcode . #will generate `hi.xcodeproj` you can load on macOS
$ g++ 1st.cpp -o 1st
$ ./1st
if you found any error then first install g++ using code as below
$ sudo apt-get install g++
then install g++ and use above run code
g++ -o foo foo.cpp
g++ --> Driver for cc1plus compiler
-o --> Indicates the output file (foo is the name of output file here. Can be any name)
foo.cpp --> Source file to be compiled
To execute the compiled file simply type
./foo
To Compile your C++ code use:-
g++ file_name.cpp -o executable_file_name
(i) -o option is used to show error in the code
(ii) if there is no error in the code_file, then it will generate
an executable file.
Now execute the generated executable file:
./executable_file_name

undefined referance to LibSerial

So i'm writing a serial transmision program, and have just changed over to using C++, it been a while since I used C++
(I've been working with C recently, and before that java)
Now I need to use LibSerial,
(it seems much simpler to use than C's termios)
my code is:
//gen1.cpp
#include "string2num.h" // a custom header
#include <iostream>
#include <SerialStream.h>
using namespace LibSerial;
//using namespace std;
int main(int argc, char*argv[])
{
if (argc<2)
{
std::cout<<argv[0]<<"requires the device name eg \"dev/tty0\" as a parameter\nterminating.\n";
return 1;
}
SerialStream theSerialStream(argv[1]); //open the device
return 0;
}
When I compile the output:
g++ -Wall -o gen1 gen1.cpp string2num.o
/tmp/cchPBWgx.o: In function `main':
gen1.cpp:(.text+0x121): undefined reference to `LibSerial::SerialStream::SerialStream(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::_Ios_Openmode)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x24): undefined reference to `LibSerial::SerialStreamBuf::showmanyc()'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x28): undefined reference to `LibSerial::SerialStreamBuf::xsgetn(char*, int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x2c): undefined reference to `LibSerial::SerialStreamBuf::underflow()'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x34): undefined reference to `LibSerial::SerialStreamBuf::pbackfail(int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x38): undefined reference to `LibSerial::SerialStreamBuf::xsputn(char const*, int)'
/tmp/cchPBWgx.o:(.rodata._ZTVN9LibSerial15SerialStreamBufE[vtable for LibSerial::SerialStreamBuf]+0x3c): undefined reference to `LibSerial::SerialStreamBuf::overflow(int)'
collect2: ld returned 1 exit status
make: *** [gen1] Error 1
This is the linker complaining that it cannot find the functions referenced by the libserial header file.
If I look on my Linux system to see how the shared library is called:
$ dpkg -L libserial0
...
/usr/lib/libserial.so.0.0.0
/usr/lib/libserial.so.0
On my system this implies I would add -lserial as a g++ option (aka link with libserial.so) this would turn your compilation command into
g++ -Wall -lserial -o gen1 gen1.cpp string2num.o
Including the header file is not enough - you also need to link with the library that implements SerialStream. Assuming it is a static library called serstream.a (it is almost certainly actually called something else):
g++ -Wall -o gen1 gen1.cpp string2num.o serstream.a
old thread, but i still use Libserial. here the completed answer
My working setup.
Ubuntu 18.04
g++ 7.3.0
1) Install package for libserial
apt install libserial-dev
2) check for your headers(.h) and .so files
dpkg -l libserial0
dpkg -l libserial-dev
the first command give you the directory of shared library and the second gives you the headers location.
3) Your code.
I have to change a little your code, first i delete the custom header and modifing the constuctor call to this.
SerialStream theSerialStream;
4) compile with g++
Here my compiling command
g++ -o test -I/usr/include test.cpp -L/usr/lib/x86_64-linux-gnu -lserial -lpthread
check for the -lpthread linking option, beacuse Libserial uses mutex.
In Ubuntu/Debian make sure you have to libserial-dev package installed and use the '-lserial' flag for gcc.