Error using Qt 5.1.1 in code::blocks - c++

I am trying to combine Code::Blocks IDE and Qt 5.1.1 on my win7 32bit. In CB I use MinGW's g++ compiler which supports C++0x.
I downloaded "qt-windows-opensource-5.1.1-mingw48_opengl-x86-offline.exe" and installed at "D:\Qt-library\5.1.1\mingw48_32\".
Then in CB I added three tools named "qmake -project" , "qmake" and "make" at Tools->Configure tools->add.
"qmake -project" with Executable "D:\Qt-library\5.1.1\mingw48_32\bin\qmake.exe", Parameters: "-project -platform win32-g++"
"qmake" with Excutable same as "qmake -project", no parameter;
"make" with Excutable "D:\Qt-library\Tools\mingw48_32\bin\mingw32-make.exe", no parameter
Working directory all set to "${PROJECT_DIR}"
And I clicked project->properties, selected "This is a custom Makefile"
Then I clickd the "qmake -project" menu item in Tools menu which I created as above, qmake generated a .pro file.
Because I use
#include <QApplication>
instead of
#include <QtGui/QApplication>
in main.cpp file so I added "QT += widgets" in the .pro file.
Then click "qmake" in Tools menu, makefiles are generated. If I directly make, it won't compile, because it doesn't support some C++11 grammars,
so I edited the Makefile.Debug and makefile.Release, it looks like:
CC = gcc
CXX = g++
DEFINES = -DUNICODE -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN
CFLAGS = -pipe -fno-keep-inline-dllexport -O2 -Wall -Wextra $(DEFINES)
CXXFLAGS = -pipe -fno-keep-inline-dllexport -O2 -frtti -Wall -Wextra -fexceptions -mthreads $(DEFINES)
I added -std=c++0x at the line CXXFLAGS, so it become
CXXFLAGS = -pipe -std=c++0x -fno-keep-inline-dllexport -O2 -frtti -Wall -Wextra -fexceptions -mthreads $(DEFINES)
(it won't work for my version of MinGW if I use -std=c++11)
Then click "make" in the Tools menu, I get two errors:
I don't know what the hell was that but some how I modified my main function from
int main(int argc, char** argv)
to int qMain(int argc, char** argv). Then make again, the two qMain error disappeared but I got this:
And now, I didn't do any thing, make again, that error disappeared !!!
But generated .exe file won't do anything, either double click or run in command line, nothing would happen (by the way, although I included , I didn't write any Qt code)
If I remove
#include <QApplication>
and build the project in normal way(cancel "This is a custom Makefile" and build directly), my program goes well.
Another thing very weird, I added CONFIG += debug and CONFIG -= release in the .pro file, qmake will generate both Makefile.Debug and Makefile.Release(whatever CONFIG or debug and release is uper or lower case), but generated .o files and .exe files are all in release directory, no debug file, why was that ?
Anyone has any ideas ? I'll be very thankful for your suggestions.

The problem is solved(although not perfectly).
The fact is that the program did do something, but somehow no console window appear(before I add Qt header, I wrote the program in C++ and use cout to print messages), thus no output can be seen, even if I run the program in console. I found this by writing a simple Qt GUI program to print the results, it works. But I don't know the reason why console wouldn't appear with QApplication included but no Qt code in the program (whatever I set project->properties->build targets->select build target options->type to GUI or console).
On the other hand, I can run the Qt GUI program by double clicking the .exe file in the debug directory, but not in Code::Blocks, it pops up "cannot locate program entry InterlockedCompareExchange#12 on dynamic link library libstdc++-6.dll". Thus I can't debug the program through CB.

Related

How to compile to SDL2 application to Windows from Linux?

So recently I downloaded the Linux Subsystem on Windows 10, with Ubuntu.
I can compile an SDL2 app to Linux with the g++ command but whenever I try doing it with i686-w64-mingw32-g++ this command, I get an error saying main.cpp:5:9: fatal error: SDL2/SDL.h: No such file or directory.
The command I'm using is i686-w64-mingw32-g++ main.cpp -w -lSDL2 -o main.exe.
https://imgur.com/a/uqcGCoJ
Anyone knows how to fix this? :(
[EDIT]
So now I've tried specifying the directory of the necesary files with this command: g++ main.cpp -I/usr/include/SDL -L/usr/lib/x86_64-linux-gnu -w -Wall -Wextra -std=c++17 -lSDL2 -o main
which worked but when I use it with mingw it doesn't i686-w64-mingw32-g++ main.cpp -I/usr/include/SDL -L/usr/lib/x86_64-linux-gnu -w -Wall -Wextra -std=c++17 -lSDL2 -o main
https://imgur.com/a/sF6CpcP
You need to include the path to SDL's include directory on the command line. However, you need to include the path to the downloaded SDL for mingw32, not /usr/include/SDL2. The difference is the headers in /usr/include/SDL2 are for Linux and libs in /usr/lib are also for Linux, but you need to link to the Windows libraries.
What I usually do is download the development libraries for Mingw32 and put them directly into my project directory. Then all you need to do is add -ISDL2-2.0.8/i686-w64-mingw32/include -LSDL2-2.0.8/i686-w64-mingw32/lib to your command line and it will be able to find the headers and libraries it needs. Finally, make sure you copy SDL2-2.0.8/i686-w64-mingw32/bin/SDL2.dll to your executable directory in the Makefile.
Also, remember to link SDLmain as well. It handles creating a WinMain for you and all that, and then calls your main function.

Qt Creator, Compiler in kit for project is being ignored

I am running macOS High Sierra (10.13.2) and Qt 5.10.0. I would like to use OpenMP with my application.
I have added the following flags to my .pro file
QMAKE_CXXFLAGS += -fopenmp
QMAKE_LFLAGS += -fopenmp
LIBS += -fopenmp
The default compilers on macOS do not contain OpenMP. I installed gcc through homebrew which does support OpenMP.
Under the Build & Run -> Compilers tab of Qt Creator, I added the homebrew g++ and gcc compilers (/usr/local/Cellar/gcc/7.2.0/bin/{gcc-7,g++-7}). I then selected the kit that I am using and changed the compiler to be the homebrew installed compiler that I added under the compilers tab.
If I inspect the Makefile generated by Qt Creator after setting this kit and rebuilding the project, I find the CC and CXX are not using the compiler that I have specified.
Here are their values in the Makefile:
CC = /Library/Developer/CommandLineTools/usr/bin/clang
CXX = /Library/Developer/CommandLineTools/usr/bin/clang++
These should be /usr/local/Cellar/gcc/7.2.0/bin/g++-7 and /usr/local/Cellar/gcc/7.2.0/bin/gcc-7.
The compiler output that I get now is:
18:14:48: Starting: "/usr/bin/make"
/usr/local/Cellar/qt/5.10.0/bin/qmake -o Makefile ../Practice/Practice.pro -spec macx-g++ CONFIG+=debug CONFIG+=x86_64 CONFIG+=qml_debug
/Library/Developer/CommandLineTools/usr/bin/g++ -c -pipe -fopenmp -g -std=gnu++11 -arch x86_64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.13.sdk -mmacosx-version-min=10.10 -Wall -W -fPIC -DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_QUICK_LIB -DQT_GUI_LIB -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -I../Practice -I. -I/usr/local/Cellar/qt/5.10.0/lib/QtQuick.framework/Headers -I/usr/local/Cellar/qt/5.10.0/lib/QtGui.framework/Headers -I/usr/local/Cellar/qt/5.10.0/lib/QtQml.framework/Headers -I/usr/local/Cellar/qt/5.10.0/lib/QtNetwork.framework/Headers -I/usr/local/Cellar/qt/5.10.0/lib/QtCore.framework/Headers -I. -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/OpenGL.framework/Headers -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/AGL.framework/Headers -I/usr/local/Cellar/qt/5.10.0/mkspecs/macx-g++ -F/usr/local/Cellar/qt/5.10.0/lib -o main.o ../Practice/main.cpp
clang: error: unsupported option '-fopenmp'
make: *** [main.o] Error 1
18:14:49: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project Practice (kit: Desktop)
When executing step "Make"
Why is the Makefile generated by Qt Creator not using the compiler that I am specifying in the kit that I am using?
Because the mkspec macx-g++ you're using will override CC and CXX settings. As a temporary workaround, try setting them in the pro file directly:
QMAKE_CC = /usr/local/Cellar/gcc/7.2.0/bin/gcc-7
QMAKE_CXX = /usr/local/Cellar/gcc/7.2.0/bin/g++-7
You may want to edit the mkspec (or making a new one) for your kit and set those variables there.
To find the mkspec file (qmake.conf):
INSTALLDIR=`qmake -query QT_INSTALL_CONFIGURATION`
MKSPECNAME=`qmake -query QMAKE_SPEC`
cd $INSTALLDIR/mkspecs/$MKSPECNAME
As to why you have to set the compilers twice: Qt Creator is meant to be a generic c++ IDE, thus able to manage non-Qt projects, which don't use qmake but need a compiler anyway.

GCC can not recognize Qt libraries or/and include files

I am trying to compile my Qt app from console. Here is specs:
MCBC 3.0 (has preinstalled Qt and gcc), Qt 4.6.4, gcc v4.1.2, qmake v2.01a.
My test qt app contains only 2 files: basket.pro and main.cpp.
basket.pro:
TARGET = basket
TEMPLATE = app
SOURCES += main.cpp
HEADERS +=
main.cpp:
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
return a.exec();
}
I need to compile that app from console.
I got 2 virtual machines. VM #1 has installed QtCreator, and QtCreator compile this app without troubles, also app can be compiled from the console by "qmake" and "make". But there is VM#2, which does not have installed QtCreator, and it is where problems come from.
I tried compile app by commands "qmake make", trying to use Makefile, created by QtCreator, trying to use only gcc comands with explicitly defined paths to Qt libs, include-files and linked libs, but always I got the same errors. For example, I use 2 gcc comands, which used by QtCreator for successful compile this app:
g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/lib/qt46/mkspecs/linux-g++ -I. -I/usr/lib/qt46/include/QtCore -I/usr/lib/qt46/include/QtGui -I/usr/lib/qt46/include -I. -o main.o main.cpp
g++ -o basket main.o -L/usr/lib/qt46/lib -lQtGui -L/usr/lib/qt46/lib -lQtCore -lpthread
And so after executing second command I got same error as always:
[root#ARM basket]# ./build_g++_step2
main.o: In function `basic_string<char, string_char_traits<char>, __default_alloc_template<true, 0> >::data(void) const':
/usr/lib/gcc-lib/i586-linux/2.95.4/../../../../include/g++-3/std/bastring.h:152: undefined reference to `QApplication::QApplication(int &, char **, int)'
/usr/lib/gcc-lib/i586-linux/2.95.4/../../../../include/g++-3/std/bastring.h:152: undefined reference to `QApplication::exec(void)'
/usr/lib/gcc-lib/i586-linux/2.95.4/../../../../include/g++-3/std/bastring.h:152: undefined reference to `QApplication::~QApplication(void)'
/usr/lib/gcc-lib/i586-linux/2.95.4/../../../../include/g++-3/std/bastring.h:152: undefined reference to `QApplication::~QApplication(void)'
collect2: ld returned 1 exit status
Please tell what I missed.
Your GCC compiler is version 2.95 (a much too old version from the previous century, which is not conforming to C++11 standard, and which was known to be quite buggy), since /usr/lib/gcc-lib/i586-linux/2.95.4/is used. Run simply g++ -v or g++ --version to check the version of your compiler. (You could have some PATH issue, or you could have misinstalled your build tools).
You should upgrade your compiler to some much newer version (at least GCC 6 and preferably GCC 7 in july 2017)
BTW, I recommend upgrading also your Qt library. Current version (in july 2017) is Qt5.9 and you should use some Qt5.
Then you need to regenerate your makefile, adding more in QT, as answered by ilbeldus
Notice that you could use pkg-config to help compiling Qt applications (perhaps in your Makefile). You still need to explicitly use moc. For example you might compile some Qt C++ GUI file foo.cc (after having used moc) with e.g.
g++ -Wall -g -c $(pkg-config --cflags Qt5Gui) foo.c
and you would use $(pkg-config --libs Qt5Gui) for link options.
I don't recommend using your old MCBC thing (even with googling I didn't find what that is), notably if it is so obsolete.
You are missing the setup of the QT variable in your .pro file.
Add QT += core gui to it, re-run qmake and try to recompile
See the docs here: http://doc.qt.io/qt-4.8/qmake-variable-reference.html#qt

Running a SFML C++ program with minGW on Windows 10

So I'm trying to run an example SFML program on my Windows laptop. If relevant, the source code is on this page.
So first I make the .o file using this command -
g++ -c a.cpp -ISFML/SFML/include
Where a.cpp is the main file, and my SFML package is located in SFML/SFML.
Then I compile using this command -
g++ a.o -o a -LSFML/SFML/lib -lsfml-graphics -lsfml-window -lsfml-system
When I first ran the program I got the errors about not being able to find certain dlls, sfml-graphics-2 etc. So I found them and put them next to the exe. But now when I run, I get this weird error:
The procedure entry point
_ZNSt7__cxx1112basic_stringSt11char_traitsIcESalcEE7reserveEj could not be located in the dynamic link library.
What is going on here?
As the SFML download page states, You could be using the wrong version of the compiler, other library versions of SFML that you have not removed from your working directory that could mismatch between code and linker. Worst case, if your compiler is not listed there, you have to compile SFML yourself:
Get CMake. Get the source code for 2.4.2 by going to the bottom of the SFML download page. Follow this guide on SFML's GitHub repo. Alternatively, you could use the guide on SFML's page but it is for an older version. It might answer some questions that the first guide misses.
Ones CMake have generated the makefiles, you're on your way to build SFML.
Good luck!
I've had this problem for so long so I just wanted to help someone out who had the same problem. I have a windows 10 FYI and MinGW-w64 8.1.0 (if it doesnt work try a 32 bit mingw instead)
for a debug mode (debug is when your still working on the game and would like command prompt to open whenever you run it)
(make sure your in the right directory first by doing "cd")
g++ -c (file_name).cpp -o (file_name).o -I(path_to)SFML-64-bit/include -g -m64 -Wall &&
g++ (file_name).o -o (game_name).exe -L(path_to)SFML-64-bit/lib -lsfml-graphics -lsfml-window -lsfml-system
The code above when placed in command will compile everything for you if its all in the same directory so make sure you keep an eye out for that
and now for release mode (if you dont want command prompt to show up)
g++ -c (file_name).cpp -o (file_name).o -I(path_to)SFML-64-bit/include -O3 -m64 &&
g++ (file_name).o -o (game_name).exe -L(path_to)SFML-64-bit/lib -lsfml-graphics lsfml-window -lsfml-system -mwindows
Noticed all I added was the -mwindows and the -O3 aswell as removing -g and -Wall which are not necessary since we wont be using command prompt
Make sure to go to SFML/bin and take all the .dlls and put it into the same directory has your .exe sorry xd
Hope this helped.

Moxa PComm.lib with g++ under mingw64: file format not recognized

I am refactoring an old Borland C/C++98 program. I would like to program it under linux platform but to beginning, as I have severals additionnals boards, I clean the program, remove all GUI OWL and make tests under win7 and mingw64 to use modern gcc/g++.
I actually try to link a sample code that use C320 turbo Moxa multiport serial board.
As mentionned in
http://www.mingw.org/wiki/Specify_the_libraries_for_the_linker_to_use
it should link .lib and .dll.
So I tried to link my sample with Moxa PComm.lib for sio_open, sio_read, sio_write… functions as
g++ -m32 -Wall -std=c++14 src/main.cpp src/rs232_c320t.cpp … -L./lib -lPComm
and it returns
./lib/PComm.lib: file not recognized: File format not recognized
Are there any options to allow link windows .lib with gcc/g++ under mingw64?
Thanks
This related "Linking *.lib files with MinGW" question doesn't mention "File format not recognize" error. See also my comments below.
bcag2
I downloaded last PCommLite for win7 x64.
Copy C:\Program Files\Moxa\PCommLite 1.6\Include\PCOMM.H in my include project folder (lib in my case), and do the same for files PCOMM.dll and PCOMM.lib in C:\Program Files\Moxa\PCommLite 1.6\Lib\x64.
Then I compile with:
g++ -c -std=c++14 -D_hypot=hypot -DWIN32 src/*.cpp -I./lib/
Where -I./lib give access to PCOMM.H and link with:
g++ -shared *.o lib/PCOMM.LIB /c/Windows/System32/msvcr120.dll /c/ProgramData/Anaconda3/python36.dll -o _project.pyd
Of course you can do all in one time and create an .exe:
g++ -o project.exe -Wall -std=c++14 -DWIN32 src/*.cpp -Ilib/ lib/PCOMM.LIB -L./lib/
NO -ansi required as suggested by Moxa support!