include LAPACK on c++ code - c++

I am trying to execute following simple c++ code in Qt environment on my mac. What I want with this code is to use libraries of LAPACK for simple mathematical operations. I just considered "dot product " function to test this case and got
"Symbols not found for architecture x86_64" and
"linker command failed with exit code 1 (use -v to see invocation) errors.
By the way, I am quite sure that i am repeating file or header file names that might cause these same errors.
Anyone knows how to overcome this ? Thanks in advance.
#include <iostream>
#include <vector>
#include <Accelerate/Accelerate.h>
extern "C"
{
double ddot_ (const int*, const double*, const int*, const double*, const int*);
}
int main()
{
std::vector<double> values(2, 1.);
int N = 2;
int one = 1;
double norm = ddot_(&N, &values[0], &one, &values[0], &one);
std::cout << "Hello world " <<norm << std::endl;
return 0;
}

The function ddot_() is part of the Blas library. It is not a part of the Lapack library. Hence, the program using ddot_() should be linked to the Blas library. For instance, the c++ program you posted can be compiled by:
g++ main.cpp -o main -lblas
provided #include <Accelerate/Accelerate.h> is commented.

Related

c++ include header get failed

recently I've started learning c++. When I tried to write my header file, I got include error. Here is my code:
First is the header file(header.h)
#pragma once
void print(int);
and then is its cpp file(header.cpp)
#include "header.h"
#include <iostream>
using namespace std;
void print(int x){
cout << x << endl;
}
and finally my main cpp program(main.cpp)
#include <iostream>
#include "./header.h"
using namespace std;
int main(){
int x = 123;
print(x);
}
Here is the error, I can't figure out what it's saying orz
cd "/Users/yianchen/Desktop/cpp practice/" && g++ main.cpp -o main &&
"/Users/yianchen/Desktop/cpp practice/"main Undefined symbols for
architecture x86_64: "print(int)", referenced from:
_main in main-90c620.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 have searched for some solution, when I use
#include "header.cpp"
It works fine, but I see guys unrecommended using #include some_file.cpp
By the way, I use visual studio code and use code runner. Thanks!
The easiest solution would be to do something like the following
g++ header.cpp main.cpp
This will make sure that the function defined in header.cpp is compiled together with the code that uses it.
Normal usage would be to compile header.cpp, not to include it in another .cpp source. Then the linker will put the pieces together.

VS Code C++ OOP don't work Mac OS High Sierra

hello guys i'm new to vs code and i couldn't find a solution to use object oriented programming
when I create a .h file to call an object function I get an error
123MacBook-Pro-de-Rogerio: life DJMatrix $ cd "/ Users / DJMatrix / Documents / Classes / c ++ / life /" && g ++ main.cpp -o main && "/ Users / Dtrix / Documents / Classes / c ++ / life / "main
Undefined symbols for architecture x86_64:
  "Life :: tryAgain ()", referenced from:
      _main in main-ea3ce4.o
ld: symbol (s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
main.cpp:
#include <iostream>
#include "life.h"
using namespace std;
int main()
{
    Life life;
    life.tryAgain();
    return 0;
}
life.h:
#include <iostream>
using namespace std;
class Life
{
public:
    bool sucess;
  void tryAgain();
  void improve();
};
life.cpp:
#include "life.h"
void Life::tryAgain()
{
cout << "Trying again!!!" << endl;
}
void Life::improve()
{
cout << "Improve !!" << endl;
}
From what I see from the VSCode terminal, only main.cpp is getting compiled. The object file for life.cpp is not getting linked when you produce the final binary, that's why it's complaining that the Life::tryAgain() symbol is missing.
This depends on whether you're invoking the compiler manually or using Makefiles or letting VSCode do all of this for you; regardless the compile command should look like:
g++ -o main life.cpp main.cpp

Strange compilation error: compiler bug or my own?

I have header-only library, and I'm getting the strangest compilation error. The problem in proceeding is that I'm not getting this error in a representative minimal example, and not on all compilers (on macOS everything compiles and runs like a charm). I am not sure if I should be worried about something in my code, or if it is an upstream bug. Regardless, I'm not sure how to work around this.
I'm getting the following compiler error:
In file included from /home/tdegeus/opt/include/eigen3/Eigen/SparseCore:59:0,
from /home/tdegeus/opt/include/eigen3/Eigen/Sparse:26,
from /home/tdegeus/opt/include/eigen3/Eigen/Eigen:2,
from /home/tdegeus/prog/src/GooseFEM/docs/examples/Statics/Periodic/LinearElasticity/main.cpp:1:
/home/tdegeus/opt/include/eigen3/Eigen/src/SparseCore/SparseSelfAdjointView.h: In function ‘void Eigen::internal::permute_symm_to_symm(const MatrixType&, Eigen::SparseMatrix<typename MatrixType::Scalar, DestOrder, typename MatrixType::StorageIndex>&, const typename MatrixType::StorageIndex*) [with int SrcMode = 1; int DstMode = 2; MatrixType = Eigen::SparseMatrix<double>; int DestOrder = 0]’:
/home/tdegeus/opt/include/eigen3/Eigen/src/SparseCore/SparseSelfAdjointView.h:517:6: internal compiler error: in predicate_mem_writes, at tree-if-conv.c:2252
void permute_symm_to_symm(const MatrixType& mat, SparseMatrix<typename MatrixType::Scalar,DstOrder,typename MatrixType::StorageIndex>& _dest, const typename MatrixType::StorageIndex* perm)
^~~~~~~~~~~~~~~~~~~~
0xb3530b predicate_mem_writes
/ssoft/spack/paien/spack.v2/var/spack/stage/gcc-7.3.0-drb5cfgzldhzbrs6lyzk7xdqqcixbxhb/gcc-7.3.0/gcc/tree-if-conv.c:2252
0xb3530b combine_blocks
/ssoft/spack/paien/spack.v2/var/spack/stage/gcc-7.3.0-drb5cfgzldhzbrs6lyzk7xdqqcixbxhb/gcc-7.3.0/gcc/tree-if-conv.c:2377
0xb359bb tree_if_conversion(loop*)
/ssoft/spack/paien/spack.v2/var/spack/stage/gcc-7.3.0-drb5cfgzldhzbrs6lyzk7xdqqcixbxhb/gcc-7.3.0/gcc/tree-if-conv.c:2883
0xb3663b execute
/ssoft/spack/paien/spack.v2/var/spack/stage/gcc-7.3.0-drb5cfgzldhzbrs6lyzk7xdqqcixbxhb/gcc-7.3.0/gcc/tree-if-conv.c:2961
Please submit a full bug report,
with preprocessed source if appropriate.
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.
Which disappears if I comment this line (obviously breaking functionality):
m_solver.compute(m_ACuu);
Note that:
Eigen::SparseMatrix<double> m_ACuu;
Eigen::SimplicialLDLT<Eigen::SparseMatrix<double>> m_solver;
I tried a minimal example:
#include <iostream>
#include <iomanip>
#include <math.h>
#include <vector>
#include <Eigen/Dense>
#include <Eigen/Sparse>
#include <Eigen/SparseCholesky>
int main()
{
size_t N = 11;
// allocate system
Eigen::SparseMatrix<double> A(N,N);
Eigen::VectorXd f(N);
// set force
f *= 0.0;
f((N-1)/2) = 1.0;
// construct list of Triplets (row,col,data) that from the matrix "A"
std::vector<Eigen::Triplet<double>> Atr;
// - first equation
Atr.push_back(Eigen::Triplet<double>(0,0,+2.0));
Atr.push_back(Eigen::Triplet<double>(0,1,-1.0));
// - body
for ( size_t i=1; i<N-1; ++i ) {
Atr.push_back(Eigen::Triplet<double>(i,i-1,-1.0));
Atr.push_back(Eigen::Triplet<double>(i,i ,+2.0));
Atr.push_back(Eigen::Triplet<double>(i,i+1,-1.0));
}
// - last equation
Atr.push_back(Eigen::Triplet<double>(N-1,N-2,-1.0));
Atr.push_back(Eigen::Triplet<double>(N-1,N-1,+2.0));
// build matrix from Triplets
A.setFromTriplets(Atr.begin(),Atr.end());
// solve the linear system
// - define solver (according to type above)
Eigen::SimplicialLDLT<Eigen::SparseMatrix<double>> solver;
// - analyze the system, and factorize (one could solve different right-hand-sides)
// solver.analyzePattern(A);
solver.factorize(A);
// - solve "A * x = f"
Eigen::VectorXd u = solver.solve(f);
// print to screen
std::cout << A << std::endl;
std::cout << u << std::endl;
return 0;
}
Compiled on the same system with the same libraries and with similar options:
g++ -march=native -O3 test.cpp
But this compiles and runs like a charm!?
Note that I've tried several Eigen versions, including 3.3.90.
Edit
Also note that this problem occurs with
g++ --version
g++ (GCC) 7.3.0
But is gone for
g++ (GCC) 6.4.0

How to link boost properly? [duplicate]

I'm writing a program to get myself acquainted with OpenSSL, libncurses, and UDP networking. I decided to work with OpenSSL's SHA256 to become familiar with industry encryption standards, but I'm having issues with getting it working. I've isolated the error to the linking of OpenSSL with the compiled program. I'm working on Ubuntu 12.10, 64 bit. I have the package libssl-dev installed.
Take, for instance, the C++ main.cpp:
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
using namespace std;
#include <openssl/sha.h>
string sha256(const string str)
{
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, str.c_str(), str.size());
SHA256_Final(hash, &sha256);
stringstream ss;
for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
ss << hex << setw(2) << setfill('0') << (int)hash[i];
}
return ss.str();
}
int main()
{
cout << sha256("test") << endl;
cout << sha256("test2") << endl;
return 0;
}
I'm using the SHA256() function found here as a wrapper for OpenSSL's SHA256 functionality.
When I attempt to compile with the following g++ arguments, I receive the following error:
millinon#myhost:~/Programming/sha256$ g++ -lssl -lcrypto -o main main.cpp
/tmp/ccYqwPUC.o: In function `sha256(std::string)':
main.cpp:(.text+0x38): undefined reference to `SHA256_Init'
main.cpp:(.text+0x71): undefined reference to `SHA256_Update'
main.cpp:(.text+0x87): undefined reference to `SHA256_Final'
collect2: error: ld returned 1 exit status
So, GCC clearly recognizes OpenSSL's defined functions and types, but ld is failing to find the function symbols referred to in sha.h.
Do I need to manually point to a specific shared object or directory?
Thanks!
You make a very common beginners mistake... Putting the libraries you link with in the wrong place on the command line when you build.
Dependencies are reversed on the command line, so something that depends on something else should actually be put before what it depends on on the command line.
In your example, you have a source file main.cpp that depends on some set of libraries, then the source file should be before the libraries it depend on:
$ g++ -o main main.cpp -lssl -lcrypto
To be safe, always put libraries last, after any source or object files listed on the command line.
This works fine on my system, but you might try:
extern "C" {
#include <openssl/sha.h>
}
which tells g++ that all the stuff in openssl/sha.h is declared as "C" functions.
BTW, how old is your OpenSSL?

How to use OpenSSL's SHA256 functions

I'm writing a program to get myself acquainted with OpenSSL, libncurses, and UDP networking. I decided to work with OpenSSL's SHA256 to become familiar with industry encryption standards, but I'm having issues with getting it working. I've isolated the error to the linking of OpenSSL with the compiled program. I'm working on Ubuntu 12.10, 64 bit. I have the package libssl-dev installed.
Take, for instance, the C++ main.cpp:
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>
using namespace std;
#include <openssl/sha.h>
string sha256(const string str)
{
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, str.c_str(), str.size());
SHA256_Final(hash, &sha256);
stringstream ss;
for(int i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
ss << hex << setw(2) << setfill('0') << (int)hash[i];
}
return ss.str();
}
int main()
{
cout << sha256("test") << endl;
cout << sha256("test2") << endl;
return 0;
}
I'm using the SHA256() function found here as a wrapper for OpenSSL's SHA256 functionality.
When I attempt to compile with the following g++ arguments, I receive the following error:
millinon#myhost:~/Programming/sha256$ g++ -lssl -lcrypto -o main main.cpp
/tmp/ccYqwPUC.o: In function `sha256(std::string)':
main.cpp:(.text+0x38): undefined reference to `SHA256_Init'
main.cpp:(.text+0x71): undefined reference to `SHA256_Update'
main.cpp:(.text+0x87): undefined reference to `SHA256_Final'
collect2: error: ld returned 1 exit status
So, GCC clearly recognizes OpenSSL's defined functions and types, but ld is failing to find the function symbols referred to in sha.h.
Do I need to manually point to a specific shared object or directory?
Thanks!
You make a very common beginners mistake... Putting the libraries you link with in the wrong place on the command line when you build.
Dependencies are reversed on the command line, so something that depends on something else should actually be put before what it depends on on the command line.
In your example, you have a source file main.cpp that depends on some set of libraries, then the source file should be before the libraries it depend on:
$ g++ -o main main.cpp -lssl -lcrypto
To be safe, always put libraries last, after any source or object files listed on the command line.
This works fine on my system, but you might try:
extern "C" {
#include <openssl/sha.h>
}
which tells g++ that all the stuff in openssl/sha.h is declared as "C" functions.
BTW, how old is your OpenSSL?