Qt Creator と Clang

この記事は Qt Blog の "Qt Creator and Clang" を翻訳したものです。
執筆: Erik Verbruggen, 2011年10月19日

Qt Creator エディタニュース」で触れたように、私たちは今、Qt Creator の独自実装である C++ コードモデルを Clang に置き換えようとしています。まだまだ開発は始まったばかりですが、他の人々にも試してもらい、さらには貢献していただける状態になったと思います。

「でも、コンパイラは既に持っているけど。」と思った方はいらっしゃいませんか。置き換えについて話したところ、たくさんのこのような反応をいただきました。「コードモデル」はコンパイラではありません。ですので、依然として任意のコンパイラは必要です。

コードモデルとは何か?

コードモデルは IDE が開発中のアプリケーションで使われている言語を「理解」するために使われています。コードモデルを利用して以下のような機能が実現できます。

  • コード補完
  • シンタックスハイライト (静的およびセマンティック)
  • ナビゲーション (クイックアクセスやシンボル追跡など)
  • イントロスペクション (クラスブラウザやアウトライン表示など)
  • 診断とツールチップ
  • 出現箇所の検索やリネーム
  • リファクタリング

このためには言語のパーサやセマンティックな解析が必要になります。コードモデルがコンパイラと異なるのは実行形式を生成しないことだけです。

Clang へ移行する理由

現在使われている独自実装のコードモデルを Clang へ変更することによって、重要な改善が得られます。その中でも特に重要なのが Clang が実際のコンパイラであるため、そこから出る情報が確かなものであるということです。エディタが出す警告やエラーなどの情報は、不完全なものや近似したものではなく、コンパイラが出すものと全く同じものになります。これらの診断に言及するならば、Clang は診断時の詳細な情報にフォーカスしたコンパイラなので、開発者がタイポするというような珍しいケースにおいて非常に役立ちます。 :-)

また、Clang は既に C++98/03 や C89, C99, Objective-C (と Objective-C++) をサポートし、C++11 のサポートも活発に開発されています。

しかし、これらのメリットがただで手に入るわけではありません。Clang をコードモデルとして使う場合、オブジェクトファイルの生成は必要ありませんが、ソースファイルのパースと解析は行わなくてはなりません。STL だけを使うような小さなプロジェクトでは問題ないでしょう。しかし、Qt Creator のように Qt の多くの機能を使うような巨大なプロジェクトでは、一つのソースファイルを処理する際に include されているすべてのファイルも処理されるので時間がかかります。その対応としてはプリコンパイル済みヘッダがあります。現在の実装ではプリコンパイル済みヘッダをプロジェクトに手動で追加するか、Qt Creator に設定する必要があります。詳細を以下に示します。

現状の実装内容

現在の実装では Clang を下記の機能で使っています。

      • シンタックスハイライト ("キーワードハイライト") およびセマンティックハイライト (型/メソッド/変数/ラベル/etc)
      • コード補完
      • 診断
      • コードナビゲーション

上記の機能でも限られた状況でだけ動作するものがあることに注意してください。

Clang 対応を試す

まず、Clang/LLVM の 3.0 か trunk をコンパイルする必要があります。ビルド方法は LLVM の Web サイト にあります。svn ではなく git を使いたければ、llvm.gitclang.git を使ってください。(最適化された)リリースビルドをするのを忘れずに!

Qt Creator の Clang 対応は gitorious の git リポジトリの wip/clang ブランチにあります。Qt Creator は通常のビルド方法でかまいません。qmake, make の順に実行し、プラットフォーム次第ですが make install してください。Linux や Mac で llvm-config が実行パスに無い場合や、Windows の場合、qmake の LLVM_INSTALL_DIR 変数で qmake に Clang と LLVM のヘッダとライブラリの場所を指示してください。

Qt Creator の実行時に、ライブラリのサーチパス(Linux では LD_LIBRARY_PATH、Windows では PATH)に <llvm-install-prefix>/lib が含まれていて、clang のライブラリがそこにあることを確認してください。

使い方

Clang 対応の Qt Creator はそうでない Qt Creator と同様に使うことができます。しかし、一つだけ気をつけてください。Qt や Boost のような巨大なライブラリをプロジェクトで使用する場合、プリコンパイル済みヘッダを定義することで、コードモデル(すなわちシンタックスハイライトやコード補完)をかなり高速化させることができます。典型的な Qt アプリのプロジェクトでは以下のようなヘッダファイルを作成します。

#ifndef PCH_H
#define PCH_H

#include <QtCore>
#include <QtGui>

#endif // PCH_H

(システムに属するヘッダなど)変更される可能性のないヘッダファイルだけを include してください。ヘッダが変更されるとプリコンパイル済みヘッダを再生成する必要が生じてしまうため、キャッシュする利点をかき消してしまいます。

Qt Creator がこのヘッダをキャッシュとして使うようにするためには、二つの方法があります。

      • qmake を使用している場合、.pro ファイルに PRECOMPILED_HEADER 変数を記述します。PRECOMPILED_HEADER = pch.h

        qmake の spec ファイルで指定されているコンパイラ次第ですが、プリコンパイル済みヘッダはコンパイル時に使用されることもあります。

      • "プロジェクト" モード で指定することもできます。追加された "Code Completion Settings" タブで "custom" ヘッダを選択してください。このオプションを使う場合、コードモデルはそのヘッダをキャッシュとして使います。しかし、コンパイラからは使用されません。

貢献方法

この記事の最初に述べたように、このリリースは主に貢献してくれる方々のためのものです。なぜなら、まだ開発途中ですので。まだまだやることは色々あります。開発を助けてくれる方はぜひ連絡してください。qtcreator メーリングリスト や IRC で freenode の #qt-creator チャンネルが主なコミュニケーション手段です。


Blog Topics:

Comments

?
Roman
0 points
187 months ago

Awesome!

?
cartman
0 points
187 months ago

Thanks!

?
John Tapsell
0 points
187 months ago

There are various spelling mistakes - can you run some sort of script to check for spelling mistakes in code and documentation?

For example, for a unit test doing --help you get:

-flush : Flushes the resutls

?
arg0
0 points
187 months ago

Oh Yeah!!! I was waiting it for months!!! Thanks!

?
Johnny P.
0 points
187 months ago

GREAT!!!

?
Bruno
0 points
187 months ago

Wow !
Thanks for the Visual Studio package too !

Thanks !

?
Remi
0 points
187 months ago

Good news! and it's cool to have VS2008 binaries!

?
divide
0 points
187 months ago

Awesomazing !

?
detro
0 points
187 months ago

Why is the Phonon support not available on the Qt 4.6 RC Package on Linux x86?
Is the only way to have Phonon working there is to manually compile Qt from scratch?

Thanks

?
Stephan Jaensch
0 points
187 months ago

Would it be possible to do a Mac OS X release with 32bit as well as 64bit binaries? Or just 64bit binaries, if the former is too big?

?
Thorbj&#248;rn Lindeijer
0 points
187 months ago

@detro: Not shipping Phonon sounds unintentional, we'll look into it.

?
Thiago Macieira
0 points
187 months ago

Stephan: The Mac Cocoa packages should be 32- and 64-bit. See the download link in http://qt.nokia.com/develop....

?
Stefano
0 points
186 months ago

Sorry,
I'm confused.. Goind round in the blog and searching for on the web, I still don't understand if finally Qt 4.6 will support phonon..
Is there anyone who can tell us about that?

?
Jaroslav Smid
0 points
186 months ago

Does MSVC version come with 64bit libraries? If not, do you plan to release it (I'm not quite happy that I had to compile it by myself from source code what is very time consuming)? And what about to release QtCreator x64 for Windows - it compiles and it works and you can compile it.
Qt even compiles with 64bit mingw available from mingw-w64 project on sourceforge, but it requires some patching stuff as there are some (stupid) typecasts from pointer to long in Qt code (where ptrdifft or sizet would be much better choice, preventing unwanted errors).
Also, there are lots of errors in ".pro" files for configuring Qt preventing to build Qt with dynamic linkage to external libraries like zlib, libpng, etc. on windows when using mingw. You just do

!contains(QTCONFIG, zlib):!contains(QTCONFIG, no-zlib) {
unix:LIBS += -lz
win32:LIBS += libz.lib
}

which is wrong as on windows one can have gcc/mingw and there should be -lz switch instead of libz.lib one, so further tests on mingw/MSVC are required.

Some patches making Qt compile with mingw-w64 (fixing Qt code errors (present with MSVC x64 build too, but silencely supressed), not mingw's ones) are to be found at http://svn.clazzes.org/svn/.... They are really few lines patches and it would be great to have them in Qt 4.6 so 64bit libraries are really safe and don't crash when they get >4GB pointers.

?
nicolas
0 points
186 months ago

I can see a -declarative option in the configure, does that means QML will be in Qt 4.6.0, even if not officially supported?
Pity that I have an error when doing:
./configure -declarative
...
Error: Unable to locate the qt-declarative package. Refer to the documentation on how to build the package.
Tested on Linux 64 bits.

Also, that would be nice to have 64bits librairies for Windows.

?
Harald Fernengel
0 points
186 months ago

@Jaroslav: Thanks for the info, feel free to use qt.gitorious.org to submit your patches :)

?
Thiago Macieira
0 points
186 months ago

@Stefano: Qt 4.6 supports Phonon, of course. Phonon is supported since Qt 4.4 (Windows CE support in 4.5).

?
mono2k
0 points
186 months ago

QGLWidget::renderText is completely broken even in precompiled examples. Tested on X1650 and GF9600, both on Windows 7, both with latest drivers...

?
Paul Colby
0 points
186 months ago

I'm getting a build error when building rc1 from source - the error is:
dialogs/qcolordialog_mac.mm:329: error: stray '@' in program
dialogs/qcolordialog_mac.mm:332: error: stray '@' in program
The colordialog_mac.mm file uses "@try" and "@catch" - never seen those before? Is that a Qt thing, or a mistake?

(didn't see this error building 4.5.* nor 4.6.0 beta1).

pc.

?
KimmoT
0 points
186 months ago

I guess there's documentation somewhere, but 2 things I'd like to know:
1. Does VS2008 version work with express version too? What are the pro/con compared to QT creator?
2. Can Symbian binary package be installed "on top of" windows/VS2008 package?

?
Morten
0 points
186 months ago

@Paul: "@try" - that's objective-c code. From the looks of the error message it seems that Qt is trying to compile qcolordialog_mac.mm as a plain c++ file. This error escaped our testing here so I have the standard questions for you: On which version of OS X are you building, and what does your configure line look like?

?
Jason McDonald
0 points
186 months ago

@nicolas: Declarative UI will be a separate package to Qt 4.6, but needed a couple of enablers added to Qt 4.6, one of which is the configure flag.

?
Jason McDonald
0 points
186 months ago

@KimmoT:
1. The VS2008 package is intended to work with the Express Edition.
2. You should install them in separate directories, as they are stand-alone packages.

?
Jason McDonald
0 points
186 months ago

@mono2k: Please submit a bug report at http://bugreports.qt.nokia.com. In the bug report, please indicate which pre-compiled windows package you were using (there are five to choose from), and which examples weren't working for you.

?
Philippe
0 points
186 months ago

Concerning Windows x64 binaries, the situation is somehow paradoxal: Windows is the most popular platform (quantitavely speaking, I guess), and will the smallest binaries, and this is the sole (desktop) platform where only 32 bit binaries are provided. Speaking as a commercial licensee.

?
Stephan Jaensch
0 points
186 months ago

Thiago: Sorry for replying so late. I tried both the Qt + Qt Creator SDK download as well as "Qt 4.6.0 RC for Mac OS X Cocoa Debug libs". Check this out:

$ file /Library/Frameworks/QtCore.framework/QtCore
/Library/Frameworks/QtCore.framework/QtCore: Mach-O universal binary with 2 architectures
/Library/Frameworks/QtCore.framework/QtCore (for architecture i386):Mach-O dynamically linked shared library i386
/Library/Frameworks/QtCore.framework/QtCore (for architecture ppc):Mach-O dynamically linked shared library ppc

No 64 bit goodness in there, which is a hassle when compiling Qt applications (need to specify -arch i386 in every project). On the other hand, I couldn't care less about all that PPC code in there. :)

?
Luke Downey
0 points
186 months ago

I'm getting the strangest bug viewing the Qt documentation sets, both in Assistant and Creator. See: http://www1.chapman.edu/~do...

My build system is a combination of ICC and LLVM GCC; I'm guessing somewhere in QtHelp there's some macro-included code that's gone awry?

?
Luke Downey
0 points
186 months ago

Oh, also re: Paul Colby / Morten, this is a bug (maybe?) with Apple's GCC misidentifying an Objective-C++ file's type. Either use the G++ front end, or possibly try the -ObjC++ flag.

?
Luke Downey
0 points
186 months ago

Noticed a strange bug when viewing the Qt documentation sets, both in Assistant and Creator.

See: http://www1.chapman.edu/~do...

My build system is a combination of ICC and LLVM-GCC; I'm guessing the problem lies with a macro-included code block or some such in QtHelp?

?
Thomas Friedl
0 points
186 months ago

Doesn't compile with VS2008.

QNetworkReplyHandler.obj : error LNK2001: unresolved external symbol "public: vi
rtual struct QMetaObject const * __thiscall WebCore::FormDataIODevice::metaObjec
t(void)const " (?metaObject@FormDataIODevice@WebCore@@UBEPBUQMetaObject@@XZ)
QNetworkReplyHandler.obj : error LNK2001: unresolved external symbol "public: vi
rtual void * _thiscall WebCore::FormDataIODevice::qtmetacast(char const *)" (?
qt_metacast@FormDataIODevice@WebCore@@UAEPAXPBD@Z)
QNetworkReplyHandler.obj : error LNK2001: unresolved external symbol "public: vi
rtual int _thiscall WebCore::FormDataIODevice::qtmetacall(enum QMetaObject::Ca
ll,int,void * *)" (?qt_metacall@FormDataIODevice@WebCore@@UAEHW4Call@QMetaObject
@@HPAPAX@Z)
QNetworkReplyHandler.obj : error LNK2001: unresolved external symbol "public: vi
rtual struct QMetaObject const * __thiscall WebCore::QNetworkReplyHandler::metaO
bject(void)const " (?metaObject@QNetworkReplyHandler@WebCore@@UBEPBUQMetaObject@
@XZ)
QNetworkReplyHandler.obj : error LNK2001: unresolved external symbol "public: vi
rtual void * _thiscall WebCore::QNetworkReplyHandler::qtmetacast(char const *)
" (?qt_metacast@QNetworkReplyHandler@WebCore@@UAEPAXPBD@Z)
QNetworkReplyHandler.obj : error LNK2001: unresolved external symbol "public: vi
rtual int _thiscall WebCore::QNetworkReplyHandler::qtmetacall(enum QMetaObject
::Call,int,void * *)" (?qt_metacall@QNetworkReplyHandler@WebCore@@UAEHW4Call@QMe
taObject@@HPAPAX@Z)
QNetworkReplyHandler.obj : error LNK2019: unresolved external symbol "protected:
void __thiscall WebCore::QNetworkReplyHandler::processQueuedItems(void)" (?proc
essQueuedItems@QNetworkReplyHandler@WebCore@@IAEXXZ) referenced in function "pub
lic: void __thiscall WebCore::QNetworkReplyHandler::setLoadMode(enum WebCore::QN
etworkReplyHandler::LoadMode)" (?setLoadMode@QNetworkReplyHandler@WebCore@@QAEXW
4LoadMode@12@@Z)
FrameLoaderClientQt.obj : error LNK2019: unresolved external symbol "protected:
void __thiscall QWebPage::unsupportedContent(class QNetworkReply *)" (?unsupport
edContent@QWebPage@@IAEXPAVQNetworkReply@@@Z) referenced in function "public: vi
rtual void __thiscall WebCore::FrameLoaderClientQt::download(class WebCore::Reso
urceHandle *,struct WebCore::ResourceRequest const &,struct WebCore::ResourceReq
uest const &,class WebCore::ResourceResponse const &)" (?download@FrameLoaderCli
entQt@WebCore@@UAEXPAVResourceHandle@2@ABUResourceRequest@2@1ABVResourceResponse
@2@@Z)
FrameLoaderClientQt.obj : error LNK2019: unresolved external symbol "protected:
void __thiscall QWebPage::downloadRequested(class QNetworkRequest const &)" (?do
wnloadRequested@QWebPage@@IAEXABVQNetworkRequest@@@Z) referenced in function "pu
blic: virtual void __thiscall WebCore::FrameLoaderClientQt::startDownload(struct
WebCore::ResourceRequest const &)" (?startDownload@FrameLoaderClientQt@WebCore@
@UAEXABUResourceRequest@2@@Z)
........libQtWebKitd4.dll : fatal error LNK1120: 9 unresolved externals
NMAKE : fatal error U1077: '"C:Program Files (x86)Microsoft Visual Studio 9.0
VCBINlink.EXE"' : return code '0x460'
Stop.
NMAKE : fatal error U1077: '"C:Program Files (x86)Microsoft Visual Studio 9.0
VCBINnmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: 'cd' : return code '0x2'
Stop.

I get it - it's a RC. But it would be real nice to have a working VS version in the future. Even the final 4.5 doesn't compile on VS2008 without errors.

?
Luke Downey
0 points
186 months ago

Thomas: I've compiled 4.5/4.6rc1, including WebKit, recently with both MSVC9 and ICC 11.1. Admittedly I don't use the vanilla build system / mkspecs, but WebKit is easily the largest and most complex component of the distribution. It looks like the source files defining the FormDataIODevice and QNetworkReplyHandler classes, possibly one or two others, aren't included in the build when they should be. I'd take a look through the various .pro and .pri files as a first step, or just skip configure with -no-webkit if you don't need it.

?
Jason McDonald
0 points
186 months ago

@ Thomas Friedl: I haven't been able to reproduce the build failure you describe. Please submit a bug report to http://bugreports.qt.nokia.com, including the complete configure line you used, OS version/patches, VS version/patches and a dump of your environment variables.

?
egarcia
0 points
186 months ago

Does this means that from now on, we can expect Visual Studio binary packages to be always released, like MinGW ones?

?
Jason McDonald
0 points
186 months ago

@egarcia: Yes, we intend to release both mingw and vs2008 binary packages for as long as those compilers are in our list of "Tier 1" platforms.

?
Milan Stezka
0 points
186 months ago

I confirm that the linker errors QNetworkReplyHandler.obj : error LNK2001: unresolved external symbol... are there. Happened to me too.

?
wildabeast
0 points
186 months ago

Fantastic. I'm using Qt 4.6.0 rc for Symbian to implement PhoneGap for Symbian(www.phonegap.com).

Here's a blog about my progress.
http://blogs.nitobi.com/rya...

Also, I'm having a couple of issues, and would love some assistance on these issues:

Cross domain security for local resource: http://discussion.forum.nok...
Adding a QObject as Q_PROPERTY: http://discussion.forum.nok...

Its open source! Helping me is helping the community :)

?
yavuz
0 points
186 months ago

mysql sqldrivers arent included in visual studio binary edition. Will it be int the 4.6 final, or can somebody explain, if possible, how can i complile it without compiling whole Qt source