首页 > 解决方案 > 在 Qt Creator 与命令行中编译 C++ 项目 - 生成的 .exe 的不同运行时

问题描述

我今年开始使用 Qt Creator(Windows 10,Qt Creator 4.12.1,基于 Qt 5.14.2)来编写我的 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++ 命令。但是,即使我使用 Qt 生成的 .pro 文件运行 qmake 和 make,它也不会生成与 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

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

标签: c++qtcompilationruntime

解决方案


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

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

Qt Creator 有自己的编译环境,包括设置、变量,甚至可能是不同的编译器,所以你必须检查设置——它们在 GUI 中被看到并存储在 .user 文件中。

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


推荐阅读