CLion and Qt Framework - clion

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.

Related

use OpenCV with Clion IDE on Windows

I'm actually looking for a way to create apps with OpenCV with Clion from JetBrains.
I've installed OpenCV with Choco, so I have all the stuff in C:\opencv
this is my projet with Clion
CMakeLists.txt:
cmake_minimum_required(VERSION 3.3)
project(test)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories("C:\\opencv\\build\\include\\")
FIND_PACKAGE( OpenCV REQUIRED core highgui imgproc)
set(OpenCV_FOUND TRUE)
set(SOURCE_FILES main.cpp)
add_executable(prog ${SOURCE_FILES})
and the main.cpp:
#include <opencv2/opencv.hpp>
int main() {
cv::Mat img = cv::imread("./test.jpg", -1);
cv::imshow("Mon image", img);
cv::waitKey(0);
return 0;
}
and the response to build is :
undefined reference to `cv::imread(cv::String const&, int)'
and undefined errors for all OpenCV functions
Do you know why it doesn't works?
First of all, you need CMake.
Then you need a compiler of your choise (MinGW, Visual Studio, ...).
Download the OpenCV source files. Link
Unpack to C:\opencv (or a folder of your choice)
Open CMake and select source (directory of 2.) and build for example C:\opencv\mingw-build or C:\opencv\vs-x64-build. Choose one accoring your configuration.
Click Configure and select the generator according to you compiler. MinGW Makefiles in case of MingGW or Visual Studio ... if you are using Visual Studio and so on ...
(If you experience problems with MinGW, ensure that the minGW/bin directory is added to the evironment path labelled, 'PATH')
Wait for the configuration to be done, edit your properties of your needs (in my case I don't need tests, docs and python).
Click Configure again. When everything is white click Generate else edit the red fields.
Open cmd and dir to build directory of 3.
If you chose Visual Studio open the generated solution.
Compile the library. Run mingw32-make (or mingw64-make) or build the BUILD_ALL project from the generated solution in Visual Studio if your choosen compiler is MSVC.
This takes a while.
Once it is done, run mingw32-make install (or mingw64-make install). If you've choosen Visual Studio you need to build the INSTALL project.
This creates an install folder, where everything you need for building your own OpenCV apps is included.
To system PATH add C:\opencv\mingw-build\install\x86\mingw\bin
Restart your PC.
Set up CLion:
You need to download FindOpenCV.cmake and add it to project-root/cmake/.
CMakeLists.txt:
project(test)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# Where to find CMake modules and OpenCV
set(OpenCV_DIR "C:\\opencv\\mingw-build\\install")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(test_cv main.cpp)
# add libs you need
set(OpenCV_LIBS opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs)
# linking
target_link_libraries(test_cv ${OpenCV_LIBS})
main.cpp:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
int main(int argc, char** argv)
{
if(argc != 2)
{
std::cout << "Usage: display_image ImageToLoadAndDisplay" << std::endl;
return -1;
}
cv::Mat frame;
frame = cv::imread(argv[1], IMREAD_COLOR); // Read the file
if(!frame) // Check for invalid input
{
std::cout << "Could not open or find the frame" << std::endl;
return -1;
}
cv::namedWindow("Window", WINDOW_AUTOSIZE); // Create a window for display.
cv::imshow("Window", frame); // Show our image inside it.
cv::waitKey(0); // Wait for a keystroke in the window
return 0;
}
Build and run main.cpp.
All Paths depend on the setup you make in 2. and 3. You can change them if you like.
UPDATE 1: I updated the post for using a compiler of you choice.
SUGGESTION: Using a package manager like Conan makes life much easier.
I just want to add one more thing in the answer of daB0bby. Few Min-GW Compilers do not support C++ Version 11 or later. This version is required for thread in OpenCV. So I will suggest using TDM-GCC Compiler instead of MinGW Compiler. Install this compiler and set path C:\TDM-GCC-64\bin to the system's environmental variable.

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).

How to use Octave libraries with C++

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.

Compile errors with #include <string> in Cocoa App

I am trying to compile a Cocoa app in xcode 4.0 and I'm getting this error...
fatal error: 'string' file not found
...when trying to compile to .pch file on this line:
#include <string>
I have another xcode project that does the same thing, but does not get the error. I have scoured the build settings for some different, but I can't find one. The only difference is that the project that compiles OK was started as a command line project, not a Cocoa project, but the build setting are the same.
The target OS is Mac OS X 10.6
The error happens when compiling the precompiled header and doesn't get to any of the other files. The only framework that the compiling version has is Foundation.framework and the non-compiling one has it as well.
Why is it not finding in one project and not the other? Any advice?
What is the extension of your source files? If it is ".m", try to change it to obj-cpp ".mm", so that Xcode will deduce correct language.
Or just put c++-specific headers inside "#ifdef __cplusplus" block
Update
The guard must exist for each language compiled in the project because this specific include is in the pch. IOW, if it were all c++ and/or objc++ there would be no error. Evidently, there is at least one file that does not recognize C++ (e.g. C or ObjC sources are also compiled in the target). Therefore, you simply guard it like so:
// MONPrefix.pch
#ifdef __cplusplus
#include <string>
#endif
// same for objc, so your C and C++ sources compile with no error:
#ifdef __OBJC__
#include <Foundation/Foundation.h>
#endif
string is a C++ header (for std::string). If you are looking for stuff like strcpy you need to include string.h

Compiling FreeType to DLL (as opposed to static library)

I want to use FreeType in a c# project. I found this binding, but I still need a freetype.dll. I usually use a static library in my c++ projects, so I never compiled one. Opening the freetype-solution (VS2010) I noticed that there is no configuration for a dynamic library - just static ones. I tried to make my own configuration and got it to generate a freetype.dll. If I use it with the c#-binding I get an exception, that the FT_Init_FreeType-entry point was not found. Any idea how I must adjust the freetype-project in order to export those functions?
If you're ok with an old version (march 2008), you can go to FreeType for Windows page, download the latest Binaries package, open the .ZIP, and extract FreeType6.dll from the bin directory. Just rename it appropriately.
If you need a more recent version, here is how you can compile the latest:
download the latest source (2.4.6 as of today) from http://sourceforge.net/projects/freetype/files/freetype2/
open Visual Studio 2010, and load freetype.sln from the builds\win32\vc2010 directory.
open the project config, and in the General tab, change Configuration Type to Dynamic Library (.dll)
open the ftoption.h file, and add these lines (near the "DLL export compilation" remarks section for example):
#define FT_EXPORT(x) __declspec(dllexport) x
#define FT_BASE(x) __declspec(dllexport) x
change the project compilation configuration to "Release".
compile the project. You should now have a freetype246.dll in the objs\win32\vc2010 directory.
I'm going to bet that the problem is that your DLL project does not export any symbols, so while all the code is in there the addresses of the symbols are not in the exports table so nobody can get to them from the outside.
This question has a nice solution to export all the symbols in a .dll without having to manually list them.
The future here. This is to future readers of this thread.
FT2 supports creating a static and dynamic library. They have solutions premade and can be found in the builds directory.
If you are forced to use CMAKE, you will have to do what the accepted answer does. However, it is no longer current. I was not able to find said file which references dll (near the "DLL export compilation" remarks section for example):. It is now located at freetype-x.x.x\include\freetype\config\ftconfig.h around line 424. I am using MSVS 2017, so try to follow along.
mkdir build
cd build
cmake -G "Visual Studio 15 2017 Win64" ..
Open freetype-x.x.x\include\freetype\config\ftconfig.h and around line 424, patch the __declspec(dllexport)'s in:
...
/* You can provide your own implementation of `FT_EXPORT` and */
/* `FT_EXPORT_DEF` here if you want. */
/* */
/* To export a variable, use `FT_EXPORT_VAR`. */
/* */
// This is due to FT_EXPORT and FT_BASE not generating a .lib file, just a .dll
#ifdef FT_EXPORT
#undef FT_EXPORT
#define FT_EXPORT(x) __declspec(dllexport) x
#endif
#ifdef FT_BASE
#undef FT_BASE
#define FT_BASE(x) __declspec(dllexport) x
#endif
#ifndef FT_EXPORT
...
Open the solution generated by CMAKE called freetype.sln . Select freetype in the Class View. Project -> Properties -> General -> Target Extension -> set to .dll and under Project Defaults -> Configuration Type -> set to Dynamic Library (.dll)
Make sure Release is select and Build -> Build freetype. In the 'build' directory that has the solutions, in Release you will have your freetype.dll and freetype.lib files for use. You will need those and all of freetype-.x.x.x\include.
Good luck :)

Resources