G++ makefile command with QProcess - c++

I'm trying to generate a .exe file with g++ compiler. I tried multiple ways without sucess.
1) QString program = "C:/Strawberry/c/bin/g++";
QStringList arguments;
arguments << "g++ -o dialog C:/Documents/ED30/dialog.cpp";
QProcess process;
process.start(program, arguments);
process.waitForFinished(-1);
2) QProcess::execute("g++ -o dialog2 C:/Documents/ED30/dialog.cpp");
3) QProcess::execute("g++ C:/Documents/ED30/dialog.cpp -o dialog2");
Everytime I have the same error message :
"In file included from C:/Documents/ED30/dialog.cpp:1:0:
C:/Documents/ED30/dialog.h:4:19: fatal error: QDialog: No such file or directory
compilation terminated."
Or sometimes nothing happens.
I'm using Qt 5.7 with MinGW on windows 8.1
EDIT:
So after discussing the issue with members, I did some progress.
Best code until now is :
system("g++ -o dialog2 -I C:/Qt/5.8/mingw53_32/include -I C:/Qt/5.8/mingw53_32/include/QtGui -I C:/Qt/5.8/mingw53_32/include/QtCore -I C:/Qt/5.8/mingw53_32/include/QtWidgets -L C:/Qt/5.8/mingw53_32/lib C:/Documents/ED30/dialog.cpp");
But this indicats me that MinGW isn't a c++ 11 compiler and it suggests me to write "-std=c++11 or -std=gnu++11" in the command-line in order to update the Compiler. After typing it in the compiler, the following error appears: "Unknow command".
I tried with Qt 5.7 and 5.8 without success.
A solution guys ?

It seems the code you are trying to compile dialog.cpp makes use of Qt (QDialog in particular).
When you run g++ -o dialog2 C:/Documents/ED30/dialog.cpp, g++ fails because it cannot find where Qt header and library files are.
The minimal command line to make it work would be:
g++ -o dialog2 -I<PathToQtheaders> -L<PathToQtLibraries> -lQt5Core -lQt5Gui -lQt5Widgets C:/Documents/ED30/dialog.cpp
You might need to add some extra libraries depending on what you code needs. Note that you cannot use Q_OBJECT macro if you just compile it like that (without calling moc etc.).
Tip: Look at the commands generated by qmake on a standard Qt project.
Extra tip: You can use QLibraryInfo::location() to get Qt installation paths.
Also your first example should be:
QString program = "C:/Strawberry/c/bin/g++";
QStringList arguments;
arguments << "-o" << "dialog" << "C:/Documents/ED30/dialog.cpp";
QProcess process = "g++";
process.start(program, arguments);
process.waitForFinished(-1);

I think you are missing the include directory of your Qt headers.
QProcess::execute("g++ -o dialog -I C:\QtDir C:/Documents/ED30/dialog.cpp");
QtDir is the actual location of the Qt Headers on your system like C:\Qt\include
You may also have trouble linking. The whole command is:
QProcess::execute("g++ -o dialog -I C:\QtDir\include -L C:\Qtdir\lib -l Qt C:/Documents/ED30/dialog.cpp")

Related

How Do I Get WxWidgets setup.h after I install all the .debs on Debian?

Problem first, then details:
I copied a hello-world program from the wxwidgets tutorials and tried to compile it from the command line like this:
g++ -o h wxhello.cpp -I/usr/include/wx-3.0
The compile terminated quickly because it could not find "wx/setup.h". I researched this apparently EXTREMELY COMMON PROBLEM and learned that there is supposed to be a second include path, pointing to the place where the individual setup.h that suits my situation can be found. So I tried:
find /usr/include/wx-3.0 -name "setup.h"
And the output was nothing.
So I installed wxWidgets by marking libwxgtk3.0-dev in Synaptic and allowing all the dependencies to be installed (something like 40 packages in all because I just set this thing up).
How do I get my program to compile?
You need to have wx-config --cxxflags --libs in your command line enclosed in back-ticks like this
g++ -o h wxhello.cpp `wx-config --cxxflags --libs`
This is not the solution on Windows, but it should work on any Linux.
This is not documented in a searchable fashion as far as I can tell.

Using Magick++ in Qt Creator

I am creating a Qt widget with a backend representation I wrote separately. The backend uses Magick++, and I can get it to compile from the command line:
g++ -c ../SpriteCreator/WriteGIF.cpp sprite.cpp -I ../SpriteCreator/ Magick++-config --cxxflags --cppflags Magick++-config --ldflags --libs -O2
but when I try to compile the project Qt Creator it tells me
/home/tpope/obeyYourThirst/qtSpriteEditor/backend/sprite.cpp:15: error: Magick++.h: No such file or directory
#include < Magick++.h>
I added the path for Magick++.h to the INCLUDEPATH, but now it has an error similar to this:
/home/tpope/obeyYourThirst/qtSpriteEditor/backend/sprite.cpp:66: error: undefined reference to `Magick::InitializeMagick(char const*)'
for every use of a Magick function. It seems to be not including the library. How do I do that in Qt Creator?
Since the .pro files in Qt Creator is (as far as I can tell) used to generate a Makefile, we can use make's ability to run a program on the shell and save its output.
I have added:
QMAKE_CXXFLAGS += $(shell Magick++-config --cppflags --cxxflags)
and
LIBS += $(shell Magick++-config --ldflags --libs)
to my .pro file, and I was able to add:
#include <Magick++.h>
to my program without a compile error, then compile a simple example (Just put out an animated .gif with a couple of colored pixels)
This obviously makes things like cross-compiling a little tricky (I don't know how to reference foreign libraries), but that's a problem for another time.

qmake: compile single cpp file into an executable

I got a project in QT and I'd like to use qmake for compiling one additional cpp file that (into standalone executable) is not connected in any way to my application or even QT (it's very simple plain C++ program). Is there any way to do this without rebulding whole project structure? Do I need separate .pro file for every executable or is there any other way for simple compiling just one, plain C++ file?
As you may know qmake -project will make one .pro file with the name of the folder containing your whole source and header files, if you qmake this pro file then make your project you will get compiled .o file from your new cpp file even if it's not connect to your Qt project directly.
but if this file got main() function of course you will have multiple main() definitions error by compiler.
you will need to rebuild that file of course
as you know for simple compiling of only one standard plain c++ file you just
g++ source.cpp -o excutable_name.exe
for more strict compiling with two steps:
g++ -Wall -pedantic -ansi source.cpp -c compiled_file_name.o
g++ compiled_file_name -o excutable_name.exe
but if you are going to use for example a code related to Qt, you have to include Qt headers and link necessary libraries :
g++ -Wall source.cpp -c compiled_file_name.o -L qt/library/path -lQtGui -lQtCore -lQtother_necessary_libraries -I Qt/include/path -I Qtother_necessary_include_paths
To make an additional executable you can use use system() in .pro like so:
system(g++ otherapp.cpp)
Which will be built every time you call qmake. However if you want to build the additional app automatically when its source is changed, use QMAKE_EXTRA_TARGETS instead.

MinGW .exe requires a few gcc dll's regardless of the code?

When compiling with MinGW, I have to copy over certain dll files from the MinGW bin directory before the exe will run (Even when using "-static" and/or "-static-libstdc++".)
How do I change that? Is there a special build of MinGW that I have to use? Ultimately I want to be able to run the program with nothing but the exe in the directory (and no windows environment variables set.) These File's are:
libstdc++-6.dll
libgcc_s_seh-1.dll
libwinpthread-1.dll
And here is the complete list of step's I fallow:
Open Up Code::Blocks
Select "File->New->Project->Console"
Fill out the project settings for project "Hello World"
Right click Project->Build Options...->Hello World (Root target)->Other Options
Enter "-static" (or "-static-libstdc++") under the already set "-fexceptions"
CTRL-F9 : Build Project (Without executing)
Navigate to, in Windows Explorer, and run the built "Hello World.exe" file.
Click "OK" when a message pop's up saying "Error: libstdc++-6.dll is missing from your computer."
Copy "libstdc++-6.dll" from the /MinGW/bin/ directory, into the "Hello World.exe" directory.
Run "Hello World.exe"
Click "OK" for the message saying "Error: libgcc_s_seh-1.dll is missing from your computer."
Copy "libgcc_s_seh-1.dll" into the "Hello World.exe" directory.
Repeat and end up copying "libwinpthread-1.dll" over aswell.
View the message
Hello World!
Edit:
My command line is:
g++.exe -Wall -fexceptions -static -static-libgcc -static-libstdc++ -g -static-libgcc -static-libstdc++ -L. -c "C:\Users\______\Desktop\Hello World\main.cpp" -o obj\Debug\main.o
g++.exe -o "bin\Debug\Hello World.exe" obj\Debug\main.o
With all the dll files mentioned above required. And, just to be safe, the code is:
// main.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
Your commands are wrong !
Go to the directory where your main.cpp file is, and try the following.
g++.exe -Wall -c -g main.cpp -o obj\Debug\main.o
g++.exe -static -static-libgcc -static-libstdc++ -o "bin\Debug\Hello World.exe" obj\Debug\main.o
then you'll no longer need to copy the DLLs (for your Hello World program).
Other notes:
The MinGW installation instructions recommends setting
c:\minGW;c:\MinGW\bin;
to the PATH environment variable.
Normally the
-static -static-libgcc -static-libstdc++
linker options should work (try all 3 of them at once). But not for libwinpthread-1.dll.
Also, try to clean before recompiling.
There's no "-static-something" command.
Only standard libraries libgcc and libstdc++ can be set to static linking.
For other libraries, you first switch to static linking with "-static" and then list the libraries to include with separate commands, i.e. "-lpthread".
Cmake users should try adding:
set(CMAKE_CXX_STANDARD_LIBRARIES "-static-libgcc -static-libstdc++ -lwsock32 -lws2_32 ${CMAKE_CXX_STANDARD_LIBRARIES}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive")
-static-libgcc may be a bad idea if exceptions are used. link options documentation states that
There are several situations in which an application should use the
shared libgcc instead of the static version. The most common of these
is when the application wishes to throw and catch exceptions across
different shared libraries. In that case, each of the libraries as
well as the application itself should use the shared libgcc.
The comments to the answer above contain the full solution, so I would like to merely add the CodeBlocks perspective. I verified it on Windows7 x64 with CodeBlocks16 and MinGW-W64 8.1.0 ''i686-posix-dwarf''.
This solves the OPs question
Create new project and name it "Hello World"
accept all defaults in the wizard
select Project/BuildOptions/ and select "Hello World". Out edits will be valid for both Debug and Release
add the following at "Other linker option" in the "Linker" Tab
-static
-static-libgcc
-static-strc++
-lwinpthread
On the Toolbar select "Debug" and press Build (the yellow gear icon)
Press the green run icon and confirm that the build was ok
Testing
open a terminal window and go to the HelloWorld\bin\debug folder
start hello world there.
Confirm it works without asking for any DLLs.
You could also start it from an explorer window and confirm that it also does not ask for DLLs.
Note: On my Win7x64 system when starting the HelloWorld.exe from the explorer adding the "-lwinpthread" line causes CodeBlocks to ignore the setting in "Projects/Properties/Tab_BuildTargets/ "Pause when execution ends". So the "Hello World" output is hardly visible because the window immediately closes after execution (mabye someone knows why)
Note that if you do not have the winpthread.dll not found problem of the OP then you likely do not use a MinGW-W64 compiler with a 'posix' thread model. Both Code blocks MinGW-W64-bundled install packages use such versions. For CB20.03 the relevant downloads from MinGW-W64 download page would be
32bit: 8.1.0 ''i686-posix-dwarf''
64bit: 8.1.0 ''x86_64-posix-seh''
For example if I set setup compilers with Codeblocks direcly and chose the 32-bit compiler package ''i686-win32-dwarf'', only the first 2 DLLs would go missing. In that case the fix is to set the linker options only to
-static-libgcc
-static-strc++

QT5 attaching project name with every sourcefile name, compiling error

i want to get started with QT. I donwloaded QT5 MINGW compiler with QT creator and i am trying to build the pre attached example named affine the problem is that the QT5 i think embed the project name with each of source file and thus gives error that file not found. some thing similar
:-1: error: ..affinemain.cpp: No such file or directory
while the file name is just
main.cpp
i don't know how to fix it. I searched lot on internet but could not found anything useful.
I even try to compile from command prompt but i am not fimmiliar with command prompt compiling as i am new to QT and previously i am totally developed with IDE in visual studio and eclipse for java so i have no idea about the make file and compiler command line arguments.
could some body please help me to fix this issue and can you tell please why compiler attaching project name with the source file name?
Thanks in advance
I have got the same problem and my solution may help you.
I am working with Qt5.0.1 now, and there are two distributions to work on windows with it: Qt5.0.1-mingw and Qt5.0.1-msvc2010.
I had to use mingw and there was a problem on my setup that "/" is ignored in path's.
So according to Qt Creator, compiler was called to process file mainwindow.cpp and this file was passed to it
g++ /*truncated*/ ..\qt-example\mainwindow.cpp
Below is the full compiler input:
g++ -c -pipe -fno-keep-inline-dllexport -g -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_OPENGL_ES_2 -DQT_OPENGL_ES_2_ANGLE -DQT_NEEDS_QMAIN -I..\qt-example -I"..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include" -I"..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include\QtWidgets" -I"..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include\QtGui" -I"..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include\QtCore" -I"debug" -I"." -I"." -I"..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\mkspecs\win32-g++" -o debug\mainwindow.o ..\qt-example\mainwindow.cpp
And the error produced.
g++.exe: error: ..qt-examplemainwindow.cpp: No such file or directory
g++.exe: fatal error: no input files
compilation terminated.
So, we can see that "\" is ignored by the compiler and file name is merged with directory name.
The solution to that problem goes to the tools that are used - MinGW (Minimalist ports of GCC and Binutils). And also MSYS - a collection of GNU utilities such as bash, make, gawk and grep to allow building of applications and programs which depend on traditionally UNIX tools to be present. In our case - g++.
MSYS is not shipped with Qt5.0.1-mingw and g++ is not using it, but having MSYS available in your PATH environment variable breaks the system.
MSYS is used for git scm, which I have installed, so my path contains links to MSYS that goes bundled with git. So I have next paths in my PATH environment variable.
C:\Program Files (x86)\git\bin;C:\Program Files (x86)\git\cmd
I have not found how MSYS is used by Qt Creator or g++, or where it is linked, but when I have dropped next path from PATH:
C:\Program Files (x86)\git\bin;
and restarted Qt Creator - g++ succeeded on compiling my file, it worked.
The question why/how it influences the Qt Creator/g++ that should not use MSYS utils installed with git is still open.
i canĀ“t comment.
important : delete all the files in the release and debug folder (compiled version) before try the tips of the autor ...