Create OSx .app from c++ application - xcode

I've created an OpenGL application using the Command Line Tool project template.
Now the output project is obviously a unix executable.
My question is How can I convert this executable into a bundle .app?
Have I to add another target?

If you created a command line tool, it is just that, a command line tool. I don't know of any way to bundle it up into an application with ease.
If I were you, and you are going to start writing Mac OpenGL application, get set up with SFML. It greatly simplifies window creation, is cross platform and has a great community. Also, getting to the resource path in a mac .app bundle is a bit tricky and the templates they provide allow you to access any file with a call:
loadFile(resourcePath() + "filename.txt");
rather than having to keep track of everything. Plus, the whole resource path code is written in objective C which can be a pain for some C++ programmers.
The whole framework is pretty robust but maintains a simplistic style and its own namespace. Use what you want, don't use what you don't need. The latest version (still beta) 2.0, comes with Xcode templates. Just create a template and in build settings for your project, scroll to the bottom. You'll see SFML_LINK_DYLIBS_SUFFIX and it has a value of '-d'. Remove the '-d' and it should compile.
Then if you are distributing, add a copy files build phase and link the .dylib(s) that you used.

Related

How to add a kit to Qt Creator from the command line, or other programmatic manner? [duplicate]

SO!
Let's say I have a number of settings (GCC compiler 9.3.0 built from source, as the distribution I have to use has a very old one, along with environment setup) for a new Kit in QtCreator.
I have managed to setup an environment for compilation and execution of compiled binaries, and made a script to make it work (like qmake -nocache -recursive/make/sudo make install, direct execution of g++, and other stuff).
One thing that script can't do at the moment, is that it cannot create a kit for QtCreator with new compilers and environment being set as required, so after running a script, its user has to go through setting it up himself through GUI, which is bad, because this can cause misconfiguration.
This thing I'm trying to create is going to be used by around ~200 people in my company, so leaving readme.txt with instructions just doesn't go well enough for me - I don't want running around fixing missing "{" and "}" in Environment description in created Kits, and other stuff.
Are there ways to create Kits for QtCreator automatically from command line? Maybe, there's some files to edit?
I've looked into this one a few years back (I wanted to do something similar for registering Buildroot toolchains automatically in QtCreator), and I was unable to find an off the shelf solution. So i think there are 2 ways to implement this:
a) Implementing a command line utility the manipulate the ~/.config/QtProject/qtcreator/{toolchains,profiles}.xml files. Maybe by (re)using the existing C++ implementation within QtCreator, or just re-implement it ie. in Python. Back than I didn't start to work on this as there was no real business need.
b) Switching to qbs, as qbs has support for setting up toolchains from the command line ( see: https://doc.qt.io/qbs/cli-setup-toolchains.html)
If you decide to go with solution a), please let me know and maybe we can partner up to implement it.
Check out the command line sdktool bundled with QtCreator:
The SDK tool can be used to set up Qt versions, tool chains, devices
and kits in Qt Creator.
There still is a lot of knowledge about Qt Creator internals required
to use this tool!
I haven't tried it yet, but I did find the executable under Tools/QtCreator/libexec/qtcreator subdirectory of the Qt Creator installation directory. ./sdktool --help works for me under Linux.

How to use 'SQLite' (the wrapper on github) in a Command Line Tool?

I'm successfully using the great Swift wrapper for sqlite from https://github.com/stephencelis/SQLite.swift in a Cocoa application.
But when I try to use the wrapper in a Command Line Tool project and follow the same detailed installation steps I get the following error:
Check dependencies
Unable to run command 'PBXCp SQLite.framework' - this target might
include its own product.
I checked the dependencies, but couldn't figure out how to solve this.
You can't link dynamic framework (a .framework) with your app in a Command Line Tool project. The reason is simple — a command line tool target builds a single binary file. This is unlike a regular Cocoa application, where the .app "file" is actually a folder containing .frameworks and other stuff inside.
So basically you'd have to build a static library instead (one that links with your app's binary during compilation) — except that as of Xcode 6.1 it's not possible yet with Swift.
So the only thing you can do — AFAIK — is add the SQLite.swift's source code directly into your own app target (so it compiles together). It's ugly, but works.

How to include Qt libraries with CMake inside a .app project on OSX?

I've created a Qt project using CMake as build system. I would like to include in the deployment phase, the necessary Qt frameworks
Qt3Support.framework/ QtNetwork.framework/ QtXml.framework/
QtCore.framework/ QtOpenGL.framework/
QtGui.framework/ QtSql.framework/
in order to be ready to distribute my program as standalone dmg.
This is usually done with macdeployqt command but it gives me a lot of errors like this:
ERROR: "strip: for architecture x86_64 object:
/Users/rs/build/myapp.app/Contents/Frameworks/QtXmlPatterns.framework/Versions/4/QtXmlPatterns
malformed object (unknown load command 5)
so I want to be able to do it directly in the deployment phase, is it possible?
I was using DeployQt4 and BundleUtilities for some time until I ran into a problem with it which wasn't easy to fix according to one of the authors.
I ended up writing a ruby script that does what BundleUtilities does barring that problem. My script has a limitation currently comparing to the BundleUtilities - it doesn't handle bundles with multiple executables. Writing the same functionality in cmake is beyond my degree of masochism (I managed to understand what BundleUtilities does and how but was appalled by the contraptions they had to use to organize data in memory).
The script itself is available here: https://github.com/artm/vision-ui-skeleton/blob/master/ruby/fixup/fixup.rb
It is used from cmake at the bottom of: https://github.com/artm/vision-ui-skeleton/blob/master/cmake/QArtmRelease.cmake
It works with ruby that comes with OSX 10.6.x and probably higher.

Best way to install a custom cocoa framework

I have a custom framework that, following the advice in Apple's Framework Programming Guide >> Installing your framework I install in /Library/Frameworks. I do this by adding a Run Script build phase with the following script:
cp -R build/Debug/MyFramework.framework /Library/Frameworks
In my projects I then link against /Library/Frameworks/MyFramework and import it in my classes like so:
#import <MyFramework/MyFramework.h>
This works very well, except that I always see the following message in my debugger console:
Loading program into debugger…
sharedlibrary apply-load-rules all
warning: Unable to read symbols for "/Users/elisevanlooij/Library/Frameworks/MyFramework.framework/Versions/A/MyFramework" (file not found).
warning: Unable to read symbols from "MyFramework" (not yet mapped into memory).
Program loaded.
Apparently, the compiler first looks in /Users/elisevanlooij/Library/Frameworks, can't find MyFramework, then looks in /Library/Frameworks, does find MyFramework and continues on its merry way. So far this has been more of an annoyance than a real problem, but when runnning unit tests, gdb stops on the (file not found) and refuses to continue. I have solved the problem by adding an extra line to the Run Script Phase
cp -R build/Debug/MyFramework.framework ~/Library/Frameworks
but it feels like sello-taping something that shouldn't be broken in the first place. How can I fix this?
In the past months, I've learned a lot more about frameworks, so I'm rewriting this answer. Please note that I'm talking about installing a framework as part of the development workflow.
The preferred location for installing a public framework (i.e. a framework that will be used by more than one of your apps or bundles) is /Library/Frameworks[link text] because "frameworks in this location are discovered automatically by the compiler at compile time and the dynamic linker at runtime."[Framework Programming Guide]. The most elegant way to do this is in the Deployment section of the Build settings.
As you work on your framework, there are times when you do want to update the framework when you do a build, and times when you don't. For that reason, I change the Deployment settings only in the Release Configuration. So:
Double-click on the framework target to bring up the Target info window and switch to the Build tab.
Select Release in the Configuration selectbox.
Scroll down to the Deployment section and enter the following values:
Deployment Location = YES (click the checkbox)
Installation Build Products Location = /
Installation Directory = /Library/Frameworks
The Installation Build Products Location serves as the root of the installation. Its default value is some /tmp directory: if you don't change it to the system root, you'll never see your installed framework since it's hiding in the /tmp.
Now you can work on your framework as you like in the Debug configuration without upsetting your other projects and when you are ready to publish all you need to do is switch to Release and do a Build.
Xcode 4 Warning
Since switching to Xcode 4, I've experienced a number of problems with my custom framework. Mostly, they are linking warnings in GDB that do not really interfere with the usefulness of the framework, except when running the built-in unit-test. I have submitted a technical support ticket to Apple a week ago, and they are still looking into it. When I get a working solution I will update this answer since the question has proven quite popular (1 kViews and counting).
There's not much reason to put a framework into Library/Frameworks, and it's a lot of work: You'd need to either do it for the user in an Installer package, which is a tremendous hassle to create and maintain, or have installation code in your app (which could only install to ~/L/F, unless you expend the time and effort necessary to make your app capable of installing to /L/F with root powers).
Much more common is what Apple calls a “private framework”. You'll bundle this into your application bundle.
Even frameworks intended for general use by any applications (e.g., Sparkle, Growl) are, in practice, built to be used as private frameworks, simply because the “right” way of installing a single copy of the framework to Library/Frameworks is such a hassle.
The conventional way to do this is to have your framework project and its clients share a common build directory. Xcode will search for framework headers and link against framework binaries in the build folder first, before any other location. So an app project that compiles and links against the header will pick up the most-recently-built one, rather than whatever's installed.
You can then remove the cp -r and instead use the Install Location build setting to place your build product in the final location, using xcodebuild install DSTROOT=/ at the command line. But you'll only need to do this when you're finished, not every time you rebuild the framework.
Naturally, when you distribute your framework it should be installed in /Library/Frameworks; however it seems odd to me that you're doing that with the test/debug versions of your framework.
My first instinct would be to install test versions under ~/Library, as it just makes setting up your test and debug environment that much simpler. If possible, I would expect the debug/test framework to be located in the build tree of the version I'm testing, in which case it's installed as a Private Framework for testing purposes. That would make your life much simpler when it comes time to deal with multiple versions of your framework.
Ultimately, it doesn't matter where the framework is located as long as your application or test suite loads the correct version. Choose the location that makes testing/debugging/development easiest.

Setting up a large Xcode project

I have a large exiting C++ project involving:
4 applications
50+ libraries
20+ third party libraries
It all builds fine on Windows using VS8, Linux using QMake (project uses Qt a lot). I also build it on OS X using QMake but I was wanting to setup an Xcode project to handle it in an IDE. I'm struggling to setup proper configuration to easily define dependencies, both to internal libraries and to the third party. I can do property sheets and .pri files in my (disturbed) sleep, but would appreciate some advice on building such large projects in Xcode.
I've been experiencing with Xcode configuration files and #including one from another but it does not seem to work as I would expect, especially when defining standard locations for header files etc.
Is there some good book describing the process of setting up Xcode (remember it's C++, I'm not wanting to learn ObjC at this time)?
Or maybe a good open source project I could learn from?
Thanks!
Step in to Xcode may be the book you're looking for. It's got a whole section devoted to using AppleScript to automate configuration includes. I've been going through the book myself on O'Reilly Safari as I've found myself in a situation similar to yourself!

Resources