Error information in the compiling of c++ with armadillo library - c++

A beginner to armadillo library and have two problems with it.
The first question is about the link of armadillo and c++. I installed armadillo by following the instructions in the package and the location is /usr/include/, I could successfully run the code:
#include <iostream>
#include "/usr/include/armadillo"
using namespace std;
int main()
{
arma::mat a = arma::randu<arma::mat>(3, 3);
cout << a << endl;
return 0;
}
but if I want to define a function, for example, the inverse of the matrix (there is a function I can call from the library but I just want to do a test).
#include <iostream>
#include "/usr/include/armadillo"
using namespace std;
arma::mat Inverse( arma::mat A){
return arma::inv(A);
}
int main()
{
arma::mat a = arma::randu<arma::mat>(3, 3);
arma::mat inv_a = Inverse(a);
cout << inv_a << endl;
return 0;
}
then the program failed with three error messages.
Undefined symbols for architecture x86_64:
"_wrapper_dgetrf_", referenced from:
void arma::lapack::getrf<double>(int*, int*, double*, int*, int*, int*) in main.o
"_wrapper_dgetri_", referenced from:
void arma::lapack::getri<double>(int*, double*, int*, int*, double*, int*, int*) in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I checked by google and have tried some advices, like:" In miscellaneous MAC OS X linkers, I had to put "-01 -larmadillo" AND I had to deactivate the shared library settings!", to be honest, I can not find the miscellaneous option and deactivate the shared library settings as i am not familiar with Xcode. Then I followed install MacPorts and the library is installed to the folder /usr/local/include/, and I tried again, the first program is alright but the second program still has three errors, could anyone help me with this?
The second question would be the use of lapack when I am using armadillo in the same program, as I know armadillo is based on lapack, then if I declared the library armadillo, do I need to declare lapacke.h. Why do I ask this questions is that when I declared RcppArmadillo and lapacke.h, an error exists:
This file includes at least one deprecated or antiquated header. Please consider using of the 32 header found in section 17.4.1.2 of the C++ standard. Example include substituting the <X> header for the <X.h> hear for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated. [-W#warnings]
Line 11763 conflicting type for 'cgetrf'
Line 11765 conflicting type for 'zgetrg_'
...

I had the same problem, but eventually I solved it.
I didn't installed armadillo for my own purpose, but provided absolute path to include and library directories. Also, in a README there is a note that if you're on Mac then you should use Accelerate framework like this "-framework Accelerate". So after doing this all linker errors were gone.

Related

Compiling multiple files in Visual Studio Code resulting in error

I'm relatively new to coding in C++ and have started working with main and header files and I've created a program to test it out, however, the following program results in the following compiler error:
Undefined symbols for architecture x86_64:
"Print()", referenced from:
_main in test-7d0225.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
This is my current code.
test.cpp
#include <iostream>
#include "test.h"
using namespace std;
int main() {
Print();
return 0;
}
test.h
#ifndef TEST_H
#define TEST_H
void Print();
#endif
test1.cpp
#include <iostream>
#include "test.h"
using namespace std;
void Print() {
cout << "Hello" << endl;
}
Nothing I've found online has helped me and my only assumptions are that my compiler isn't set up correctly. I've tried compiling both of these of files by typing in "g++ test.cpp test1.cpp", as well, but yields similar results. I would like to note that I am on Mac as well. Please feel free to leave any comments or suggestions for how I've asked this question, this is my first time on stack overflow.
Your code seems just fine, I’ll will try to compile it on vscode myself.
One problem could be your vscode launch.json. I have had the same problem as what I am describing. Vscode can be quite “Finicky” with low level languages like c and c++. In your launch file (or tasks file if it calls something in tasks) make sure you compile *.cpp files in the folder. I would look something like {folder}/**.cpp. To find the exact command look on vscode’s official site.
Given that you tried compiling your code outside of vscode and assuming that everything is in the same folder, it might be a coding problem. Again, your code looks fine. Go online and find some learning to code website with multiple file coding. Copy their code and do the same thing that you did with your code. If it doesn’t work there is definitely a problem with how to code is being compiled. Otherwise revise your code and compare to find the problem.
Also, could you provide the terminal command vscode prints (I am also on Mac). It will be in the terminal tab in the g++ section, not in bash)
P.S. I am also new to stack overflow so any suggestions you have for me will be great.

C++ Namespace and Linking not working

Update: Fixed. Very simple solution. I just have to add in my Add_Executable method. Here:
add_executable (POC eb_pms.cpp url_binder.cpp)
So this problem looks pretty small, yet, I am unable to find any solution of this. Or, I should say, none of articles explains it in a simple manner so I can understand. I have come from a strongly typed background like C#/Java. I am trying to build a Rest API in C++11 using Simple Web Server and RapidJSON. Since we are building this project to be a part of a bigger solution, I cannot use commandline to link the program. I have to use the CMakeLists to compile and run the program. It means, the linking has to be done through the CMakeLists.txt file. The whole source code can be found here at GitHub. My environment is Macbook Pro + VS Code. So I have two CPPs here: eb_pms.cpp, url_binder.cpp. The urlbinder has a namespace 'EBPMS' and eb_pms has none. Following is the code of ep_pms:
(header file)
#include "server_http.hpp"
#include "client_http.hpp" //client for testing the API code
#include <boost/property_tree/ptree.hpp> /*for the shared pointer*/
#include "url_binder.h"
using HttpServer = SimpleWeb::Server<SimpleWeb::HTTP>;
using HttpClient = SimpleWeb::Client<SimpleWeb::HTTP>;
using Binder = EBPMS::URLBinder;
void bindUrls(HttpServer *server);
cpp file:
# include "eb_pms.h"
using namespace boost::property_tree;
using namespace std;
using namespace EBPMS;
int main(){
HttpServer server;
server.config.port = 8081;
bindUrls(&server);
thread server_thread([&server]() {
server.start();
});
server_thread.join();
}
void bindUrls(HttpServer *server){
//URLBinder c;
Binder binder;
binder.bindURL_string();
}
the url binder h:
#include <boost/property_tree/ptree.hpp> /*for the shared pointer*/
#include "server_http.hpp"
#include "client_http.hpp"
#include <iostream>
namespace EBPMS{
class URLBinder{
public: URLBinder();
void bindURL_string();
};
}
the url binder cpp;
#include "url_binder.h"
using namespace boost::property_tree;
using namespace std;
namespace EBPMS{
URLBinder::URLBinder() {}
void URLBinder::bindURL_string(){ //HttpServer *server
std::cout << "Trying to log if it comes" << std::endl;
}
}
So all I need is basically, to find a way to link this file with NameSpace with my original cpp so I can use the method from url binder inside my main cpp class. I am trying to call function in eb_pms.cpp class. When I run make command, I get this error:
Scanning dependencies of target POC
[ 2%] Building CXX object CMakeFiles/POC.dir/eb_pms.cpp.o
[ 5%] Linking CXX executable POC
Undefined symbols for architecture x86_64:
"EBPMS::URLBinder::bindURL_string()", referenced from: _main in eb_pms.cpp.o
bindUrls(SimpleWeb::Server<boost::asio::basic_stream_socket<boost::asio::ip::tcp>> >*) in eb_pms.cpp.o
"EBPMS::URLBinder::URLBinder()", referenced from: _main in eb_pms.cpp.o
bindUrls(SimpleWeb::Server<boost::asio::basic_stream_socket<boost::asio::ip::tcp>> >*) in eb_pms.cpp.o
ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [POC] Error 1
make[1]: ***
[CMakeFiles/POC.dir/all] Error 2
make: *** [all] Error 2
It has been hours since I am trying to research. But I am unable find that missing piece. Any help will be greatly appreciated.
I'm just guessing here since you don't show your CMakeLists.txt file...
I would say that you don't list your url_binder.cpp file as a source in the CMake add_executable command.
All source-files you want to be part of your program must be listed.

Type undefined reference to `cv::fastFree(void*)'

I started the tutorial for opencv 2.4.7 on Win8, latest MinGW, and Eclipse Kepler R1, CDT. C++ Compiler includes are referencing to [path-to-opencv]\open247\build\include.
in the MinGW C++ Linker I add the library path [path-to-opencv]\open247\build\x64\vc11\lib. The vc11 directory contains libraries for VisualStudio from what I've read. Is that still okay to compile them with MinGW?
I added the core and highgui library for this sample (I don't think we need more than those).
However, for this simple idle code below, I provoke the following compilation error.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main(){
Mat image;
}
The error on compilation is:
Description Resource Path Location Type
undefined reference to `cv::fastFree(void*)' FirstOpenCV line 278, external location: C:\Users\John\Documents\Software\opencv247\build\include\opencv2\core\mat.hpp C/C++ Problem
What's the reason for this?
If I add
namedWindow("testWindow", 1);
I even get this error
Description Resource Path Location Type
undefined reference to `cv::namedWindow(std::string const&, int)' main.cpp /FirstOpenCV/src line 23 C/C++ Problem
Have you link correctly your library?
-lopencv_core
-lopencv_highgui
-L<opencv_library_path>
The libraries have to be specified after the source file on the compiler/linker command line. (The linker keeps track of unresolved symbols, and resolves them via later command-line arguments.)

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.

Undefined Symbol: _sf_open (Simple audio stuff on OSX)

I'm trying to get into C++ programming, and I'm quite struggling against the little details. Right now, I'm trying to get the snippet below to work, and apparently it will compile, but not link. (error message is a the bottom of this post)
I'm using libsndfile to open audio files, and the linker doesn't seem to find the corrent library files for it. This is on OS X 10.5.
I've downloaded libsndfile from mega-nerd.com and installed it via the ususal configure, make, sudo make install procedure. Is there anything else I need to do?
Edit: I have macports installed on my system, if that's of any concern.
Here's my code:
#include <iostream>
#include <string>
#include <sndfile.h>
#include "stereoSplit.h"
using namespace std;
int stereoSplit(string filename)
{
cout << filename;
SF_INFO sfinfo;
sfinfo.format = 0;
SNDFILE * soundfile;
soundfile = sf_open( filename.c_str(), SFM_READ, &sfinfo );
return 0;
}
int main (int argc, char const *argv[])
{
return stereoSplit("testStereo.wav");
}
And here's the complete error message:
Undefined symbols:
"_sf_open", referenced from:
stereoSplit(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)in cc3hvkkk.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
You need to link in the library. Your compiler command should look something like:
g++ mystuff.cpp -lsndfile
To link the library in Xcode, select the project target, add a new item to Link binary with libraries, and find the libsndfile.1.dynlib on your computer.