在 Qt Creator 与命令行中编译 C++ 项目 - 生成的 .exe 的不同运行时
Compiling C++ project in Qt Creator vs. command line - different runtime of resulting .exe
我今年开始使用Qt Creator(Windows10,Qt Creator 4.12.1,基于Qt 5.14.2)编写我的C++代码。
运行Qt中的程序没有问题(编译时有一些小警告,运行时间还可以)。
然而,当我在 Qt 之外(在命令行中)编译程序时,生成的可执行文件略有不同:它有点大(142 而不是 130kb)并且 运行time 慢得多(大约 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.13.0\mingw73_32\include -IC:\Qt.13.0\mingw73_32\include\QtCore -Irelease -IC:\Qt.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.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.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.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.13.0\mingw73_32\bin\qmake.exe -install qinstall
QINSTALL_PROGRAM = C:\Qt.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?这将使可执行文件依赖于使用 side-by-side 运行time.
的 Qt .dll
您无法仅通过 运行ning g++ 从头开始编译项目,您必须 运行 qmake
,然后进行编译。外部创建者和内部您都必须在 .pro 文件上使用 qmake
。编译程序包括更多步骤,运行宁moc
和uic
,编译和包含资源文件等。您可能只有一个带有外部资源文件的.exe而不是嵌入资源。
Qt Creator 有自己的编译环境,其中包括设置、变量,甚至可能是不同的编译器,因此您必须检查设置 - 它们在 GUI 中可见并存储在 .user 文件中。
Qt Creator,根据版本可能会隐藏它 运行s qmake
的事实,您可以在项目设置中看到它。我处理的版本使用 jom
作为构建工具。
我今年开始使用Qt Creator(Windows10,Qt Creator 4.12.1,基于Qt 5.14.2)编写我的C++代码。 运行Qt中的程序没有问题(编译时有一些小警告,运行时间还可以)。 然而,当我在 Qt 之外(在命令行中)编译程序时,生成的可执行文件略有不同:它有点大(142 而不是 130kb)并且 运行time 慢得多(大约 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.13.0\mingw73_32\include -IC:\Qt.13.0\mingw73_32\include\QtCore -Irelease -IC:\Qt.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.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.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.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.13.0\mingw73_32\bin\qmake.exe -install qinstall
QINSTALL_PROGRAM = C:\Qt.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?这将使可执行文件依赖于使用 side-by-side 运行time.
的 Qt .dll您无法仅通过 运行ning g++ 从头开始编译项目,您必须 运行 qmake
,然后进行编译。外部创建者和内部您都必须在 .pro 文件上使用 qmake
。编译程序包括更多步骤,运行宁moc
和uic
,编译和包含资源文件等。您可能只有一个带有外部资源文件的.exe而不是嵌入资源。
Qt Creator 有自己的编译环境,其中包括设置、变量,甚至可能是不同的编译器,因此您必须检查设置 - 它们在 GUI 中可见并存储在 .user 文件中。
Qt Creator,根据版本可能会隐藏它 运行s qmake
的事实,您可以在项目设置中看到它。我处理的版本使用 jom
作为构建工具。