June 27, 2011

bovilexic bovilexic
Ant Farmer
76 posts

Qt Creator for shared library on Mac?

 

Greetings –

I have an existing Qt application that compiles on both Mac and PC with Qt Creator. With the help of the Qt plugin for Visual Studio 2008, I have created a VS 2008 project for the application. The intention was ultimately to create a shared library, and I was able to change the VS 2008 project from creating an application to a shared library by simply changing the project type to produce a dll file instead of an exe (via the project properties).

As usual with Qt applications, the “main” entry point simply creates a QApplication, shows the main window (widget) and execs it:

  1. int main(int argc, char *argv[])
  2. {
  3.     QApplication a(argc, argv);
  4.     MyWindow w;
  5.     w.show();
  6.     return a.exec();
  7. }

In VS 2008, I was able to turn this into a DLL by simply changing the project type to dll from exe, and changing the suffix accordingly. By adding code for a function that’s known by another program, that other program can load the dll, and execute the function. That entry point simply does just what main() does, and so the host program launches the app as a “plugin”.

So far, so good, eh?

Well, now I’d like to do the same thing on a Macintosh. Of course, the same source compiles under Qt Creator on the Mac. But although I’ve done some XCode and Qt Creator application programming on the Mac, I just don’t know where to begin. I don’t really care if I use XCode or Qt Creator for development, but I basically just need a project that creates a shared library (a dylib) from the source code for the application, and add the few entry points that the host program can call, and again simply launch QApplication, show the main window (dialog), and exec.

Should be pretty simple, but documentation is sparse, and web searching just leads down a lot of rat-holes…

Any help/advice/example projects (either XCode or Creator) would be most welcome!

Thanks!

— Philip

[EDIT: code formatting, please wrap in @-tags, Volker]

7 replies

June 27, 2011

Tobias Hunger Tobias Hunger
Robot Herder
3386 posts

main() is reserved as the entry point of an application. So it makes no sense to have that in a library. This will not work.

I think window lets you get away with that since it internally uses some other method to start up your application.

June 27, 2011

loladiro loladiro
Lab Rat
596 posts

There is a template/wizard for creating shared libraries within Qt Creator, have a look at that (File -> New File or Project -> Other Project -> C++ library)

June 27, 2011

Volker Volker
Ant Farmer
5428 posts

If you use qmake, just change the template from app (the default) to lib. See the qmake docs [developer.qt.nokia.com] and the qmake tutorial [developer.qt.nokia.com] for detailed information.

June 27, 2011

bovilexic bovilexic
Ant Farmer
76 posts

Hello and thank you for the responses!

I have had success with this by using Qt Creator to make a shared library project, and then using qmake to create an equivalent XCode project….XCode 3, that is.

I will have to try to see what happens in XCode 4, but I have heard that it’s not quite working?

Again, thanks!

June 27, 2011

Volker Volker
Ant Farmer
5428 posts

You can use Qt Creator on all platforms of course ;-)

June 28, 2011

bovilexic bovilexic
Ant Farmer
76 posts

Yes, I would like to have just gone with Qt Creator. Originally, I was unable to make the plugin link with the application. Turns out it was a 32-bit app, and Qt Creator insisted on making a 64-bit library. Figured that out sometime after I switched to XCode. Probably will go back and sort it out with Qt Creator, as it’s rather a bit nicer than XCode in some ways, with regard to Qt coding… :-)

Now that the 32- vs 64-bit issue is sorted out, I’ll probably go back and try Qt Creator again for this project…

June 28, 2011

Tobias Hunger Tobias Hunger
Robot Herder
3386 posts

Well, we can not really guess at the word size used in an application you want to link against the library you develop with Qt Creator:-)

 
  ‹‹ [SOLVED] Simulator problems      [Moved] Qt Creator saying no valid qt versions found please add a qt version when creating a new project ››

You must log in to post a reply. Not a member yet? Register here!