Simley
Simley

Reputation: 23

Compiling C++ project in Qt Creator vs. command line - different runtime of resulting .exe

I started this year to use Qt Creator (Windows 10, Qt Creator 4.12.1, based on Qt 5.14.2) for writing my C++ code. Running the program in Qt is no problem (compiling with some minor warnings, runtime is okay). However, when I compile the program outside of Qt (in the command line) the resulting executable is slightly different: It is a little bit bigger (142 instead of 130kb) and the runtime is much slower (about 8 times slower).

I didn't change anything of the compiler settings of Qt Creator generated when starting a Qt console application.

Do I miss anything? Is Qt Creator internally optimizing the program?

I also noticed that I am not able to run the executable compiled with Qt Creator externally in the command line (the program starts, but it crashes when I read in an external txt-file).

Here is an example of the g++ command produced by Qt, which I also used within the command line:

g++ -c -fno-keep-inline-dllexport -O2 -std=gnu++11 -Wall -W -Wextra -fexceptions -mthreads -DUNICODE -D_UNICODE -DWIN32 -DMINGW_HAS_SECURE_API=1 -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_CORE_LIB -I..\Test2 -I. -IC:\Qt\5.13.0\mingw73_32\include -IC:\Qt\5.13.0\mingw73_32\include\QtCore -Irelease -IC:\Qt\5.13.0\mingw73_32\mkspecs\win32-g++  -o release\runtimeenvironment.o ..\Test2\runtimeenvironment.cpp
g++ -Wl,-s -Wl,-subsystem,console -mthreads -o release\Test2.exe release/cell.o release/ft_pop.o release/ft_traits.o release/gridenvironment.o release/lcg.o release/main.o release/output.o release/runparameter.o release/runtimeenvironment.o  C:\Qt\5.13.0\mingw73_32\lib\libQt5Core.a   

When I compile the program in the command line, I tried the qmake/make command and also the raw g++ commands. But even when I run qmake and make with the .pro file generated by Qt, it is not generating the same executable as within Qt Creator.

If it is of interest, this is the project file generated by Qt Creator:

QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        cell.cpp \
        [...]

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

HEADERS += \
    cell.h \
    [...]

And this is the generated Makefile:

#############################################################################
# Makefile for building: Test2
# Generated by qmake (3.1) (Qt 5.13.0)
# Project:  Test2.pro
# Template: app
# Command: C:\Qt\5.13.0\mingw73_32\bin\qmake.exe -o Makefile Test2.pro
#############################################################################

MAKEFILE      = Makefile

EQ            = =

first: release
install: release-install
uninstall: release-uninstall
QMAKE         = C:\Qt\5.13.0\mingw73_32\bin\qmake.exe
DEL_FILE      = del
CHK_DIR_EXISTS= if not exist
MKDIR         = mkdir
COPY          = copy /y
COPY_FILE     = copy /y
COPY_DIR      = xcopy /s /q /y /i
INSTALL_FILE  = copy /y
INSTALL_PROGRAM = copy /y
INSTALL_DIR   = xcopy /s /q /y /i
QINSTALL      = C:\Qt\5.13.0\mingw73_32\bin\qmake.exe -install qinstall
QINSTALL_PROGRAM = C:\Qt\5.13.0\mingw73_32\bin\qmake.exe -install qinstall -exe
DEL_FILE      = del
SYMLINK       = $(QMAKE) -install ln -f -s
DEL_DIR       = rmdir
MOVE          = move
SUBTARGETS    =  \
        release \
        debug


release: FORCE
    $(MAKE) -f $(MAKEFILE).Release
release-make_first: FORCE
    $(MAKE) -f $(MAKEFILE).Release 
release-all: FORCE
    $(MAKE) -f $(MAKEFILE).Release all
release-clean: FORCE
    $(MAKE) -f $(MAKEFILE).Release clean
release-distclean: FORCE
    $(MAKE) -f $(MAKEFILE).Release distclean
release-install: FORCE
    $(MAKE) -f $(MAKEFILE).Release install
release-uninstall: FORCE
    $(MAKE) -f $(MAKEFILE).Release uninstall
debug: FORCE
    $(MAKE) -f $(MAKEFILE).Debug
debug-make_first: FORCE
    $(MAKE) -f $(MAKEFILE).Debug 
debug-all: FORCE
    $(MAKE) -f $(MAKEFILE).Debug all
debug-clean: FORCE
    $(MAKE) -f $(MAKEFILE).Debug clean
debug-distclean: FORCE
    $(MAKE) -f $(MAKEFILE).Debug distclean
debug-install: FORCE
    $(MAKE) -f $(MAKEFILE).Debug install
debug-uninstall: FORCE
    $(MAKE) -f $(MAKEFILE).Debug uninstall

Makefile: Test2.pro C:/Qt/5.13.0/mingw73_32/mkspecs/win32-g++/qmake.conf C:/Qt/5.13.0/mingw73_32/mkspecs/features/spec_pre.prf \
        C:/Qt/5.13.0/mingw73_32/mkspecs/qdevice.pri \
        [and several more....]
        Test2.pro \
        C:/Qt/5.13.0/mingw73_32/lib/Qt5Core.prl
    $(QMAKE) -o Makefile Test2.pro
C:/Qt/5.13.0/mingw73_32/mkspecs/features/spec_pre.prf:
C:/Qt/5.13.0/mingw73_32/mkspecs/qdevice.pri:
C:/Qt/5.13.0/mingw73_32/mkspecs/features/device_config.prf:
C:/Qt/5.13.0/mingw73_32/mkspecs/common/sanitize.conf:
C:/Qt/5.13.0/mingw73_32/mkspecs/common/gcc-base.conf:
[and several more...]
Test2.pro:
C:/Qt/5.13.0/mingw73_32/lib/Qt5Core.prl:
qmake: FORCE
    @$(QMAKE) -o Makefile Test2.pro

qmake_all: FORCE

make_first: release-make_first debug-make_first  FORCE
all: release-all debug-all  FORCE
clean: release-clean debug-clean  FORCE
distclean: release-distclean debug-distclean  FORCE
    -$(DEL_FILE) Makefile
    -$(DEL_FILE) .qmake.stash

release-mocclean:
    $(MAKE) -f $(MAKEFILE).Release mocclean
debug-mocclean:
    $(MAKE) -f $(MAKEFILE).Debug mocclean
mocclean: release-mocclean debug-mocclean

release-mocables:
    $(MAKE) -f $(MAKEFILE).Release mocables
debug-mocables:
    $(MAKE) -f $(MAKEFILE).Debug mocables
mocables: release-mocables debug-mocables

check: first

benchmark: first
FORCE:

$(MAKEFILE).Release: Makefile
$(MAKEFILE).Debug: Makefile

It would be great if anyone could help me.

Upvotes: 2

Views: 1742

Answers (1)

Swift - Friday Pie
Swift - Friday Pie

Reputation: 14589

is it possible that you use Visual Studio version of Creator? that would make executable depend on Qt .dll's that use side-by-side runtime.

You can't compile project from scratch by running g++ alone, you have to run qmake, then make. Both outside creator and inside you HAVE to use qmake on the .pro file. Compiling program includes more steps, running moc and uic, compiling and including resource file, etc. It possible that you just have an .exe with external resource file instead of embedded resources.

Qt Creator got own environment in which it compiles, that includes settings, variables and it even may be different compiler, so you have to check settings - they are seen i GUI and stored in .user file.

Qt Creator, depending on version may hide fact that it runs qmake, you you can see that in project settings. Versions I dealt with were using jom as build tool.

Upvotes: 1

Related Questions