Linking and compiling armadillo in Xcode 4 - c++

I am a Mac OS X 10.8.3 user (Mountain Lion). I have download armadillo and installed it in OS X as per README.txt instructions. It compiles fine by typing:
g++ ... *stuff* ... -O2 -larmadillo
in the terminal. By including the same linker arguments in Eclipse CDT, I have been able to compile armadillo in Eclipse (Juno) in OS X. However, my problem is when it comes to compiling in Xcode 4 -- the program I need to have armadillo in! I have already tried adding the "-O2 -larmadillo" string to the other linker flags. I also tried adding /usr/local/lib and /usr/local/include/ in Header Search Paths and Library Search Paths. I have gotten Xcode 4 to thus recognize the command:
# include < armadillo >
or
# include "/usr/local/include/armadillo"
HOWEVER, when it comes to compile time Xcode 4 does NOT recognize a line like:
mat A;
So for the code:
# include < iostream >
# include < armadillo >
using namespace std;
int main() {
mat A;
return 0;
}
I get errors like:
Use of undeclared identifier 'mat'; did you mean 'max'?
Expected ';' after expression
Use of undeclared identifier 'A'
Reference to overloaded function could not be resolved; did you mean to call it?
Note that the code does not even get to the compile stage!
I would like to now : how do I properly link and compile a C++ code which uses the armadillo library in Xcode 4???
Thank you very much!
Daniel.

did you forget to add "using namespace arma"? ie.
# include <iostream>
# include <armadillo>
using namespace std;
using namespace arma; // make Armadillo classes and functions visible by default

Related

Linker hangs when compiling a simple eigen3 program on Win10

I am using the build tools provided by Rtools.
gcc version 8.3.0 x86_64-w64-mingw32 (Built by Jeronen for the R-project)
GNU ld version 2.33.1
eigen version 3.4.0
I have been testing various functions of the eigen package, and when I calculated the singular values using the BDCSVD object, the linker just hangs with full cpu usage. The following line is causing the trouble:
BDCSVD<MatrixXd> svd(m, ComputeThinU | ComputeThinV);
This line of code compiles fine. Just the particular linker does not return. I have waited a few minutes for the linker, but it just won't return.
On the other hand, when I used the latest gcc from the Windows Linux subsystem. Everything worked fine. So is this a known issue? Can it be easily fixed (but still using the build system provided by Rtools in Windows)?
PS: I have encounted this issue several times before, but the cpp files were much more complex, so I wasn't sure back then what caused the hanging linker.
Update:
A code sample is provided below. I configured my Windows copy of eigen3 using the "Unix Makefiles" option, since I do not have Visual Studio and don't want to download it.
Here is the cpp file:
#include <iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
int main()
{
double data[9];
for (int i = 0; i < 9; i++)
{
data[i] = i;
}
Map<MatrixXd> m(data, 3, 3);
BDCSVD<MatrixXd> svd(m, ComputeThinU | ComputeThinV);
auto v = svd.singularValues();
cout << "condition #: " << v.maxCoeff() / v.minCoeff() << endl;
}
I was trying to calculate the condition number of a singular matrix here.
Compiled using command line:
g++ -I"my eigen lib" t.cpp -o t.exe
where g++ comes from Rtools, "my eigen lib" is the eigen include directory, t.cpp the cpp file, and t.exe the build target.
Surprisingly or not, when I changed the algo from BDCSVD to JacobSVD, the compilation would succeed, even though BDCSVD defaults to JacobSVD for small matrices.
cc1plus and as both returned normally. ld keeps running forever, it seems.
Edits:
It's been awhile, but I am still looking for an answer.

'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;

Using Caffe in C++; Unable to pass caffe::SolverParameter to function without error

I am attempting to compile the "Hello World XOR" C++ example given by:
https://medium.com/#shiyan/caffe-c-helloworld-example-with-memorydata-input-20c692a82a22
Hardware:
EX50-UD3R Motherboard
GT-640 NVidia cards (2 Cards)
Intel 980 (6 core)
16Gig (4x4) DDR3 RAM
Environment:
Ubuntu 16.04.2 LTS Kernel: 4.4.0-78-generic
Eclipse Neon3 R4.6.3
CUDA 8.0
Caffe: whatever ver I downloaded from Berkley on 06/04/2017
I followed the installation instructions for installing Caffe in Ubuntu 16.04: https://github.com/BVLC/caffe/wiki/Ubuntu-16.04-or-15.10-Installation-Guide
and all the make commands succeeded.
However when I try to use the libraries in a simple cpp program:
#include <memory>
#include <cstdlib> // Provides NULL, rand, size_t;
#include <stdio.h> // For printf
#include <cstring> // For string
#include "/home/caffe-master/include/caffe/layers/memory_data_layer.hpp"
#include "/home/caffe-master-path>/include/caffe/caffe.hpp"
void blah( caffe::SolverParameter *sp);
int main()
{
... standard c++ code to generate random data deleted for brevity...
caffe::SolverParameter solver_param; // No errors given here for this declaration
blah( solver_param); // Simple function created to test error, same error appears here
caffe::ReadSolverParamsFromTextFileOrDie( path, &solver_param); // Actual function from XOR example, same error
caffe::ReadSolverParamsFromTextFileOrDie("./solver.prototxt", &solver_param); // This also gives same error
boost::shared_ptr<caffe::Solver<float> > solver(caffe::SolverRegistry<float>::CreateSolver(solver_param)); // Same error here
... code that would of passed random data to caffe deleted ...
return 0;
}
void blah( caffe::SolverParameter *sp)
{
std::cout << "BLAH: " << sp->rms_decay_;
return;
}
In Eclipse Neon I get the following error for any line that includes the variable solver_param including the lines that where I created the simple function blah which takes one argument of type caffe::SolverParameter *
Invalid arguments 'Candidates are:
void ReadSolverParamsFromTextFileOrDie(const std::__cxx11::basic_string,std::allocator> &, ? *)' caffe_hellow.cpp /caffe line 47 Semantic Error
I tried setting the full paths in the include path and that got rid of some errors, I added the libpath in eclipse to include caffe and boost and that got rid of more errors leaving only this one for now. I've been searching for a solution, looking at the .hpp caffe files to try to understand them and trying different settings in eclipse to get rid of this error but nothing works. Casting did not work either.
For some reason it does not recognize any variable declared as type SolverParameter as a compatible argument to a function that requires it. The error message gives "? *" as the argument type. What does that mean? Can you help? Thank you.
You need to include
#include "caffe/proto/caffe.pb.h"
For all caffe.proto related declarations
I am able to compile now. It is a consequence of being a noob at using IDE.
I had to add the libraries in Project->Properties->GCC C++ Linker-> Libraries (-l):
caffe
boost_system
glog
... and this may not be needed ... cuda
I also needed to add -L paths for the above. Then I needed to update either /etc/environment or .bashrc to include the paths for caffe and cuda in the LD_LIBRARY_PATH variable otherwise I would get an unable t link binary error. Again cuda may not be needed to solve this error the other three sure are. I plan to use Cuda-C to access the GPU processors. Thank you Shai for your response on the protoc item it lead me to the other comments on including libraries.

Can't multiply matrix with armadillo

I can not do matrix multiplications with armadillo. I don't know if there are more features I can't use. So far, I've only been using vectors and dot product with no problem.
Basically:
#include <iostream>
#include <armadillo>
using namespace std;
using namespace arma;
int main(){
//this works
vec v = randu<vec>(10);
cout<<dot(v,v)<<endl;
int n =5;
//this doesn't work
mat M = randu<mat>(n,n); // program compiles but stops running when reaches here
cout<<M*M<<endl;
return 0;
}
I am using the newest version of codeblock and armadillo. OS is windows 7. I've included the lapack and blas libraries in the compiler linker settings and blas_win64_MT and lapack_win64_MT are both in PATH. And I've also included de armadillo folder in the search directory. In config.hpp (armadillo folder), #define ARMA_USE_LAPACK and #define ARMA_USE_BLAS are uncommented. #define ARMA_USE_WRAPPER is commented. Also, I have tried to add -lapack -lblas to the build->project options->compiler->other options and to build->project options->linker setting->other options, but I had no success. Same thing happened when I tried to add -larmadillo to the compiler with #define ARMA_USE_WRAPPER uncommented.
What am I missing?
Your program is of course perfectly fine as Armadillo is a well-designed and delivered library.
What may not be right is your installation, or local setup. Only you can figure that out.
On my box, and on the command-line -- where I only need to link with libarmadillo which itself has linkage to lapack and blas:
edd#max:/tmp$ g++ -o arma5by5 arma5by5.cpp -larmadillo
edd#max:/tmp$ ./arma5by5
4.06892
1.5043 1.3996 0.6353 0.8246 1.4694
1.6543 1.6822 0.4338 0.6739 1.5782
1.3145 1.2759 0.3825 0.4967 1.2959
1.4222 1.4584 1.0028 1.3742 1.3593
1.6126 1.7886 0.4599 0.8348 1.5648
edd#max:/tmp$

How to properly include Armadillo in CLion (without having false errors)?

When I use the header-only Armadillo C++ library in CLion, the IDE indicates (highlights) multiple (false) errors in the code, however the usage of Armadillo is valid and the code builds and runs without any errors.
For example, in a very simple Armadillo test project, the IDE indicates 3 errors, as written in the comments:
#include <iostream>
#include "armadillo"
using namespace std;
using namespace arma;
int main() {
cout << "Armadillo version: " << arma_version::as_string() << endl;
// Returns 5.0.1 (Ankle Biter)
mat A(2,3); // Error: Too many arguments, expected 0
A.fill(99);
A(1,2) += 101.0; // Error: Called object is not a function
A.print("A = ");
A.set_size(4,5); // Error: Too many arguments, expected 1
A.fill(77);
A.print("A = ");
return 0;
}
Since Armadillo is header-only, I did not modify the default CMakeLists.txt file, only included the main header in main.cpp and copied armadillo_bits to the project directory.
I've tried to configure Armadillo with CMake, but on Windows it seems Armadillo's bundled CMakeLists.txt just copies the includes and creates a config.hpp in my working dir.
Is there a way to index symbols in header-only libraries?
CLion version is 1.0 (141.353), Armadillo version is 5.0.1.
My platform is Windows 8.1 x64, and I'm using MinGW v64 4.9.2 (x86_64-4.9.2-win32-seh-rt_v4-rev2)
The CLion project is available in this repository.
Thanks to anyone trying to investigate this issue.