在Windows中构建Qt程序时,如何从waf编译中移除控制台

3
我有一个使用waf构建的Qt程序。我在Windows上进行测试,每次运行exe文件时,控制台都会打开。 在(pro)文件中(如果使用qmake进行构建),您只需要确保删除

即可。
CONFIG += console

但我不确定我需要添加什么链接器标志到我的wscript(waf)中才能实现这一点。我必须指定 /SUBSYSTEM:WINDOWS ,以便msvc编译器将我的程序视为Windows程序。

请帮忙。

我的wscript.py

#! /usr/bin/env python
# encoding: utf-8
# Thomas Nagy, 2005, 2011 (ita)
"""
Including the moc files *is* the best practice (KDE), not doing it is easy,
but makes the compilations about 30-40% slower on average.
If you still want the slow version (we warned you!), see the example located
in the folder playground/slow_qt/
"""
VERSION='0.0.1'
APPNAME='qt4_test'
top = '.'
out = 'build'
def options(opt): 
    opt.load('compiler_cxx qt4 compiler_c boost')
def configure(conf):
    conf.load('compiler_cxx qt4 compiler_c boost')
    #if conf.env.COMPILER_CXX == 'msvc': g++
    #conf.check_tool('boost')
    conf.env.append_value('CXXFLAGS', ['-DWAF=1']) # test
    #conf.env.append_value('DWAF','1')
    #conf.recurse(subdirs)
    #conf.check_cc( ccflags='-mwindows', mandatory=True, msg='Checking for flags -mwindows')
def build(bld):
    cxxflags = bld.env.commonCxxFlags
    uselibcommon = 'QTCORE QTGUI QTOPENGL QTSVG QWIDGET QTSQL QTUITOOLS QTSCRIPT'
    bld(features = 'qt4 cxx',  includes = '.',source   = 'ListModel.cpp', target = 'ListModel.o', uselib = uselibcommon, cxxflags=cxxflags)
    bld(features = 'qt4 cxx',  includes = '.', source   = 'Model.cpp', target = 'Model.o', uselib = uselibcommon,  cxxflags=cxxflags)
    bld(features = 'qt4 cxx',  includes = '.', source = 'ProxyModel.cpp' , target = 'ProxyModel.o', uselib = uselibcommon, cxxflags=cxxflags)
    flags = cxxflags + ['-DWAF']
    bld(features = 'qt4 cxx',  includes = bld.options.boost_includes, source = 'TableModel.cpp', target = 'TableModel.o', uselib = uselibcommon, cxxflags=flags)
    bld(features = 'qt4 cxx',  includes = '.', source   = 'SongItem.cpp', target = 'SongItem.o',use = 'ListModel.o',  cxxflags=cxxflags)

    use = [ 'sqlite3.o', 'Master.o' , 'DatabaseUtil' , 'SQLiteError.o' ,  'Vector.o' , 'Song.o' , 'Songs.o' , 'SQLiteVector.o' , 'SQLiteVectorIterator.o' , 'ListModel.o' , 'Model.o' , 'TableModel.o' , 'SongItem.o'  ,  'ProxyModel.o']
    bld(features = 'qt4 cxx c', uselib = uselibcommon, includes = bld.options.boost_includes , source   = 'MainWindow.cpp' , target = 'MainWindow.o', lib = ['phonon'], libpath = ['/usr/lib'], use = use, cxxflags=cxxflags)

    use = [ 'sqlite3.o', 'Master.o' , 'DatabaseUtil' , 'SQLiteError.o' ,  'Vector.o' , 'Song.o' , 'Songs.o' , 'SQLiteVector.o' , 'SQLiteVectorIterator.o' , 'ListModel.o' , 'Model.o' , 'TableModel.o' , 'SongItem.o'  , 'ProxyModel.o',
    'MainWindow.o']
    bld(features = 'qt4 cxx cxxprogram', includes = bld.options.boost_includes, source = 'main.cpp MasterDetail.qrc', target   = 'app', uselib = uselibcommon , cxxflags=cxxflags, use = use, linkflags = (['-Wl,-subsystem,windows']) )

from waflib.TaskGen import feature, before_method, after_method
@feature('cxx')
@after_method('.')
@before_method('apply_incpaths')
def add_includes_paths(self):
        incs = set(self.to_list(getattr(self, 'includes', '')))
        for x in self.compiled_tasks:
                incs.add(x.inputs[0].parent.path_from(self.path))
        self.includes = list(incs)
3个回答

2
我已经找到解决方案。
在Windows上,使用(MinGW)。
linkflags = ['-Wl,-subsystem,windows'], -> to disable the console
linkflags = ['-Wl,-subsystem,console'], -> to enable the console

使用 (msvc)

subsystem='windows', -> to disable the console
subsystem='console', -> to enable the console

0

我认为您需要使用/SUBSYSTEM:WINDOWS,而不是/SUBSYSTEM = WINDOWS。


有什么想法,我怎样可以在WAF中使用它...我在谷歌搜索中没有得到任何结果。 - user671253

0

曾经我使用过一种hackish方法 - 当一个应用程序根据命令行标志必须表现为控制台应用程序或无控制台的UI应用程序时。在Windows下,它归结为将其构建为控制台应用程序,并在满足某些条件时摆脱控制台:

#include <windows.h>

if(getRidOfTheConsole)
    FreeConsole();

备选方案(你已经知道的)是使用/SUBSYSTEM:WINDOWS。我不知道怎么在waf中放置它,但还有另一种方法 - 在你的int main()文件中添加以下内容:

#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")

谢谢...好的解决方案...但不完全是我要找的解决方案...我正在寻找一种可以将我的应用程序构建为Windows应用程序(应用程序不需要控制台,可能是因为它会创建自己的窗口与用户进行交互),而非默认的控制台应用程序。 - user671253

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接