Why does my Qt 4.5 app open a console window under Windows? - windows

I've been playing around with Qt Creator 4.5 under Linux. My application builds just fine under Linux, but if I build in Windows, the app always opens a console window at startup.
Can I stop it doing that?
I'm building using the default MinGW setup, perhaps that is related. If need be I can build with Visual Studio, but I'd like to understand what is happening first...
Edit: I just created a simple test GUI app with Qt Creator under Windows and it didn't exhibit this behaviour. Either this behaviour has occurred because of the way the project was created under linux, or there is something my app does which causes the console window to appear. Will post details when I diagnose it in case it helps someone else.

For those who have this issue using CMake under Windows (see Amoo's comment), here's a solution here.
In short, you need to add WIN32 to your add_executable() statements:
add_executable(GuiApplication WIN32 src/main.cpp)
For further details, see the CMake documentation on add_executable and WIN32_EXECUTABLE.

The short answer is that including the Qt testlib causes the console to appear. Removing it makes it go away.
To explain further, if your .pro file adds testlib to QT, e.g.
QT += sql \
webkit \
network \
testlib
then the final link step is carried out with a line like this
g++ -enable-stdcall-fixup
-Wl,-enable-auto-import
-Wl,-enable-runtime-pseudo-reloc
-mthreads
-Wl
-Wl,-subsystem,console
-o debug\MyApp.exe object_script.MyApp.Debug
-L"c:\Qt\2009.01\qt\lib"
-lglu32 -lgdi32 -luser32 -lQtWebKitd4 -lQtTestd4
-lQtSqld4 -lQtGuid4 -lQtNetworkd4 -lQtCored
We've wound up using the console subsystem! I presume using testlib forces this to happen so that the test output has somewhere to go.
If we now edit the .pro file and remove the reference to testlib and rebuild, we get a link step like this
g++ -enable-stdcall-fixup
-Wl,-enable-auto-import
-Wl,-enable-runtime-pseudo-reloc
-mthreads
-Wl
-Wl,-subsystem,windows
-o debug\Ammotin.exe object_script.Ammotin.Debug
-L"c:\Qt\2009.01\qt\lib"
-lglu32 -lgdi32 -luser32 -lmingw32 -lqtmaind -lQtWebKitd4
-lQtSqld4 -lQtGuid4 -lQtNetworkd4 -lQtCored4
Yay! subsystem is windows, no more console window.

I think that this is not a solution for this specific answer (besides it is 4 years later),
but I think that many people landing in this thread will be looking for this setting:
Projects > Build and Run > Run Settings > Run > [x] Run in terminal
De-select it and run your GUI from QtCreator without an extra Terminal window. Terminal output will be then embedded in the IDE.

You will want to make sure the -mwindows switch is provided.
Edit:
alternatively, you can go into your makefile and add this to your linker flags:
-Wl,-subsystem,windows

Make sure your .pro file doesn't add console to the variable CONFIG. You can do this by adding
CONFIG -= console
somewhere at the end of your .pro file. If CONFIG contains console a window pops up every time you start the program, and this is also used when printing debug output. Thus, adding console can be useful when debugging a program. Otherwise you'd need to use tools like DebugView to show the output of the qDebug() calls.

I use cmake instead of qmake. I used set(CMAKE_CXX_FLAGS "-mwindows")
seems as though
QMAKE_CXXFLAGS +=-mwindows
in your .pro file would do the same.

Sounds like your linker settings are set for a console app. You could try setting the linker to use the /subsystem:windows option. The option for console applications is /subsystem:console. If you build your project in Visual Studio, create a Win32 Project. Don't create a Win32 Console App. The option under Visual Studio for setting the subsystem is under Project Settings->Linker->System->SubSystem. Select Windows(/subsystem:windows).
I took a look into how to do this with MinGW/gcc. The online docs for gcc shows the different options for Windows targets. Like other have stated, the option you are looking for is -mwindows. The option -mwindows creates a windowed app. It seems like your current setting is -mconsole. The option -mconsole produces a console app. This is a linker option. I'm not familiar with Qt Creator but I'm guessing it has a similar way to view project settings like Visual Studio has.

Go to: Projects --> Run and uncheck Run in terminal checkbox

By default the linker assumes you want a console application.
The fix is to add "-mwindows" to your compiler's argument list, and this will also invisibly add the required libraries (-lkernel32, -lgdi32, etc.). You'll probably need to change from main() to WinMain() as the entry point.
RE: your Edit: the Qt build system uses "spec" files to add variables, you can find them in $QTDIR/qt/mkspecs. When you create a new Windows app in QCreator, it sets the default spec to "win32-g++", which automatically sets the correct variables (windows app, linker flags, etc.).
When you made the project under Linux, it used another spec and that caused your issue. You can see the current spec in the "yourproject.pro.user" file.

Qt/2009.05/qt/mkspecs/win32-g++
edit file qmake.conf
modify line
QMAKE_LFLAGS_CONSOLE = -Wl,-subsyetem,console
to
QMAKE_LFLAGS_CONSOLE = -Wl,-subsyetem,windows
and modify your ???.pro file (example add new line or space)
and compiling
it`s working ok

Related

CodeBlocks not finding wxWidgets

I'm on PopOS and installed wxWidgets via the terminal when I first got the error error: wx/wxprec.h: No such file or directory as said by the user "oBFusCATed". I did not manually compile from source.
wx-config --version outputs 3.0.5
wx-config --cxxflags outputs:
-I/usr/lib/x86_64-linux-gnu/wx/include/gtk3-unicode-3.0 -I/usr/include/wx-3.0 -D_FILE_OFFSET_BITS=64 -DWXUSINGDLL -D__WXGTK__ -pthread
wx-config --libs outputs:
-L/usr/lib/x86_64-linux-gnu -pthread -lwx_gtk3u_xrc-3.0 -lwx_gtk3u_html-3.0 -lwx_gtk3u_qa-3.0 -lwx_gtk3u_adv-3.0 -lwx_gtk3u_core-3.0 -lwx_baseu_xml-3.0 -lwx_baseu_net-3.0 -lwx_baseu-3.0
I created my project via the wizard and made sure I selected the same version (wxWidgets 3.0.x).
In the step that says "Please select your favorite GUI Builder to use.", I chose wxSmith and Frame based options.
In the next step, which prompts me to choose a compiler, I left it as it was (see image below).
In the next step, which prompts me to select the wxWidgets configuration I selected the following options:
Though, I also tried using the Use default wxWidgets configuration and without any Other Options selected.
And last, I clicked Finish.
In Code::Blocks I've added the `wx-config --cxxflags` and `wx-config --libs` (even two additional ones but with the full path which the commands above return in the terminal), as you can see in the image below.
In the Other compiler options, I have these two lines (which I did not add myself - must've been the wizard):
-Winvalid-pch
-include wx_pch.h
I even added the output of wx-config --cxxflags to Other resource compiler options.
I also added a Global Variable wx with this base path /usr/lib/x86_64-linux-gnu/wx (copy/pasted), as mentioned in step #7 here. If I put the name of the variable exactly as $(#wx), it appears as set___wx__ instead of just wx, as seen in some other instructions I've found here and on other forums/guides.
Interestingly enough, if I click the button on the right to navigate to it, there is no wx folder. Like, it's nowhere to be found. If I were to use my normal file manager, I can navigate to it (terminal works as well). Could this have something to do with it?
In the Linker Settings tab, I've added these two libraries:
At the end of it all, I still get the error mentioned at the beginning. If I were to comment out that line, I get the same error but for wx/app.h and wx/frame.h, which made me think that it's just not finding the wxWidgets, but I can't figure out why.
This is the first time I'm using C++/Code::Blocks/wxWidgets, so if you give me any instructions where I need to put stuff, please, try to be specific.
P.S. Many of the guides on how to set this up seem to be for Windows and the only one I found for Ubuntu (since PopOS is based on it) is this one. Where he even gets the same error, but his solution did not work (add the output of wx-config --cxxflags to Other resource compiler options - which I've already tried - though, he does build from source).
P.S.2. I found this setup build which uses the binary download option, but it's for Windows. Would something mess up if I were to attempt it?
P.S.3. I know a lot of people have similar issues, but the stuff I've already tried seem to have solved their problem and since for me it didn't, I figured I'd ask.
Son of a motherless goat >_<
The moment I ask, an idea popped into my head from another issue I had recently.
I had installed Code::Blocks via the PopShop (i.e. flatpak) and that flatpak had no access to the wx folder, which is why it wouldn't show up when trying to navigate to it via Code::Blocks.
I uninstalled it and installed it again via sudo apt install codeblocks. I created a new project via the wizard (it wouldn't let me select wxSmith because it doesn't seem to come with it - wxSmith plugin is not loaded can not continue) with the same wizard settings mentioned in my original post.
Then I only added the wx-config --cxxflags and wx-config --libs compiler flags and it built!
Darn it, Flatpak...
Anyway, for people that are interested in adding wxSmith plugin, the person in this video shows how.
Run sudo apt install codeblocks-contrib in your terminal and if you had Code::Blocks open, make sure to restart it - and perhaps give the dude a like if the video is still up! :3

Building in CLion

Is it possible to build a single file in CLion and see the actual compiler command line being used?
I have a large existing project, which CLion managed to load however it fails to build, I suspect because of various CmakLists.txt setting. However as far as I can tell you can only build an entire project and it gets built using -j 8 so you get a lot of output which does not include the command line being used so it's hard to know what to fix. i know CLion does create a temporary build directory in .clion10 but I was hoping not to have to go a fish around in that.
Also as a newbie to CLion I can't seem to work out how to undock Clion Windows ( on a Windows 7 box ). The "Floating" option doesn't seem to let me drag a Window outside of the main Clion window.
You have to use add_executable command multiple times.
Here an example :
cmake_minimum_required(VERSION 3.3)
project(test_build)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(BUILD_1 main)
set(SOURCE_FILES_1 main.cc)
add_executable(${BUILD_1} ${SOURCE_FILES_1})
set(BUILD_2 main_2)
set(SOURCE_FILES_2 main_2.cc)
add_executable(${BUILD_2} ${SOURCE_FILES_2})
Old anwser :
You can use add_subdirectory(path_to_directory) to use an other CMakeLists and have multiples building process. ie: you have your cmakelists at the root of your project and multiple other in some files of your project. You just use add_subdirectory and you get other build options.
For your other question, on ubuntu i have to split the view and after i can drag in another floating window.
Under you can see one of my projects and the main CMakeLists.txt on the right
And the build options on CLion with this structure.
The CMakeLists.txt under the files are like full project builders.

Can't find file executable in your configured search path for gnc gcc compiler

My problem is that code::blocks error message tells me that it can't find file executable in the search path for gnc gcc compiler. Although, I don't know what that means. Also I typed out some code:
#include <iostream>
using namespace std;
int main(void) {
cout <<"Hello World" <<endl;
return 0;
}
I can't build it or run in code::blocks. What do I need to do?
I went on line but I got some answers that are way over my head. I was able to use code::blocks once before I installed Visual studios express 2013. Visual studios didn't work right either. It kept asking me to repair or uninstall every time I tried to open it. So I deleted it along with code::blocks. Now that I re-installed code::blocks I still can't get to work right.
This problem with compilers is taking up all my time and I can't practice learning programming because I can't get any compiler to work right. I need some help, please.
I'm guessing you've installed Code::Blocks but not installed or set up GCC yet. I'm assuming you're on Windows, based on your comments about Visual Studio; if you're on a different platform, the steps for setting up GCC should be similar but not identical.
First you'll need to download GCC. There are lots and lots of different builds; personally, I use the 64-bit build of TDM-GCC. The setup for this might be a bit more complex than you'd care for, so you can go for the 32-bit version or just grab a preconfigured Code::Blocks/TDM-GCC setup here.
Once your setup is done, go ahead and launch Code::Blocks. You don't need to create a project or write any code yet; we're just here to set stuff up or double-check your setup, depending on how you opted to install GCC.
Go into the Settings menu, then select Global compiler settings in the sidebar, and select the Toolchain executables tab. Make sure the Compiler's installation directory textbox matches the folder you installed GCC into. For me, this is C:\TDM-GCC-64. Your path will vary, and this is completely fine; just make sure the path in the textbox is the same as the path you installed to. Pay careful attention to the warning note Code::Blocks shows: this folder must have a bin subfolder which will contain all the relevant GCC executables. If you look into the folder the textbox shows and there isn't a bin subfolder there, you probably have the wrong installation folder specified.
Now, in that same Toolchain executables screen, go through the individual Program Files boxes one by one and verify that the filenames shown in each are correct. You'll want some variation of the following:
C compiler: gcc.exe (mine shows x86_64-w64-mingw32-gcc.exe)
C++ compiler: g++.exe (mine shows x86_64-w64-mingw32-g++.exe)
Linker for dynamic libs: g++.exe (mine shows x86_64-w64-mingw32-g++.exe)
Linker for static libs: gcc-ar.exe (mine shows x86_64-w64-mingw32-gcc-ar.exe)
Debugger: GDB/CDB debugger: Default
Resource compiler: windres.exe (mine shows windres.exe)
Make program: make.exe (mine shows mingw32-make.exe)
Again, note that all of these files are in the bin subfolder of the folder shown in the Compiler installation folder box - if you can't find these files, you probably have the wrong folder specified. It's okay if the filenames aren't a perfect match, though; different GCC builds might have differently prefixed filenames, as you can see from my setup.
Once you're done with all that, go ahead and click OK. You can restart Code::Blocks if you'd like, just to confirm the changes will stick even if there's a crash (I've had occasional glitches where Code::Blocks will crash and forget any settings changed since the last launch).
Now, you should be all set. Go ahead and try your little section of code again. You'll want int main(void) to be int main(), but everything else looks good. Try building and running it and see what happens. It should run successfully.
Just open your setting->compiler and click on the reset defaults and it will start work.
* How to Download and install CodeBlocks.* ( I have already downloaded )
***How to solve the CodeBlocks environment error.
Go to "Settings"----"Compiler"----"Selected compiler"( GNU GCC Compiler ).
Then, Selected "Toolchain executables".
Now, "( C:\Program Files (x86)\CodeBlocks\MinGW )"
See Video : https://youtu.be/Tb1VnXs60Lg
I had also found this error but I have solved this problem by easy steps. If you want to solve this problem follow these steps:
Step 1: First start code block
Step 2: Go to menu bar and click on the Setting menu
Step 3: After that click on the Compiler option
Step 4: Now, a pop up window will be opened. In this window, select "GNU GCC COMPILER"
Step 5: Now go to the toolchain executables tab and select the compiler installation directory like (C:\Program Files (x86)\CodeBlocks\MinGW\bin)
Step 6: Click on the Ok.
Now you can remove this error by follow these steps. Sometimes you don't need to select bin folder. You need to select only (C:\Program Files (x86)\CodeBlocks\MinGW) this path but some system doesn't work this path. That's why you have to select path from C:/ to bin folder.
Thank you.
For that you need to install binary of GNU GCC compiler, which comes with MinGW package. You can download MinGW( and put it under C:/ ) and later you have to download gnu -c, c++ related Binaries, so select required package and install them(in the MinGW ). Then in the Code::Blocks, go to Setting, Compiler, ToolChain Executable. In that you will find Path, there set C:/MinGW.
Then mentioned error will be vanished.
Uninstall/Remove your current codeblocks compiler.
Install codeblocks using this link that contains GCC compiler files: http://sourceforge.net/projects/codeblocks/files/Binaries/13.12/Windows/codeblocks-13.12mingw-setup-TDM-GCC-481.exe.
Now go to : Settings > Compiler.... > ToolChain Executables Tab
CLICK on Auto-detect button and then click OK button. Now just restart CodeBlocks and start writing your codes and use the Build and run option. It will RUN normally.
Fistly, Code Blocks is not a compiler. It is just an integrated development environment.
So, you must show the path of your compiler at first, (if you dont have a compiler you have to download an install, it is not difficult to find. f.e. GCC is good one.)
If code blocks could not find automatically the path of compiler it is an obligation to show it yourself.
But when you install, probably Code Blocks automatically find your compiler.
Enjoy.
This simple in below solution worked for me.
http://forums.codeblocks.org/index.php?topic=17336.0
I had a similar problem. Please note I'm a total n00b in C++ and IDE's but heres what I did (after some research)
So of course I downloaded the version that came with the compiler and it didn't work. Heres what I did:
1) go to settings in the upper part
2) click compiler
3) choose reset to defaults.
Hopefully this works
I'm a total noob but I reinstalled over the codeblocks giving me these "Can't find file executable in your configured search path for gnc gcc compiler" errors by downloading:
codeblocks-20.03mingw-setup.exe
(IMPORTANT: make sure it has the "mingw" in the file download name, that has the compiler build that is required to compile the code which doesn't automatically comes with the main codeblocks editor software download because codeblocks already assumes you already have another compiler installed on your computer {visual studio 2019 or such}).
Then when I created a new project (console application) and used the defaults to quickly test it out.
It gave me errors.
So I went to Settings > Compiler > Selected Compiler set to: GNU GCC Compiler > Click on the "Tooolchain executables" tab > Click on Auto-Detect > Should say "C:\Progam Files\CodeBlocks\MinGW" > Click OK.
Build and run a simple hello world code.
Should work! If not, look for the "MingGW" in the C:\Program Files\CodeBlocks and select it.
Here's an easy way for Windows users.
Uninstall the existing codeblocks from your system.
Restart system.
Go to http://www.codeblocks.org/downloads/26
Download the codeblocks-16.01mingw-setup.exe file. It includes the GCC/G++ compiler and GDB debugger from TDM-GCC (version 4.9.2, 32 bit, SJLJ).

Code::blocks verbose build

I want to see the actual commands sent to g++ during a Code::Blocks build. I want to see exactly what command-line arguments it uses in the compile and link steps, and I don't want to have to poke around in the build settings GUI to do it.
Alternatively, converting the Code::Blocks project to an equivalent Makefile would work, but I see nowhere where I can do that, either...
Edit
I ended up using a Code::Blocks plugin, "cbMakeGen", to generate a makefile from which I removed some #s. Then I was able to see the commands. Surely there is an easier way...
I see you already solved the problem, but there's still a bit more to that.
Code::Blocks can write a build log when the following option is checked:
Settings->Compiler and debugger->Global compiler settings->{slide tabs to the right}->Build options tab->Save build log to HTML.
Besides, you can use "cbp2make" to convert Code::Blocks projects to makefiles. This is not a plugin like "cbMakeGen", but a stand-alone command-line tool. See also http://forums.codeblocks.org/index.php/topic,13675.0.html .
Besides the logging to html you can also go to
Settings->Compiler and debugger->Global compiler settings->{slide tabs to the right}->Other Settings and in that tab set 'Compiler Logging:' to 'full command line' (from drop down menu).
Now you can see the gcc command line in the console when you build.

setting up qt for xcode debugging

I just installed QT 4.6 on snow leopard 10.6.3. I wrote a really simple program. I can generate a xcode project using qmake, but I can't step into QT function. How can I set it up?
By default, qt is built with a debug and a non-debug library. This is my understanding. For example,
% ls /Library/Frameworks/QtCore.framework/
Contents/ Headers# QtCore# QtCore.prl QtCore_debug# QtCore_debug.dSYM/ QtCore_debug.prl Versions/
Also, my default from source build of Qt 4.7 branch also has the *_debug libs.
Setting up for Xcode is cake, you just set up your project and
% qmake -spec macx-xcode
This -spec is the default for the official mac distribution, but if you build your own from source the default is macx-g++ which creates a Makefile project.
This generates a MyProject.xcodeproj that comes preconfigured to link all the necessary Qt frameworks, sets up paths, and has a Release and Debug build target set to the same options as the official SDK's.
This is all assuming you have your qt project file set up, if you need to generate that first from a raw source directory:
% qmake -project
Debugging works "out of the box" for these generated *.xcodeproj files. However, there's one little "hitch". Since Qt is full of custom data types, Xcode doesn't know how to display their "values" in the debugger's summary pane. So you can't see what value a QString has, for example.
There's a method of entering custom macros for display, but I've found these often (always?) don't work for QObjects.
To get that working, I've started a project that uses xcode's debugger c callbacks (also mentioned in the above linked article, though their example doesn't even work o.O). I call it Qt4DataFormatters.
I've just started it and have been adding types as the need arises. It's dirt simple to create one using the existing functions as a template though.
I haven't tried this on Mac, but on Linux you need to take the following process:
First, you need to setup Qt so that it has debugging symbols available to you:
./configure -debug-and-release separate-debug-info # other options
With the debugging symbols available, you should now be able to get valid stack traces.
When building your application with qmake, you need to have the debug (or debug_and_release) flag set in your project file:
CONFIG += debug
Once you've done that, you should only need to tell the debugger where the Qt source is located:
(gdb) dir /path/to/qt/src
After that, list should show you the actual Qt source code. You may need to add additional directories under the src directory for the debugger to pick it all up.

Resources