Linker hangs when compiling a simple eigen3 program on Win10 - c++

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.

Related

Getting error including eigen library in c++

I am trying to use eigen for linear algebra but can't get it to include the eigen library. It keeps on giving me an error even though I am following all the instructions to include the eigen folder while compiling the program. I have tried this in both visual studio 2017 and the MinGW version of gcc. I am giving the relevant part of my code and what I am doing to include the Eigen library.
#include <iostream>
#include <fstream>
using namespace std;
#include <Eigen/Dense>
using Eigen::MatrixXd;
int main()
{
MatrixXd m(2,2);
m(0,0) = 3;
m(1,0) = 2.5;
m(0,1) = -1;
m(1,1) = m(1,0) + m(0,1);
std::cout << m << std::endl;
}
To run this in g++, I am using the command
g++ -I /C:\Users\aqils\Documents\C++\Eigen/ Matrixbasics.cpp -o Matrixbasics.exe
Here C:\Users\aqils\Documents\C++\Eigen
is the path for the unzipped Eigen package.
Matrixbasics.cpp is the name of the c++ file and it's located in the folder
C:\Users\aqils\Documents\C++
The error I get is
"Fatal error: Eigen/Dense: no such file or directory"
To run this in Visual Studio 2017, I have followed these steps to add a folder to a project:
1. Right click on the project name in the solution explorer and hit properties
2. Then look for c++ and find the option for adding a folder.
3. Select the Eigen package folder in the tree view, hit ok and then apply.
4. Run the program. It should now work.
I have spent several frustrating days trying to make this work and have read all related questions on stack overflow as well as several other websites. The answers don't go beyond the above instructions which I am already following. Will be really grateful if someone can help me fix the problem.
You can replace this line:
#include <Eigen/Dense>
by:
#include <eigen3/Eigen/Dense>

GCC C/C++ MEX Matlab R2015 Mac OS X (with OpenMP) doesn't work

I'm trying to compile a very simple MEX file in Matlab using GCC/G++...
First I checked that this is already installed in matlab by:
!which gcc
output:
/usr/bin/gcc ...
After, I tried to do the following:
mex -v GCC='/usr/bin/gcc' hello.c
but it stills compiling with XCode Clang...
This is to use G++ with OPENMP library for parallel computing.
Is known that is NOT OFFICIALLY Supported by Mathworks, but POSSIBLE.
MATLAB only supports XCode 5.1+ or 6.0 on Mac.
See this list of supported compilers, from Mathworks:
Supported Compilers and Compatible Compilers - Release R2015a
EDIT!!!
Even Windows doesn't support a GCC compiler. However, if you can build your library in Windows there is a 3rd party library called Gnumex designed to provide MinGW or CygWin GCC compiler capabilities for Matlab MEX. Take a look.
An update on their website says that changed to Matlab have broken their utility. However, there seems to be a workaround available via this StackOverflow post.
Finally, I found a proper way to solve it...
First, the file mexopts.sh doesn't appear in the folder by default, is necessary to open a terminal and look for it and create it (then Matlab will redirect to it automatically when compiling with MEX):
find ~/ -name 'mexopts.sh'
and will appear:
/Users/FOO//.Trash/MATLAB_R2014a.app/bin/mexopts.sh
Then, copy it as:
cp /Users/FOO//.Trash/MATLAB_R2014a.app/bin/mexopts.sh ~/.matlab/R2014a
then go to the folder cd ~/.matlab/R2014a
and change permissions for your user as:
chmod u+rwx mexopts.sh
then, open it with your default text editor (Sublime text recommended) as:
open mexopts.sh
and edit the following:
Change where appears macosx10.7 to your current version, in my case macos10.10
then, modify the following lines as describen in (http://www.mathworks.com/matlabcentral/newsreader/view_thread/334250):
# CC='xcrun -sdk macosx10.7 clang' #OLD
CC='xcrun /usr/local/bin/gcc' #NEW
# CXX='xcrun -sdk macosx10.7 clang++' #OLD
CXX='xcrun /usr/local/bin/g++' #NEW
# CLIBS="$CLIBS -lstdc++" #OLD
CLIBS="$CLIBS -lstdc++ -I/usr/local/lib -lgomp" #directory <-I/usr/local/lib> #NEW
#CXXLIBS="$MLIBS -lstdc++" #OLD
CXXLIBS="$MLIBS -lstdc++ -I/usr/local/lib -lgomp" #NEW
IMPORTANT NOTE:
Make sure that your current G++/G++4.9 is able to compile with OpenMP, trying to include <omp.h> in a hello world file doing in the command line:
g++-4.9 -o test hello.cpp -fopenmp
or
g++ -o test hello.cpp -fopenmp
is also possible that a file is corrupted and is necessary to do Can not compile fortran because dyld: Library not loaded :
brew rm cloog
brew install cloog
(But check first)...
Is also possible that if you're not able to compile with OMP is necessary to do first a couple of things as described here (Compile OpenMP programs with gcc compiler on OS X Yosemite):
1. Got a new gcc complier from http://hpc.sourceforge.net/
2. Place a new executable folder by $ sudo tar -xvf gcc-4.9-bin.tar -C /
3. Switched to it by export PATH=/usr/local/bin:$PATH
Finally, try to compile your MEX file with:
mex hello.cpp COMPFLAGS="/openmp $COMPFLAGS"
That's it
... .
I was unable to get the mexopts.sh approach to work.
Instead I followed this first to make sure that I could get omp to work with xcode: clang-omp in Xcode under El Capitan
Then the mex compile st
mex CC='/usr/local/bin/clang-omp' -I/usr/local/include -I/usr/local/lib /usr/local/lib/libiomp5.dylib test.c
However, I get some weird results when comparing between Matlab and pure terminal based c. The common work, I put in the file "do_work.c":
double do_work(int maxit){
double tmp,x,x2;
int numThreads=0;
numThreads = omp_get_max_threads() ;
// numThreads = 4;
printf("Setting max num threads to %d.\n",numThreads);
omp_set_num_threads(numThreads);
// int Nthreads=omp_get_num_threads();
tmp = 0.0;
x2 = 0.0;
#pragma omp parallel for shared(tmp,x2) private(x)
for (int i=0;i<maxit;i++) {
x = 0.0;
for (int k=0; k<10000; k++) x += pow(.011,1.0/.5); // does some heavy computations
tmp += (double) i ;
if (i%1000==0){
printf("Hello, %d\n",i);
}
x2 += x;
}
printf("x2 = %f\n",x2);
return tmp;
}
I can compile this from command line using this wrapper file:
#include "stdio.h"
#include "math.h"
#include <libiomp/omp.h>
#include "do_work.c"
double do_work(int maxit);
int main(int argc, const char * argv[]) {
do_work(10000);
return 0;
}
I compile it by running
clang-omp -fopenmp test_c_wrapper.c
I can also interface to it from Matlab with this code:
#include "mex.h"
#include <libiomp/omp.h>
#include "math.h"
#include "stdio.h"
#include "do_work.c"
double do_work(int maxit);
void mexFunction(int nlhs, mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
do_work(10000);
return;
}
To compile this from Matlab, save it as test.c and run the first mex statement that I mentioned higher up. However, the results are really strange. It looks as if Matlab is not even using omp. At the same time, Matlab actually runs faster than the terminal based one, even though Matlab appears to run in serial mode? Strange... Also, the omp_get_max_threads() is acting weird, it doesn't return the same number every time when called from Matlab even though it does from terminal.

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.

-m32 flag not working to build 32 bit application

I am trying to compile some super simple code in Code::Blocks using GCC 4.4. I am on OS X Mavericks if that matters. I'm trying to compile and make a 32 bit executable, so I've taken the advice of a bunch of stack overflow posts to add the -m32 flag to the compiler (in Code::Blocks, I did Project > Build Options > Compiler Settings > Other Options > added -m32).
The code is the standard hello world program that pops up in every main file. I'll post it here for those who aren't familiar with Code::Blocks.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
When I go to build, I clean all the object files and rebuild, and I get the following warning which prevents an executable from being created:
||warning: ignoring file obj/Release/main.o, file was built for i386 which is not the architecture being linked (x86_64): obj/Release/main.o|
I'm not really sure where to go from here. I thought the -m32 flag deals with these things.
Any ideas or directions I can take? If there's any info I left out let me know.