Creating Xcode projects for wxWidgets applications
|This article applies to the following versions|
|Mac OS X 10.7.3||2.9.x Cocoa||4.3.2|
|Status: Up to date|
|Note: This article applies to Xcode 4 with wxOSX/Cocoa 2.9.x. For older versions of OSX/Xcode, see Creating Xcode 3 projects for wxWidgets applications|
Note that some parts of the previous Xcode 3 article have not yet been ported to the Xcode 4 article. This article should nonetheless be helpful to get you started.
Creating the project and its files
- Start Xcode.
- Choose menu item File --> New --> Project --> OSX --> Cocoa Application, then click Next
- Name the project, fill other required fields, then continue and create the project.
- Remove from the project all files the wizard created, leaving only the PCH and plist files.
- Use menu File --> New --> File then select C++ file under MacOSX. Call it (for instance) main.cpp.
- Replace the contents of main.cpp with that of a wxWidgets sample. Some samples may require images or other resources; in order to quickly build a template you can use the following minimal code :
If you chose a wxWidgets sample more complex than the dummy code posted above, it may also be necessary to copy the sample.xpm file from the samples directory to the folder above your project folder (you will know if Xcode gives the error "../sample.xpm: No such file or directory"). This should not be necessary for your own projects.
Configure build options
- Highlight your project in the tree that is on the left.
- Select the target corresponding to your project.
- Select the build settings tab.
- In the architectures subsection, edit setting architectures so it matches your wxWidgets build (most importantly, if your wxWidgets build is not universal, you will want to change this setting to not build universal binaries)
- Now open a terminal window, execute one of these sets of commands :
# if you did make install (make sure you get the right wx-config, you may need to adjust PATH) wx-config --cxxflags wx-config --libs # if you left wxWidgets in-place cd /path/to/wxWidgets/build-release ./wx-config --cxxflags ./wx-config --libs
then copy the output of the two last commands.
Warning : if you built wxWidgets as a universal binary, it will output flags like -arch ppc and -arch i386. Drop these flags because Xcode needs to manage Universal Binary stuff itself, passing flags to gcc without Xcode knowing will mix it up and result in linking errors.
Warning : make sure you don't copy and paste linebreaks, this may cause obscure errors
|Note: The commands given above will link against the core wx libraries only. If you build non-monolithic (the default) and wish to use more components than the default ones (like OpenGL, HTML, wxAUI, etc.) adapt the command with more library names. Example :
./wx-config --libs core,base,gl
If you are not yet sure which libraries you will need, don't worry, you can start with the core ones and update the build settings later as needed by repeating this step.
- In Other C Flags (use the search bar) add the output of the wx-config --cxxflags command.
- In Other Linking Flags (use the search bar) add the output of the wx-config --libs command.
(Warning: If you copy and paste the result of "wx-config --libs" into a single row at Xcode's config, Xcode will correctly fix and populate the rest of the rows, but be careful that sometimes a blank line is also added. This could lead to "(blank): No such file or directory")
Warning : do NOT copy the example flags in the screenshot. You need to run the command on the terminal to know the right flags for you.
- It is also recommended to configure the compiler to be GCC (or "LLVM GCC") and not Clang (sometimes also called "Apple LLVM Compiler"), since wxWidgets itself by default is built using GCC
- Close the settings window.
- In the toolbar, at the top-left, you will see the scheme selector. If 64-bits is selected but your wx build is 32 bits, change the selected scheme.
- If Xcode can't find a header *.h file, go back and check your build settings. There's probably some flags from wx-config missing or misplaced.
- If you get
ld: warning: in /Developer/SDKs/MacOSX10.6.sdk/usr/lib/libwx_macud-2.8.dylib, missing required architecture x86_64 in file
or similar, followed by lots of missing symbol errors, this means that your wxWidgets build is 32-bits (the default), but your Xcode project tries to build 64-bits binaries (the default on OS X 10.6). You need to edit your target settings, like mentionned in the instructions above, to build 32-bits binaries
- You can also check the full build log from Xcode, and compare it with those that Sample Makefiles output.
- If you ever changed the parameters to ./configure, you must always remove the prior build directory first and start from scratch in a new directory. Otherwise you can get a mixture of old and new files in the directory which will confuse the compiler, confuse you, and lead to strange errors and warnings.
- Make sure you don't enable monolithic build (--enable-monolithic) if you are generating a static library (--disable-shared). wxWidgets will probably build and install fine, until you try to link against the libraries with your code or one of the samples. You probably have monolithic and static enabled together (while they are mutually exclusive for some reason) if you see something like this:
Undefined symbols for architecture i386: "wxTopLevelWindowMac::DoCentre(int)", referenced from: vtable for MyFramein minimal_minimal.o ld: symbol(s) not found for architecture i386 collect2: ld returned 1 exit status
- If your program crashes when compiled from Xcode, but does not crash when compiled from the "samples" makefile, then it means that your compiler settings in Xcode do not match the compiler settings you used when you compiled the wxWidgets library (like defining __WXDEBUG__ and linking against release libraries, or vice-versa)
- If you want to debug beyond your code and into the wxWidgets code, you need to do three things:
- Build a debug version of the wxWidgets library (see articles about building wxWidgets)
- Add the __WXDEBUG__ compile flag to your project.
- Add the debug library to your Xcode project, clean and rebuild.
When using wx as a static library you may also need to set 'Build Options -> Debug Information Format' to 'DWARF with dSYM File' (no idea why but otherwise I only got debug symbols for my own code).