在Qt Creator与命令行中编译C ++项目-生成的.exe不同的运行时

问题描述

我从今年开始使用Qt Creator(Windows 10,基于Qt 5.14.2的Qt Creator 4.12.1)编写C ++代码。 在Qt中运行该程序没有问题(编译时会有一些小警告,运行时还可以)。 但是,当我在Qt外部(在命令行中)编译程序时,所生成的可执行文件略有不同:它稍大一些(142而不是130kb),并且运行时要慢得多(大约慢8倍)。>

启动Qt控制台应用程序时,我没有更改Qt Creator生成的任何编译器设置。

我想念什么吗? Qt Creator是否在内部优化程序?

我还注意到,我无法在命令行中从外部运行Qt Creator编译的可执行文件(程序已启动,但是当我读取外部txt文件时它崩溃了。)

这是Qt生成的g ++命令的示例,我也在命令行中使用了它:

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   

当我在命令行中编译程序时,我尝试了qmake / make命令以及原始的g ++命令。但是,即使我运行qmake并使用Qt生成的.pro文件进行制作,它也不会生成与Qt Creator中相同的可执行文件。

如果有兴趣,这是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 \
    [...]

这是生成的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

如果有人能帮助我,那就太好了。

解决方法

是否可能使用Visual Studio版本的Creator?使可执行文件依赖于使用并行运行时的Qt .dll。

单独运行g ++不能从头开始编译项目,必须运行qmake,然后进行make。内部创建者和内部创建者都必须在.pro文件上使用qmake。编译程序包括更多步骤,运行mocuic,编译并包括资源文件,等等。可能只有一个带有外部资源文件的.exe而不是嵌入式资源。

Qt Creator具有自己的编译环境,其中包括设置,变量,甚至可能是不同的编译器,因此您必须检查设置-它们在GUI中可见并存储在.user文件中。

Qt Creator,根据版本可能会隐藏它运行qmake的事实,您可以在项目设置中看到它。我处理的版本使用jom作为构建工具。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...