Using MATLAB API in C++, matOpen not working - c++

I am writing a piece of code to access MATLAB files, my program compiles but crashes when I call matOpen. When I try to debug the code, the debugger also exits without reaching the offending line of code. I am working in Qt, and I am not too sure if I have done my includes properly.
.pro file
INCLUDEPATH += "C:\Program Files\MATLAB\R2018a\extern\include"
LIBS += "C:\Program Files\MATLAB\R2018a\extern\lib\win64\microsoft\libmx.lib"
LIBS += "C:\Program Files\MATLAB\R2018a\extern\lib\win64\microsoft\libmat.lib"
SOURCES += \
main.cpp
main.cpp
#include <stdlib.h>
#include <vector>
#include <mat.h>
int main(int argc, char *argv[])
{
MATFile *mfPtr;
const char *file = "data.mat";
mfPtr = matOpen(file,"r"); //code runs successfully without this line
return 0;
}
I am compiling using Desktop Qt 5.11.0 MSVC2017 64-bit and my MATLAB version is 2018a. I have also tried deleting the build folder and rebuilding.

Try this
E:\CodePath>gcc *.c -IC:\PROGRA~1\MATLAB\R2018a\extern\include -LC:\PROGRA~1\MATLAB\R2018a\extern\lib\win64\mingw64 -o code -l libmat -l libmx
Place libmat.dll and libmx.dll in the CodePath

I could not get it to work with the Matlab libraries either. For everyone looking for a different approach to write MAT files from C++, I recommend this open source project: https://github.com/jkriege2/TinyMAT

Related

How to compile a Qt program without qtCreator on Windows?

I have read the question Can I use Qt without qmake or Qt Creator? which is basically the same for Linux, and very useful.
How to compile a basic program using QtCore (console application, even without GUI) on Windows, without using qmake or qtCreator IDE, but just the Microsoft VC++ compiler cl.exe?
For example, let's say we have:
#include <iostream>
#include <QtCore>
int main()
{
QVector<int> a; // Qt object
for (int i=0; i<10; i++)
a.append(i);
std::cout << "hello";
return 0;
}
Using:
call "C:\path\to\vcvarsall.bat" x64
cl main.cpp /I D:\coding\qt\qtbase-everywhere-src-5.15.5\include
fails with:
D:\coding\qt\qtbase-everywhere-src-5.15.5\include\QtCore\QtCore(3): fatal error C1083: Cannot open include file: 'QtCore/QtCoreDepends': No such file or directory
Indeed this file is not present in the release qtbase-everywhere-opensource-src-5.15.5.zip from https://download.qt.io/archive/qt/5.15/5.15.4/submodules/.
TL;DR: More generally, which cl.exe arguments should we use to to able to use all Qt includes, and effectively compile such a minimal project using QtCore?
I finally managed to do it 100% from command line, without the qtCreator IDE, but not yet without qmake. Steps to reproduce:
Let's assume Microsoft MSVC 2019 is installed.
Install qt-opensource-windows-x86-5.14.2.exe. (This is the latest Windows offline installer I could find), double check that you install at least msvc2017_64.
Note: Don't use qtbase-everywhere-opensource-src-5.15.4.zip: using the include subfolder from this package for cl.exe /I ... is not enough. (I thought it would, at first)
Create a folder example containing the main.cpp file above
Open a command line window in this folder and do:
vcvarsall.bat x64
Now either do "c:\path\to\msvc2017_64\bin\qmake.exe" -project to create a example.pro project file or create it manually with:
TEMPLATE = app
TARGET = qt_example
INCLUDEPATH += .
CONFIG += console
SOURCES += main.cpp
Do "c:\path\to\msvc2017_64\bin\qmake.exe". This will create a Makefile file.
Run nmake. This is Microsoft MSVC's equivalent of the make tool.
Copy c:\path\to\msvc2017_64\bin\Qt5Core.dll into the release folder
Run release\example.exe. Working!
Addendum: here is solution now for a minimal GUI app:
main.cpp
#include <QtCore/QCoreApplication>
#include <QTextStream>
#include <QMessageBox>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMessageBox::information(NULL, "Hello", "Hello", "Ok");
return a.exec();
}
qt_example_gui.pro
TEMPLATE = app
TARGET = qt_example_gui
INCLUDEPATH += .
SOURCES += main.cpp
QT += gui widgets
Do the vcvarsall.bat x64, qmake, nmake like in the solution above. No be sure you have this file structure:
release\qt_example_gui.exe
release\Qt5Core.dll
release\Qt5Gui.dll
release\Qt5Widgets.dll
release\platforms\qwindows.dll
Run the .exe, that's it!

Add SFML (third party library) to C++ project on Xcode

I'm learning how to build a simple UI in C++ on my Mac (OS 11.6) using Xcode.
As first step I'm compiling the "Hello world" program, my problem is that the build on Xcode fails but write my own command from terminal, instead, works.
This is the program, I'm using SFML :
#include <iostream>
#include "SFML/Graphics.hpp"
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
I have no error here but when launching Run from Xcode this is the output, in Graphics.hpp file :
#include <SFML/Window.hpp>. //'SFML/Window.hpp' file not found
#include <SFML/Graphics/BlendMode.hpp>
#include <SFML/Graphics/CircleShape.hpp
//other header files
This is how the project is structured ("TestGui" is the project name) :
-TestGui.xcodeproj
-TestGui(folder)
--SFML(directory with all headers file available
-- main.cpp
SFML source code here
So I tried to compile it with my own hands from terminal with :
g++ main.cpp -I ./SFML -o main
and
clang++ main.cpp -I ./SFML -o main
In both cases it compiled, also run worked.
Since the error is linked to a file not found I tried to tell it where libraries are located, so in Xcode from Product->Scheme->Edit Scheme->Run->Arguments->Arguments passed on launch : added -I ./SFML. But the error is still alive.
Added SFML folder to targets from Xcode, didn't copy-pasted but maybe I did it wrong, this is my first time.
EDIT : SFML folder:
--SFML
--- many .hpp files
--- 5 folders (Audio, Graphic, Network, System and Window)
I tried to add also this argument : -L ./SFML but nothing.

Qt OpenCV programm exits with code -1073741515

What i want to do
I try to use opencv and run a short example program.
Problem
It exits with code -1073741515. I never managed to use openCV on any of the 4 systems i tried, so i do something wrong but don't know what.
I had diffrenet errors using different attempts (undefined reference, crashing programm without error code, etc.), this here is the just simplest example.
What i figured out
Another post told me, that -1073741515 is equal to 0xC0000135 meaning STATUS_DLL_NOT_FOUND.
What i tried
The programm crashes, if include #include <opencv2/core/core.hpp>,
#include <opencv2/highgui/highgui.hpp>and
#include <opencv2/imgproc/imgproc.hpp>. When i comment them out, it runs wnd returns 0 as intended. I don't even need to use anything like cv::Mat or something to make it crash. That seems strange, because the code completion suggests me stuff from openCV, so it somehow sees it. Including #include <opencv2/opencv.hpp> instead leads to the same error code. Including something beginning with opencv (not opencv2) works, for example #include <opencv/cv.hpp>, but has not enough functionality.
Source file
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace std;
int main()
{
cv::Mat InputImage = cv::imread("C:/Users/Public/Pictures/Sample Pictures/Chrysanthemum.jpg");
if(!InputImage.empty())
cv::imshow("test", InputImage);
else
cout << "imgage is empty" << endl;
return 0;
}
Project file
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
INCLUDEPATH += D:/opencv/build/include
LIBS += -LD:/opencv/build/x64/vc14/lib
LIBS += -lopencv_world343d
LIBS += -lopencv_world343
SOURCES += \
main.cpp
Relevant folders
Include path:
libs:
project:
build (copied the libs to the release/debug folders too):
Versions
QT 5.10.1
MSVC17 64bit Compiler
Windows 7 Enterprise 64bit
openCV 3.4.3 (vc14/vc15)
As Jiu pointed out in the comment above, the solution was simple:
Copy the .dll not the .lib and it works.

QtCreator LNK2019 error with external library

I have a problem when I want to link a library to my Qt project.
When I try to include an external library (libnodave.lib) in Qt Creator and try to build it, the following error occurs.
main.obj:-1: Fehler: LNK2019: unresolved external symbol __imp_daveSetDebug referenced in function main
I'm pretty sure that I included all needed files in my project and the .pro file. I used the "Add Library" wizard to add the library.
After no success with Qt Creator, I created a minimal example with Visual Studio. When I include all the needed files to the VS project, I can build and run it without errors. So I think that there must be a problem with Qt Creator linking the library. I also tried the Qt-Visual-Studio-Add-in, but there, the same error occurs.
Here are my minimal examples with the library I want to include.
In the Visual Studio example, I added the library path, the include path, and the name of the library to the project properties. It works.
I hope you can help me with my problem.
EDIT:
I want to use the library to get some data from a S7-300 SPS device.
The following code is the minimal example from Qt Creator.
#include <QCoreApplication>
#include <QDebug>
#include <nodave.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
daveInterface *di;
daveSetDebug(daveDebugConnect); // Function of libnodave Library
qDebug() << "Hello World";
return a.exec();
}
This is the whole code from the Visual Studio minimal example.
#include "stdafx.h"
#include <nodave.h>
int _tmain(int argc, _TCHAR* argv[])
{
daveInterface *di;
daveSetDebug(daveDebugConnect);
printf("Hello World\n");
return 0;
}
The code is very small, so I don't think that there is an error inside.
That's why I think it must be a problem with the Qt linker or something like that.
EDIT:
My .pro file.
QT += core
QT -= gui
TARGET = qtminimal
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
win32: LIBS += -L$$PWD/../libnodave-0.8.5/win/ -llibnodave
INCLUDEPATH += $$PWD/../libnodave-0.8.5
DEPENDPATH += $$PWD/../libnodave-0.8.5
The problem was that the Qt project is 64bit and the library I want to include is only 32bit.
So I downloaded the 32bit version of Qt and now it works.
I found the mistake, when I tried to build only the minimal example with libnodave, without any 64bit Qt libraries.
By creating a new Qt project in VS2013, with this workaround and adding the libnodave library afterwards I could change whether it should be a 64bit or 32bit build. By choosing the 32bit build, the Qt library creates errors but not the libnodave lib. When I choose 64bit build, only libnodave creates the errors.
I hope it is useful for someone else.

Portaudio example "record_file.c" does not find "min"

First off, I alread got the smaller example paex_record.c to work.
I use MinGW on Windows8 and did compile portaudio from source, that's were I got the used libportaudio-2.dll from.
I set up a project in QtCreator (without gui) with following .pro-file:
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
LIBS += -LC:/Audio/patest/paex_record_file -llibportaudio-2
My folder C:/Audio/patest/paex_record_file looks like:
libportaudio-2.dll
main.cpp
pa_ringbuffer.c
pa_ringbuffer.h
pa_util.h
paex_record_file.pro
paex_record_file.pro.user
portaudio.h
I copied both pa_ringbuffer and pa_util to this folder after they were not found.
Main.cppcontains the whole example file paex_record_file.c from the portaudio source.
There is an error when calling the ringbuffer (line 246 and 271: source):
"min" was not declared in this scope
I thought this example would directly run just like the record.c example.
Do I need to include further packages? I've tried algorithm, math, std and using namespace std, but still the error occurs. But I get the feeling it should work out off box, perhaps my include files or folder setup / linkage is not ok?
EDIT: Ok so I just defined a min-function on my own. Now it throws different error:
undefined reference to "PaUtil_GetRingBufferReadAvailable"
and a couple more of this kind. There is probably a lib missing to be linked, having a look...
I also had this linking error trying to compile paex_record_file.c. Turns out you need to compile pa_ringbuffer.c before compiling the main file. There's also an external dependency on some alloc function, so you need to change 2 lines in the file.
I'm not savvy of qt but you need to do something like:
SOURCES += pa_ringbuffer.c main.cpp
Here's how I compile with MinGW:
Changes inside paex_record_file.c
Line 313 : data.ringBufferData = (SAMPLE *) malloc( numBytes );
Line 443 : free( data.ringBufferData );
Compile with MinGW:
gcc -o p_rec.exe C:\path_to_portaudio\portaudio\common\pa_ringbuffer.c \
paex_record_file.c -lportaudio \
-IC:\path_to_portaudio\portaudio\src\common
I didn't get the min problem, but perhaps you can try to remove the ifdef guard and try to recompile.
#ifdef _WIN32
#include <windows.h>
#include <process.h>
#endif
to
#include <windows.h>
#include <process.h>