从Windows中的waf build Qt程序中删除控制台

时间:2012-04-20 07:33:20

标签: c++ windows qt waf subsystem

我正在使用waf构建这个Qt程序。我在Windows中测试它,每次运行控制台打开的exe文件。 在(Qt)专业文件中(如果使用qmake构建),您只需确保删除

CONFIG += console

但是我不确定是什么链接器标志,我必须在我的wscript(waf)中添加以实现这一点。我必须为msvc编译器指定/SUBSYSTEM: WINDOWS以将我的程序作为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 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我认为你需要/ SUBSYSTEM:WINDOWS,而不是/ SUBSYSTEM = WINDOWS。

答案 2 :(得分:0)

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

#include <windows.h>

if(getRidOfTheConsole)
    FreeConsole();

替代选项(已为您所知)是使用/SUBSYSTEM:WINDOWS。我不知道如何将其放入waf,但还有一种方法 - 将以下内容放入您的int main()文件中:

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