Closed seqwalt closed 11 months ago
Hey @seqwalt , thanks for the question and sorry about the slow reply!
The problem is that I was a bit lazy when I wrote Arduino.h and I instantiated Serial
in that header file. That means for your setup it'll be defined twice, once when Test.h includes BasicLinearAlgebra.h
and again in the TEST
library.
For the time being the easiest (and slightly hacky) way to fix this would be to just move the definition of Serial from here into say, main.cpp
.
When I get a chance I'll push a change to fix this properly too. Hope that helps! Let me know if you have any more trouble
Hey @tomstewart89 no worries on the slow reply!
Thanks for the explanation, I have a quick follow up question. Following your advice, I moved the definition of Serial into main.cpp
, such that main.cpp
looks like:
#include "Test.h"
#include <BasicLinearAlgebra.h>
int main(){
Test test;
Print Serial;
BLA::Matrix<3, 3> A;
A.Fill(0);
Serial << A;
}
and Arduino.h
looks like:
#pragma once
#include <algorithm>
#include <iomanip>
#include <sstream>
struct Print
{
// stuff in the struct
...
};
using std::endl;
using std::max;
It compiles, however when I run ./my_test
, nothing is printed to the terminal. Is this the expected behavior? Thanks again for all the help :)
Hey @seqwalt, confusingly, that actually is the intended behavior. I wrote that Serial implementation just to help with my unit tests, not for printing to the console. If you want to see what's being written to that Serial you can write:
std::cout << Serial.buf.str();
You can also just define this function somewhere in your project:
template <typename DerivedType, int Rows, int Cols, typename DType>
std::ostream &operator<<(std::ostream &strm, const MatrixBase<DerivedType, Rows, Cols, DType> &mat)
{
strm << '[';
for (int i = 0; i < Rows; ++i)
{
strm << '[';
for (int j = 0; j < Cols; ++j)
{
strm << mat(i, j) << ((j == Cols - 1) ? ']' : ',');
}
strm << (i == Rows - 1 ? ']' : ',');
}
return strm;
}
And then you'll be able to print matrices to std::cout
directly like so:
Matrix<3, 3> A = {3., 5., 8., 4., 7., 9., 2., 5.0, 10.};
std::cout << A;
Hope that helps!
Thanks @tomstewart89 this is very helpful!
When trying to make a c++ class that utilizes BLA, I encounter an error when testing on Linux and Mac. After running
cmake ..
andmake
from directoryTest_BLA/build/
, I receive the error:Here is my directory structure: |├── BasicLinearAlgebra/ |└── Test_BLA/ | . . . ├── Arduino.h | . . . ├── build/ | . . . ├── CMakeLists.txt | . . . ├── main.cpp | . . . ├── Test.cpp | . . . └── Test.h
The contents of Arduino.h are the same as https://github.com/tomstewart89/BasicLinearAlgebra/blob/master/test/Arduino.h.
Contents of CMakeLists.txt:
Contents of main.cpp:
Contents of Test.h:
Contents of Test.cpp:
Note that if I change the instance name of the
Print
struct withinArduino.h
to beSerial_test
instead ofSerial
, then the error message changes tomultiple definition of `Serial_test'
. Thanks for any help and this great library!