Using Matlab "engine.h" from c++ correctly - c++

I have a code that proceses frames in each iteration and generatesa matrix. My final goal is to send the matrix data to matlab in order to examine the evolution of the matrix with each frame.
In order to achieve this I defined a static variable Engine in a header file (helper.h).
#include "engine.h";
#include "mex.h";
static Engine *engine;
In the main() program I open the engine only once:
#include helper.h
main(){
if (!(engine = engOpen(NULL))) {
MessageBox ((HWND)NULL, (LPSTR)"Can't start MATLAB engine",(LPSTR) "pcTest.cpp", MB_OK);
exit(-1);}
//here comes frame processing using a while loop
.
. //a function is called (defined in matrix.cpp)
.
//frame processing ends
}
And inside matrix.cpp is where I get the matrix I want to send to Matlab Engine, so I do something like this:
#include helper.h
mxArray *mat;
mat = mxCreateDoubleMatrix(13, 13, mxREAL);
memcpy(mxGetPr(mat),matrix.data, 13*13*sizeof(double));
engPutVariable(engine, "mat", mat);
I want to use the pointer to engine the most efficient way. I am a bit conffused about how to correctly use matlab engine.
Any help woould be welcomed, because the matlab documentation and examples didn't help at all as they have all the code in the same file and they don't use iterations. Thanks in advance.
EDIT
First problem solved about the engine pointer. The solution is declaring it as extern.
#include "engine.h";
#include "mex.h";
extern Engine *engine;
and in main.cpp
#include helper.h
Engine *engine=NULL;
main(){}

static means "local to the current compilation unit". A compilation unit is normally a single .cpp file, so you have two engine variables in your program, one in main.o and one in matrix.o. You need to declare engine as extern in the header file and define it without any modificator in exactly one .cpp file.
helper.h:
extern Engine* engine;
main.cpp:
#include "helper.h"
Engine* engine = NULL;

Related

Calling functions from separate C++ files in main.cpp trouble

I have been searching online for solutions to this rather simple problem. My goal is to call a function from a separate .cpp file in my main.cpp file. What I have found thus far has told me to define my function in a separate .cpp file (averageScore.cpp) which looks like:
void averageScore()
{
"Blah, Blah, Blah"
}
And then declare the function as a prototype in a header file (Lesson1.h) which looks like:
#include "C:/averagescore.cpp"
void averageScore();
And finally call the function again in the main.cpp:
#include "Lesson1.h"
int main()
{
averageScore();
return 0;
}
I am currently a CS student and my overall objective with this method of organization and execution is to create a single project for all of the rudimentary programs we must create on a weekly basis instead of creating a new project for every single program. For reference, I am using VScode and have used the following link to help me thus far:
http://www.cplusplus.com/forum/beginner/97779/
My condolences and gratitude are extended to anyone who has taken the time to read through this and help me out!
To achieve what you want, you must create a header file, and declare your function there, for example:
lesson1.h
void averageScore();
In a .cpp file, you define that function and include the header you just created:
lesson1.cpp
#include "lesson1.h"
void averageScore(){
// Do what you want in this function
}
Then you can call that function in your main.cpp by including "lesson1.h":
main.cpp
#include "lesson1.h"
int main()
{
averageScore();
return 0;
}

C++ Thor library - problem with using resource loader class ( ' ' does not name a type)

I have been recently practicing managing multiple objects and drawing them in C++ using SFML library. I wanted my textures and future resources to be more reusable so I decided to make use of Thor library which suits my needs really well.
So I've written first few lines of code based on what you can find in this tutorial and the compiler always says:
main.cpp|12|error: 'textures_holder' does not name a type
This line gives an error :
textures_holder.acquire("Dirt", thor::Resources::fromFile<sf::Texture>("Textures\\dirt_block.png"));
I'm using Code::Blocks IDE with MinGW compiler and SFML 2.5.0.
Here's my main.cpp and the header file which contains extern object :
//...
#include <Thor/Resources.hpp>
#include "Dirt_Block.h"
using namespace std;
//Adding textures to the texture library
//THIS LINE GIVES AN ERROR
textures_holder.acquire("Dirt", thor::Resources::fromFile<sf::Texture>("Textures\\dirt_block.png"));
//Rest of code...
Dirt_Block.h (only the upper part) :
#ifndef DIRT_BLOCK_H
#define DIRT_BLOCK_H
#include <SFML\Graphics.hpp>
#include <vector>
#include <Thor/Resources.hpp>
#include <Thor/Resources/SfmlLoaders.hpp>
extern sf::Vector2u screenRes;
extern thor::ResourceHolder<sf::Texture, std::string> textures_holder;
//Rest of the code
I'd like to know what is causing this error and maybe help others who may experience similiar frustrating problems. Thanks for help.
EDIT :
As suggested in the comment I've declared a few extern int variables in the Dirt_Block.h so now it looks like this :
//...
extern int test_int_up;
extern sf::Vector2u screenRes;
extern thor::ResourceHolder<sf::Texture, std::string> textures_holder;
extern int test_int_d;
//...
And then assinged to them some value in main.cpp :
//...
test_int_up = 55;
test_int_d = 55;
//Adding textures to the texture library
textures_holder.acquire("Dirt", thor::Resources::fromFile<sf::Texture>("Textures\\dirt_block.png"));
//...
But the compiler gives error :
main.cpp|9|error: 'test_int_up' does not name a type
main.cpp|10|error: 'test_int_d' does not name a type
main.cpp|12|error: 'textures_holder' does not name a type
Much less distracting to see what your problem is without all the extraneous code!
C++ programs don't start from the top of the file and run code down to the bottom. They start at the main(), and control flow proceeds from there, with one thing triggering another.
(Note: That doesn't take into account global constructor ordering, which does go in order of declaration--but you have no guarantee of the order declarations from "different files" might run in.)
Point being, you can't just make random function or method calls in the middle of a file. That's where you put declarations. You have to be inside of a function or method to make calls, e.g.
int main() {
textures_holder.acquire(
"Dirt",
thor::Resources::fromFile<sf::Texture>("Textures\\dirt_block.png")
);
...
}

Moving function to different file

I had a function to get points inside a 3D arc working inside a file but, trying to order the code into the appropriate folders, I broke it, and I don't know why because I think I am including it correctly.
Originally a lot of calculations were inside a file "messages.cpp", but it is supposed to have just the messages and the calculations should be in Calculations folder.
So now messages.cpp is something like this:
#include "../../FGProcessorModule/Calculations/ArcToPoints.h"
namespace myNamespace {
void MsgProvider::onEvent{
std::vector<Formulas::LLPoint> allPoints = ArcToPoints(center, start, end);
}
}
In "../../FGProcessorModule/Calculations/ArcToPoints.h" I have:
#ifndef ARCTOPOINTS_H
#define ARCTOPOINTS_H
#include blablabla
namespace myNamespace{
std::vector<Formulas::LLPoint> ArcToPoints (Formulas::LLPoint, Formulas::LLPoint, Formulas::LLPoint);
}
#endif /* ARCTOPOINTS_H */
And finally in "../../FGProcessorModule/Calculations/ArcToPoints.cpp" I have:
#include "ArcToPoints.h"
namespace myNamespace{
std::vector<Formulas::LLPoint> ArcToPoints (Formulas::LLPoint center, Formulas::LLPoint start, Formulas::LLPoint end){
//Lots of calculations
}
}
I think everything is OK but I receive this error when I want to compile:
undefined reference to `FVIS::ArcToPoints(Formulas::LLPoint,
Formulas::LLPoint, Formulas::LLPoint)'
I don't know if this will help others as it is a very specific problem, this is a project using ros and catkin.
I was missing including the new created file in CMakeLists.txt. Now it compiled just fine.

Trying to use Kabsch algorithm but keep getting this error

This is my code:
#include <eigen3/Eigen/Geometry>
#include <stdio.h>
int main(){
Eigen::Matrix3Xd in(3, 100, 100), out(30, 100, 0);
Eigen::Affine3d A;
A = Find3DAffineTransform(in,out);
return 0;
}
and I am using the functions provided by wiki:
https://github.com/oleg-alexandrov/projects/blob/master/eigen/Kabsch.cpp#L4
Basically, I was trying to get the rotation matrix based on the input and output points given.
And this the error I got:
Well, from your code it does not seem that you are actually using this code from github.
You should either copy-paste the contents of Kabsch.cpp to your application above main (this is a quick'n'dirty solution), or you should:
Add Kabsch.cpp to your build (meaning it should be compiled and linked with your main file
Forward declare Find3DAffineTransform function above your main (or put the declaration in a separate hpp file and include it from your main file

How can contents of a namespace be made available to an entire project without generating Multiply Defined Symbol errors?

I am attempting to make a fairly large video game, and my current method of handling mouse input is to pass mouseX, mouseY, and an enum mouseState as arguments to the update function of EVERY single object that requires knowledge of the mouse. It's fairly messy, and I want to make the mouse variables more global by putting them in a namespace called Input so that I can access them with Input::mouseX, et al.
As it stands now, The namespace Input exists in Input.h (contents below)
#pragma once
#include "allegro5\allegro.h"
#include "J_Enum.h"
namespace Input{
ALLEGRO_EVENT_QUEUE *inputQueue;
int mouseX;
int mouseY;
MOUSE_STATE mouseState;
void setUpInput();
void updateInput();
};
and the two member functions are defined in Input.cpp
#include "Input.h"
void Input::setUpInput(){...declaration
void Input::updateInput(){...''
Upon including Input.h in the main loop object's header Core.h the linker throws a hissy fit because in its eyes everything included in Input.h is now a Multiply Defined Symbol.
Clearly something is wrong with my use of header files, because to my knowledge, I haven't made any glaring mistakes in my use of namespaces and the error code prefix of LNK2005 seems to implicate the linker(?).
If anyone can possibly shed some light on my dilemma I would be most grateful
Declare the variables as extern:
// header file:
namespace Input {
extern int mouseX;
}
// implementation
#include "input.h"
namespace Input {
int mouseX;
}