How to use Octave libraries with C++ - qt-creator

This is my first question here, so sorry in forward if it is not well formulated or stupid.
I am trying to use the octave libraries with C++
I am using Qt creator on Ubuntu (linux noob)
#include "octave/oct.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Matrix matrix(3,4);
return a.exec();
}
At first I got some undefined references errors. I figured out the program is missing libraries, so I looked for the library "liboctave.so". I found it in usr/lib/octave-3.2.4. To be more precise there was a symbolic link named "liboctave.so" pointing to "liboctave.so.3.2.4" file. I used the QtCreators "add Library" feature to add the library to my project. The program generated this code in my .pro file
unix:!macx:!symbian: LIBS += -L$$PWD/../../../../usr/lib/octave-3.2.4/ -loctave
INCLUDEPATH += $$PWD/../../../../usr/lib/octave-3.2.4
DEPENDPATH += $$PWD/../../../../usr/lib/octave-3.2.4
The program built without error. Not even complaining about undefined references. But when I run it I get
Starting /home/martin/Projects/test-build-desktop/test...
/home/martin/Projects/test-build-desktop/test: error while loading shared libraries: liboctave.so: cannot open shared object file: No such file or directory
/home/martin/Projects/test-build-desktop/test exited with code 127
I cannot figure out why it cannot find the file. I am looking at the file with my bare eyes.
I figured out that the problem may be permission, so I copied the "liboctave.so.3.2.4" file to the project location, renamed it "liboctave.so" and added all permissions for everybody. Then added this library using the Qtcreator "add library" feature and I still get the same error.
Please help me

The liboctave is not installed in standard location, when you compile it your povide
a parameter -L$$PWD/../../../../usr/lib/octave-3.2.4/ however in the run
time it is not known.
So you have two options:
Provide environment variable LD_LIBRARY_PATH=/full/path/to/usr/lib/octave-3.2.4 and then run the program:
export LD_LIBRARY_PATH=/full/path/to/usr/lib/octave-3.2.4
Hardcode the path withing excutable using an additional option: -Wl,-rpath=$$PWD/../../../../usr/lib/octave-3.2.4/
And it would search for it in this location.

Related

CLion and Qt Framework

So I figured out how to get Qt Framework working with CLion, but I don't have Qt Designer in CLion. How can I make a GUI in CLion using Qt Framework? Every tutorial i've seen uses Qt Creator so I cannot get an answer.
I worked with QtCreator and then with Qt5 libraries on Clion.
For a better understanding see my project at https://github.com/FraScandiffio/Timer_Laboratorio
Search for MyTimerApp.ui .h .cpp files.
1)You need a .ui file: it was created for me by QtCreator: when i worked on Clion i just copied the file into my project folder and add in the SOURCES_FILES.
You can download it from my project: it's the MyTimerApp.ui file but you have to change just 3 words to adapt it to your needs.
Re-name the file with the name that you prefer. Now open the file with a text editor and replace all the "MyTimerApp" with the name that you have just chosen.
2)Add this in your Cmake.txt (the explanation of why you have to do it is here)
cmake_minimum_required(VERSION 3.3)
project({your_project_name})
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(SOURCE_FILES main.cpp MyApp.ui #...)
#... the rest of your cmake.txt ...
Download also MyTimerApp.cpp and MyTimerApp.h files. (i suggest you to create a new .h file and paste into them the code instead of refactor my file)
Open it and replace "MyTimerApp" whit the same name you have chosen before.
obviously add them into the SOURCE_FILES list.
In the main.cpp type
#include "nameyouhavechoosen.h"
#include "QApplication"
#include "QWidget"
int main(int argc, char *argv[]){
QApplication app(argc, argv);
QWidget* MyWidget= new {nameyouhavechoosen};
MyWidget->setGeometry(500,200,617,416);
app.setActiveWindow(MyWidget);
MyWidget->show();
return app.exec();
}
If you run it you should see a very simple window.
In my github repository you can find how to create buttons/timer and more. Anyway Qt documentation is great for learning.

XCode with pkg-config

Hi I am using gtkmm in an XCode project and I am wondering how I can include the headers without using the command line and adding them in manually. Is there a way to using pkgconfig in XCode without doing it this way, ie some extra params or something?
Any help would be appreciated.
May be look here
You can use pkg-config, if a .pc file for that package is present. These files are generally located at /usr/share/pkgconfig. You can prepare your own .pc file also by writing the header files directory and library directory in a specified format.
Just have a look at the contents of a .pc file and it it intuitive to understand.
I know that this is old, but I was able to get gtkmm working in Xcode 6 so I figured I'd share.
Assuming that gtkmm and Xcode 6 has been installed on your mac, the following steps should allow you to add gtkmm to an existing Xcode (c++) project.
Run pkg-config <pkgconfig file name> —cflags —libs in the terminal. This command returns the linker flags/compiler flags Xcode will need. I have gtkmm 2.24.2_1 installed on my machine so for me the command is pkg-config gtkmm-2.4 —cflags —libs for me. I cannot guarantee that this works for other versions.
To determine the name of your .pc file, mine was located inside /usr/local/Cellar/gtkmm/2.24.4_1/lib/pkgconfig
The terminal command should return all the required flags, here's what mine returns:
-D_REENTRANT -I/usr/local/Cellar/gtkmm/2.24.4_1/include/gtkmm-2.4 -I/usr/local/Cellar/gtkmm/2.24.4_1/lib/gtkmm-2.4/include -I/usr/local/Cellar/atkmm/2.22.7/include/atkmm-1.6 -I/usr/local/Cellar/gtk+/2.24.28_2/include/gtk-unix-print-2.0 -I/usr/local/Cellar/gtk+/2.24.28_2/include/gtk-2.0 -I/usr/local/Cellar/gtkmm/2.24.4_1/include/gdkmm-2.4 -I/usr/local/Cellar/gtkmm/2.24.4_1/lib/gdkmm-2.4/include -I/usr/local/Cellar/glibmm/2.44.0/include/giomm-2.4 -I/usr/local/Cellar/glibmm/2.44.0/lib/giomm-2.4/include -I/usr/local/Cellar/pangomm/2.36.0/include/pangomm-1.4 -I/usr/local/Cellar/pangomm/2.36.0/lib/pangomm-1.4/include -I/usr/local/Cellar/glibmm/2.44.0/include/glibmm-2.4 -I/usr/local/Cellar/glibmm/2.44.0/lib/glibmm-2.4/include -I/usr/local/Cellar/cairomm/1.11.2_1/include/cairomm-1.0 -I/usr/local/Cellar/cairomm/1.11.2_1/lib/cairomm-1.0/include -I/usr/local/Cellar/libsigc++/2.4.1/include/sigc++-2.0 -I/usr/local/Cellar/libsigc++/2.4.1/lib/sigc++-2.0/include -I/usr/local/Cellar/gtk+/2.24.28_2/include/gtk-2.0 -I/usr/local/Cellar/gtk+/2.24.28_2/lib/gtk-2.0/include -I/usr/local/Cellar/pango/1.36.8_1/include/pango-1.0 -I/usr/local/Cellar/atk/2.16.0/include/atk-1.0 -I/usr/local/Cellar/cairo/1.14.2_1/include/cairo -I/usr/local/Cellar/pixman/0.32.6/include/pixman-1 -I/usr/local/Cellar/fontconfig/2.11.1/include -I/usr/local/Cellar/freetype/2.5.3_1/include/freetype2 -I/usr/local/Cellar/freetype/2.6_1/include/freetype2 -I/usr/local/Cellar/libpng/1.6.17/include/libpng16 -I/usr/local/Cellar/gdk-pixbuf/2.30.8/include/gdk-pixbuf-2.0 -I/usr/local/Cellar/libpng/1.6.17/include/libpng16 -I/usr/local/Cellar/glib/2.44.1/include/glib-2.0 -I/usr/local/Cellar/glib/2.44.1/lib/glib-2.0/include -I/usr/local/opt/gettext/include -L/usr/local/Cellar/gtkmm/2.24.4_1/lib -L/usr/local/Cellar/atkmm/2.22.7/lib -L/usr/local/Cellar/gtk+/2.24.28_2/lib -L/usr/local/Cellar/gtkmm/2.24.4_1/lib -L/usr/local/Cellar/glibmm/2.44.0/lib -L/usr/local/Cellar/pangomm/2.36.0/lib -L/usr/local/Cellar/glibmm/2.44.0/lib -L/usr/local/Cellar/cairomm/1.11.2_1/lib -L/usr/local/Cellar/libsigc++/2.4.1/lib -L/usr/local/Cellar/gtk+/2.24.28_2/lib -L/usr/local/Cellar/pango/1.36.8_1/lib -L/usr/local/Cellar/atk/2.16.0/lib -L/usr/local/Cellar/cairo/1.14.2_1/lib -L/usr/local/Cellar/gdk-pixbuf/2.30.8/lib -L/usr/local/Cellar/glib/2.44.1/lib -L/usr/local/opt/gettext/lib -lgtkmm-2.4 -latkmm-1.6 -lgtk-quartz-2.0 -lgdkmm-2.4 -lgiomm-2.4 -lpangomm-1.4 -lglibmm-2.4 -lcairomm-1.0 -lsigc-2.0 -lgtk-quartz-2.0 -lgdk-quartz-2.0 -lpangocairo-1.0 -lpango-1.0 -latk-1.0 -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lintl
Now in Xcode open your project file and select your target, within build settings you then need to copy the flags you got from the terminal into “Other Linker Flags” and “Other C Flags”
At this point you should be able to run, compile, build, etc. c++ projects that use gtkmm in Xcode.
Answer based on the details from user2618142's answer + link
Also just as an aside, if you are getting the error
no matching constructor for initialization of 'GTK::Main'
on the line Gtk::Main kit( argc, argv ) it is likely because Xcode by default creates a main like this int main(int argc, const char * argv[]) but gtkmm uses argv as a non const parameter, so you need to change your main function to int main(int argc, char * argv[]).

Getting Started with C development and GTK+

I'm really a Python developer exclusively, but I'm making my first foray into C programming now, and I'm having a lot of trouble getting started. I can't seem to get the hang of compilation and including libraries. At this point, I'm just identifying the libraries that I need and trying to compile them with a basic "hello, world" app just to make sure that I have my environment setup to do the actual programming.
This is a DBus backend application that will use GIO to connect to DBus.
#include <stdlib.h>
#include <gio/gio.h>
int
main (int argc, char *argv[])
{
printf("hello, world");
return 0;
}
Then, I try to compile:
~$ gcc main.c
main.c:2:21: fatal error: gio/gio.h: No such file or directory
#include <gio/gio.h>
I believe that I've installed the correct packages as indicated here, and gio.h exists at /usr/include/glib-2.0/gio/gio.h.
I found a command online to add a search directory to gcc, but that resulted in other errors:
~$ gcc -I /usr/include/glib-2.0/ main.c
In file included from /usr/include/glib-2.0/glib/galloca.h:34:0,
from /usr/include/glib-2.0/glib.h:32,
from /usr/include/glib-2.0/gobject/gbinding.h:30,
from /usr/include/glib-2.0/glib-object.h:25,
from /usr/include/glib-2.0/gio/gioenums.h:30,
from /usr/include/glib-2.0/gio/giotypes.h:30,
from /usr/include/glib-2.0/gio/gio.h:28,
from main.c:2:
/usr/include/glib-2.0/glib/gtypes.h:34:24: fatal error: glibconfig.h: No such file or directory
#include <glibconfig.h>
^
compilation terminated.
There has to be some relatively simple method for being able to set some options/variables (makefile?) to automatically include the necessary headers. I'm also going to use Eclipse-CDT or Anjuta as an IDE and would appreciate help to fix the import path (or whatever it's called in C).
Any help is greatly appreciated.
Use pkg-config (and make). See exactly this answer to a very similar question. See also this and that answers. Don't forget -Wall -g flags to gcc ..
You don't need an IDE to compile your code (the IDE will just run some gcc commands, so better know how to use them yourself).

setting up OpenCV 2.4.2 with Qt Creator

I tried two methods to use opencv with qt creator
first one using Mingw where the dlls and .dll.a files are already downloaded with the opencv library and I just add reference to the .dll.a files in the .pro file as follow
INCLUDEPATH += D:\\OpenCV\\opencv\\build\\include
LIBS += D:\\OpenCV\\opencv\\build\\x64\\mingw\\lib\\libopencv_calib3d242.dll.a
LIBS += D:\\OpenCV\\opencv\\build\\x64\\mingw\\lib\\libopencv_contrib242.dll.a
LIBS += D:\\OpenCV\\opencv\\build\\x64\\mingw\\lib\\libopencv_core242.dll.a
LIBS += D:\\OpenCV\\opencv\\build\\x64\\mingw\\lib\\libopencv_features2d242.dll.a
I have a simple code to test opencv:
#include <QtCore/QCoreApplication>
#include <opencv/cv.h>
using namespace cv;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Mat image;
return a.exec();
}
but I got a build issues as follow
C:\Users\Kato\Documents\QT projects\QtOpenCVYaRab\debug\main.o:-1: In function ~Mat':
d:\OpenCV\opencv\build\include\opencv2\core\mat.hpp:278: error: undefined reference tocv::fastFree(void*)'
d:\OpenCV\opencv\build\include\opencv2\core\mat.hpp:367: error: undefined reference to `cv::Mat::deallocate()'
:-1: error: collect2: ld returned 1 exit status
Here is some of the compile output:
Running build steps for project QtOpenCVYaRab...
Configuration unchanged, skipping qmake step.
Starting: "C:\QtSDK\mingw\bin\mingw32-make.exe"
C:/QtSDK/mingw/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory `C:/Users/Kato/Documents/QT projects/QtOpenCVYaRab'
g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL
d:/OpenCV/opencv/build/include/opencv2/core/mat.hpp:278: undefined reference to `cv::fastFree(void*)'
debug/main.o:d:/OpenCV/opencv/build/include/opencv2/core/mat.hpp:367: undefined reference to `cv::Mat::deallocate()'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug\QtOpenCVYaRab.exe] Error 1
mingw32-make: *** [debug] Error 2
The process "C:\QtSDK\mingw\bin\mingw32-make.exe" exited with code 2.
Error while building project QtOpenCVYaRab (target: Desktop)
When executing build step 'Make'
the second method is using cmake to compile the opencv library the using visual studio 2010 to build it and add references to the files in the bin folder but I got almost the same building issues.
#include "iostream"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main()
{
IplImage *image = cvLoadImage("C:\\lena.jpg");
Mat im(image);
imshow("TEST",im);
waitKey();
return 0;
}
this is ur main.cpp...the above programme displays the picture of lena...use double backslashes for indicating change of directory on windows platform...some how the imread does work for me so i have loaded the image as IplImage and casted it to Mat...u cn do the following also..
IplImage *image = cvLoadImage("C:\\lena.jpg",1);
cvShowImage("TEST",image);
cvWaitKey();
your .pro file should have the following lines as mentioned earlier...
INCLUDEPATH += D:\OpenCV\opencv\build\include
LIBS +=-LD:\OpenCV\opencv\build\x64\mingw\lib\
-lopencv_core242\
-lopencv_highgui242\
-lopencv_imgproc242\
-lopencv_video242\
and your system variable named path should have
D:\Opencv2.4.2\opencv\build\x86\vc9\bin (if you have Qt 4.8.1 for
desktop MSVC2008 (QtSDK) Debug as your target)
D:\Opencv2.4.2\opencv\build\x86\mingw\bin (if your target is based on Qt MinGW x86 )
D:\OpenCV2.4.2\opencv\build\common\tbb\ia32\vc9(mingw) (i have added this coz it was showing some weird errors...u can try it)
after editing the path variable close the Qt ide/application and restart it for the system variable change to get reflected..
INCLUDEPATH += D:\OpenCV\opencv\build\include
LIBS +=-LC:\OpenCV\opencv\build\x64\mingw\lib\
-lopencv_core242\
-lopencv_highgui242\
-lopencv_imgproc242\
-lopencv_video242\
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main()
{
}
if u are running it as an console application then no need to include QtCore/QtApplication
You have to specify your library path with -L and then add the library file with
-l<libname_without_extension>
For unix (installed in default place):
unix: LIBS += -lopencv_core
unix: LIBS += -lopencv_highgui
For windows (your problem):
win32: LIBS += -L C:\OpenCV2.3\opencv\build\gpu\x64\lib\ -lopencv_core231
From the error mentioned, it seems that Opencv library is missing some files/components.Please start with the simple program for Opencv. This program just display the camera image.
Please follow the given link.
http://linux.softpedia.com/get/Multimedia/Graphics/qwebcam-38246.shtml
Download the source code for qwebcam and follow the instruction to setup Opencv. This is a very simple source code and works fine (tested on Linux-os).
I recently tried with Opencv & this link was quiet useful for me to begin with.
Hope you will be able to resolve your error through this code.
I ran into the same problem, but altering the .pro manually didn't work for me. Eventually I found a simple solution to connect the openCV to Qt. I posted about it a few other threads, https://stackoverflow.com/a/51914928/10245006 and have included the information below.
The steps listed below are found in the Qt5 documentation: [http://doc.qt.io/qtcreator/creator-project-qmake-libraries.html][1] under the "To Add Library" section.
Right click on the project file located in the 'project pane' on the left side of the creator... and select "Add Library..."
Follow the instructions of the wizard
Let me add some specificity from here...
Select "External Library"
For the "Library File" navigate to your opencv_worldXXX.lib file (or opencv_worldXXXd.lib file, you will notice that by specifying only one or the other the wizard has a checkbox which includes the other automatically) [ex. ...\opencv\build\x64\vc12\lib\opncv_world310.lib]
For the "Include Folder" navigate to the "include" folder within the build. [ex. ...\opencv\build\include]
Select your operating system, dynamic/static library (whichever is appropriate)
Hit NEXT, CLEAN UP, and RUN!

Compiling openCV 2.3.1 programs with MinGW gcc/g++ on Windows 7 64bit

For a week I've been struggling with compiling openCV programs. I've tried everything I could possibly find on the internet.
What I did is: I've downloaded OpenCV-2.3.1-win-superpack.exe and followed this official installation guide.
In the CMake (gui) my source was: D:\opencv and build destination was: C:\opencv.
I've also added C:\opencv\install\bin;C:\opencv\bin to my system's PATH variable.
What I want is to compile openCV programs on my Windows OS using MinGW's gcc/g++ compilers.
I've tried various gcc/g++ parameters that I've found on the internet and days playing with the -I and -L options the compiler can never find the openCV functions or structures.
What I am trying to compile:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
int main(int argc, char *argv[])
{
// Nothing but create a window
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
cvMoveWindow("mainWin", 100, 100);
cvWaitKey(0);
return 0;
}
Error:
Input:
gcc test.c -o test -I"C:\opencv\install\include" -I"C:\opencv\install\include\opencv" -L"C:\opencv\install\bin"
Output:
...\ccK4MfHv.o:test.c:(.text+0xa0b): undefined reference to `cvFree_'
Or with g++:
Input:
g++ test.c -o test -I"C:\opencv\install\include" -I"C:\opencv\install\include\opencv" -L"C:\opencv\install\bin"
Output:
...\ccXCTKa1.o:test.c:(.text+0x1e): undefined reference to `cvNamedWindow'
Side note: trying to compile in VS2005 I get the same error.
Thank you for your time!
In case someone else needs to solve this issue, here's how I got the posted OpenCV/HighGUI sample code to compile in Windows 7 x64 using MinGW, MSYS, and CMake:
build OpenCV from source using MinGW/MSYS/CMake. This is because I could not get the MinGW compiled version in the OpenCV-win-SuperPack to link properly in MinGW/MSYS/Windows 7 x64.
For full reference, here's how I compiled OpenCV:
make sure you have an up-to-date CMake (v2.6 or later) and MinGW (with GCC, G++, and MSYS options) installed
if you want the new Qt-based OpenCV HighGUI front-end, you will need to install Qt 4 (SDK).
download a OpenCV source/superpack version 2.2 or later (I used OpenCV-2.3.1-win-superpack.exe)
unzip the contents to [OPENCV_SOURCE_DIR] (I put it in C:/opencv, so there should be a file at C:/opencv/README for example)
create a [OPENCV_BUILD_DIR] directory elsewhere (I used C:/opencv/build/mingw)
use the CMake-GUI tool, specify the source directory as [OPENCV_SOURCE_DIR], the build directory as [OPENCV_BUILD_DIR], and click "Configure".
you may wish/need to go tweak the options (e.g. I ticked "Qt" and "Qt-OpenGL" entries, then clicked "Configure" again, then had to provide the path to the qmake executable)
once you have finished configuring OpenCV, click "Generate"
in a MSYS terminal, browse to [OPENCV_BUILD_DIR], and run "make" to build the code (this may take a while)
once the has been built properly, run "make install", which collects the built code/libraries/include dirs into [OPENCV_BUILD_DIR]/install folder (or a different folder if you changed the corresponding option when using the CMake-GUI tool)
add [OPENCV_BUILD_DIR]/install/bin folder to the PATH environmental variable. If you do not know how to do this, then I'd recommend using the Path Editor GUI tool.
if you end up using Qt, you will also need to put the bin folder of Qt SDK in the PATH environmental variable. This is the folder that includes qmake.exe.
put the following sample code into a file called test.c. I modified the includes slightly to make them compatible with OpenCV v2.2 and above.
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
int main(int argc, char *argv[])
{
// Nothing but create a window
cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
cvMoveWindow("mainWin", 100, 100);
cvWaitKey(0);
return 0;
}
in a MSYS terminal, browse to the folder where you put test.c, and run:
gcc -o test -I"[OPENCV_BUILD_DIR]/install/include" test.c \
-L"[OPENCV_BUILD_DIR]/install/lib" \
-lopencv_core[OPENCV_VERSION] \
-lopencv_imgproc[OPENCV_VERSION] \
-lopencv_highgui[OPENCV_VERSION]
So in my case:
gcc -o test -I"/c/opencv/build/mingw/install/include" test.c \
-L"/c/opencv/build/mingw/install/lib" \
-lopencv_core231
-lopencv_imgproc231
-lopencv_highgui231
Path Editor: http://www.redfernplace.com/software-projects/patheditor/
You have the directory, C:\opencv\install\bin, to locate libraries on the gcc/g++ command line, but I think you'll also need to specify the libraries to use as linker inputs as well. I'm not sure what libraries are part of the OpenCV distribution, but going by the example on the instruction page you linked to, one might be:
-lopencv_calib3d220.dll
You'll probably have to add one or more other ones (that follow the name pattern lib*.a in the C:\opencv\install\bin directory - or maybe some other lib directory that you should be passing in a -L option).

Resources