3

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

CONFIG += console

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

3 回答 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
于 2012-04-22T08:41:30.120 回答
0

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

于 2012-04-20T08:26:23.260 回答
0

一旦我使用了一种骇人听闻的方法——当一个应用程序,取决于命令行标志,必须表现为一个控制台应用程序或一个 UI 应用程序(没有控制台)。它归结为(在 Windows 下)将其构建为控制台应用程序并在满足某些条件时摆脱控制台:

#include <windows.h>

if(getRidOfTheConsole)
    FreeConsole();

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

#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
于 2012-04-20T08:55:59.087 回答