在CodeBlock中使用QT

来源:互联网 发布:r语言编程合集 编辑:程序博客网 时间:2024/05/16 11:57
最近一直使用vim写QT的程序,不是很爽,尤其是自动补全。
今天,想起使用CodeBlock来,就下载了一个使用。结果不能够新建QT的工程,真是郁闷,后来google了一下,在一个德国的论坛上看到了类似的问题的一个帖子。
说是修改CodeBlock的一个templates文件就可以了。

找到那个文件:
/usr/share/codeblocks/templates/wizard/qt4下面有一个wizard.script。
就是它了。

因为我的QT不是源码编译的,是apt-get 回来的,所以分布比较乱,是按照debian的目录分布的,但是CodeBlock是按照所有的文件分布在一个目录下来进行配置的,所以新建工程的时候会出问题。

apt-get回来的QT的目录在/usr/share/qt4,需要在目录下面有include和lib,QT编译的include在/usr/include/qt4下面,连接库在/usr/lib下面。
修改了之后,再次新建工程,发现在lib选项上出问题了,提示lib必须为.a或者是.lib结尾的文件,真是郁闷。

写了一个脚本自动的将.so文件做一个链接。
#!/bin/sh
#
#Author:wangyao @ 07-01-04
#Make link for Qt library for CodeBlocks
#
#

for old in $(ls /usr/lib/libQt*.so)
do
    echo $old
    new=$(echo $old | sed 's/.so/.a/')
    echo $new
    ln -s $old $new
done
运行后就将QT编译需要的一些库做了链接,再新建工程,好使了。

试着写了一个程序,编译一下,link的时候出错了???
原来它连接的是-lQtCore4,如果你从命令行里面编译的话,就会看到是-lQtCore,改一下工程的属性就可以了。如果想一劳永逸的话,就去改一下上面提到的那个文件,将链接库中去掉"4".

下面是我改过的那个wizard.script:
////////////////////////////////////////////////////////////////////////////////
//
// Qt4 project wizard
//
////////////////////////////////////////////////////////////////////////////////

// globals
QtPathDefault    <- _T("$(#qt)");
QtPathDefaultInc <- _T("$(#qt.include)");
QtPathDefaultLib <- _T("$(#qt.lib)");
QtPath <- _T("");

function BeginWizard()
{
    local intro_msg = _T("Welcome to the new Trolltech Qt4 project wizard!/n" +
                         "This wizard will guide you to create a new Qt4 project/n" +
                         "using the Trolltech Qt4 cross-platform GUI toolkit/n/n" +
                         "When you're ready to proceed, please click /"Next/"...");

    local qtpath_msg = _T("Please select the location of Trolltech Qt4 on your computer./n" +
                          "This is the top-level folder where Qt4 was installed./n" +
                          "To help you, this folder must contain the subfolders/n" +
                          "/"include/" and /"lib/".");

    Wizard.AddInfoPage(_T("QtIntro"), intro_msg);
    Wizard.AddProjectPathPage();
    Wizard.AddGenericSelectPathPage(_T("QtPath"), qtpath_msg, _T("Qt's location:"), QtPathDefault);
    Wizard.AddCompilerPage(_T(""), _T("gcc*"), true, true);
}

////////////////////////////////////////////////////////////////////////////////
// Qt's path page
////////////////////////////////////////////////////////////////////////////////

function OnLeave_QtPath(fwd)
{
    if (fwd)
    {
        local dir         = Wizard.GetTextControlValue(_T("txtFolder")); // txtFolder is the text control in GenericSelectPathPage
        local dir_nomacro = VerifyDirectory(dir);

        if (dir_nomacro.IsEmpty())
            return false;

        // verify include dependencies
        local dir_nomacro_inc = GetCompilerIncludeDir(dir, QtPathDefault, QtPathDefaultInc);
        if (dir_nomacro_inc.IsEmpty())
            return false;
        if (!VerifyFile(dir_nomacro_inc + wxFILE_SEP_PATH + _T("QtGui"), _T("QApplication"), _T("Qt's include"))) return false;

        // verify library dependencies
        local dir_nomacro_lib = GetCompilerLibDir(dir, QtPathDefault, QtPathDefaultLib);
        if (dir_nomacro_lib.IsEmpty())
            return false;
    //Edit by wangyao at 07-01-04
        if (!VerifyLibFile(dir_nomacro_lib, _T("QtCore"), _T("Qt's"))) return false;


        QtPath = dir; // Remember the original selection.

        local is_macro = _T("");

        // try to resolve the include directory as macro
        is_macro = GetCompilerIncludeMacro(dir, QtPathDefault, QtPathDefaultInc);
        if (is_macro.IsEmpty())
        {
            // not possible -> use the real inc path we had computed instead
            QtPathDefaultInc = dir_nomacro_inc;
        }

        // try to resolve the library directory as macro
        is_macro = GetCompilerLibMacro(dir, QtPathDefault, QtPathDefaultLib);
        if (is_macro.IsEmpty())
        {
            // not possible -> use the real lib path we had computed instead
            QtPathDefaultLib = dir_nomacro_lib;
        }
    }
    return true;
}

// return the files this project contains
function GetFilesDir()
{
    return _T("qt4/files");
}

// setup the already created project
function SetupProject(project)
{
    project.AddIncludeDir(QtPathDefaultInc);
    project.AddIncludeDir(QtPathDefaultInc + wxFILE_SEP_PATH + _T("QtGui"));

    project.AddLibDir(QtPathDefaultLib);

    // add link libraries
    //Edit by wangyao at 07-01-04
    project.AddLinkLib(_T("QtCore"));
    project.AddLinkLib(_T("QtGui"));

    // enable compiler warnings (project-wide)
    WarningsOn(project, Wizard.GetCompilerID());

    // Debug
    local target = project.GetBuildTarget(Wizard.GetDebugName());
    if (!IsNull(target))
    {
        target.SetTargetType(ttConsoleOnly); // ttConsoleOnly: console for debugging
        target.SetOutputFilename(Wizard.GetDebugOutputDir() + Wizard.GetProjectName() + DOT_EXT_EXECUTABLE);
        // enable generation of debugging symbols for target
        DebugSymbolsOn(target, Wizard.GetCompilerID());
    }

    // Release
    target = project.GetBuildTarget(Wizard.GetReleaseName());
    if (!IsNull(target))
    {
        target.SetTargetType(ttExecutable); // ttExecutable: no console
        target.SetOutputFilename(Wizard.GetReleaseOutputDir() + Wizard.GetProjectName() + DOT_EXT_EXECUTABLE);
        // enable optimizations for target
        OptimizationsOn(target, Wizard.GetCompilerID());
    }

    return true;
}
我只是改变了两个地方。

 原文地址 http://blog.chinaunix.net/u/12592/showart_226437.html
原创粉丝点击