Manish Chakravarty ([info]manish_chaks) wrote,

CMake + Qt4 + XCode on OSX

After joining TW , the MacBookPro has become my primary dev machine (
including my open source work which is mainly KDE ). I end up using OS X
Leopard most of the time and I love it.

Qt4 is widely used GUI
library. KDE is built on top of Qt4. Many well-known GUI applications such
as Google Earth, Google Talk, Picassa, Skype are all Qt applications.

CMake is a cross-platform
make system. It is used by many big open source projects such as KDE4 and VTK

Using Qt4 you can write rich GUI applications in C++ and in Java ( using Jambi
). Qt4 supports Windows, Linux, OSX , BSD and Solaris.

You can get Qt4 for Mac from here

XCode is the default IDE on OSX.

So I decided to find out a way to use
- CMake as my build system
- Qt4 as my GUI library ( IMO its the best GUI library out there )
- XCode as my IDE

And here's how you do it.

Download CMake from here

Create a folder; let's call it HelloWorld

Create a file called main.cpp in that folder.

Let's write a simple Hello World in Qt4

=======================
#include <QApplication>
#include <QPushButton>
#include <QString>


int main ( int argc, char ** argv )
{
QApplication app (argc, argv);

QPushButton hello ("Hello World");

QString string = "my Object name is " + app.objectName();

return app.


}
=======================

Now create a file called CMakeLists.txt in the same folder and write the following:
(comments inline )

=======================
PROJECT ( HelloWorld )

SET ( HelloWorld_SRCS ./main.cpp )

#enable all warnings
ADD_DEFINITIONS ( -Wall )

#This command find Qt4 and all required variables
FIND_PACKAGE (Qt4 REQUIRED)

#The QT_USE_FILE macro is defined by FIND_PACKAGE that contains a path to the CMake script
INCLUDE (${QT_USE_FILE})

INCLUDE_DIRECTORIES( ${CMAKE_BINARY_DIR} )

#Here we instruct to build sample executable from all the source files
ADD_EXECUTABLE ( HelloWorld ${HelloWorld_SRCS} )

#last thing that we need to do is to tell CMake what libraries our executable needs
#Luckily FIND_PACKAGE preparted QT_LIBRARIES variable for us
TARGET_LINK_LIBRARIES( HelloWorld ${QT_LIBRARIES})
=======================

Now for the magic

Run " cmake -G Xcode"

CMake will generate an XCode project for you :)

Just open the HelloWorld.xcodeproj in XCode and you are done.

The obligatory screenshot is attached :)



--
Manish Chakravarty
http://manish-chaks.livejournal.com/
Tags: qt4 osx cmake geek

  • Post a new comment

    Error

    Your reply will be screened

    Your IP address will be recorded 

  • 5 comments

Anonymous

July 4 2008, 20:03:06 UTC 3 years ago

Please don't forget to pull out your blog from KDE4 Solaris blog list at opensolaris.org, if your target OS is Mac. Or tag them so the MacOS specific posts do not have to appear at the opensolaris site.

Raul

[info]manish_chaks

July 4 2008, 20:11:33 UTC 3 years ago

Is my blog syndicated at opensolaris.org ?
I didnt know that.


If yes, then where?

I am primarily a KDE guy and I work on KDE on Linux , Solaris and Mac.

If my blog is syndicated, I would setup a "Solaris" tag

Anonymous

August 25 2008, 08:21:33 UTC 3 years ago

Thank you.

I also switched from KDE environment to OS X. Is there a way to integrate open source QT into XCode?

[info]manish_chaks

August 25 2008, 09:27:01 UTC 3 years ago

Re: Thank you.

You are in luck! qmake on OS X can generate .xcodeproj files so you will not have a problem!

Just go to the top-level dir and fire a "qmake"
you will see a .xcodeproj file generated in the current dir.

Example: ( a simple project with just one file "main.cpp")

manish:Qt4XCodeTest manish$ ls
main.cpp
manish:Qt4XCodeTest manish$ qmake -project
manish:Qt4XCodeTest manish$ ls
Qt4XCodeTest.pro main.cpp
manish:Qt4XCodeTest manish$ qmake
manish:Qt4XCodeTest manish$ ls
Info.plist Qt4XCodeTest.pro Qt4XCodeTest.xcodeproj main.cpp


You can open up the *.xcodeproj file direcly in XCode and can view / browse / edit / build the sources directly from XCode.


Anonymous

January 18 2010, 17:56:26 UTC 2 years ago

Cmake or qmake?

Why don't you just use qmake instead than Cmake? There is any specific reason for that? IMHO would be easier to set up than Cmake, and you don't lose the portability of your code...
Create an Account
Forgot your login or password?
Facebook Twitter More login options
English • Español • Deutsch • Русский…