CMake, Xcode and Unix Makefile - xcode

I was using classic Unix Makefile generator until I found a webpage explaining CMake could produce a nice xcode project file using "cmake -G Xcode" so I ran that command in my source directory and I get a nice xcode project. This is great but I'm facing some drawback and was wondering if there are solutions to these :
now I can't build out of tree using unix makefiles (I use to run cmake ../myproj in a build dir and then make command) this doesn't work anymore (I suppose temp files in the project dir are responsible)
some of my headers are generated by cmake (myproj.h.in became myproj.h and is include in some tests for example) but I can't find those files in the xcode project I get.
Thanks in advance for any good advice on how I can combine xcode project and unix makefile to be able to use both.

I found the answer by myself (I mean asking the good person) :
Use 2 separate build directories for each type of build files. This will ensure each build directory has the correct build files and generated files for its use.
In the add_executable() command, in addition to the source files also include the headers and generated headers for those to show up in Xcode.

You can use xcodebuild to still have a proper Xcode project and build it from the command line/shell script/Makefile, e.g.
xcodebuild -project MyProject.xcodeproj -target MyTarget -configuration Release

Related

How to create a Code::Blocks project from the cmake file?

I would like to use the Code::Blocks IDE to build a C++ project based on a CMakeLists.txt file. Previously, I was using Qt Creator, where I could open a CMakeLists.txt file directly, which would create a project. I could then define the build commands, eg. cmake ../src followed by make -j8. This would run cmake on the CMakeLists.txt file, and then run make on the makefile.
How can I do something similar in Code::Blocks? If I try to create a new project, it gives me the option of creating an empty project, but then I am stuck as to where to go next...
It's similar but you have to specify the compiler, supposing that you have a folder project/build and project/src and your CMakeLists.txt is under /project. Then your commands should be something as the following :
$ cd build/
$ cmake .. -G"CodeBlocks - Unix Makefiles"
you will have your CodeBlocks project created on the /build.
Code::Blocks, has its own .cbp files which do the same things as Makefile automatically.
Therefore Code::blocks canot generate a Makefile, but you can build your project from an alredy existing Makefile, i've found a very good explaination of it on the code::blocks wiki. I do hope this will help you :)

cmake generate Xcode project from existing sources

This is what I have, when I started generation:
iMac:IXCSoftswitch alex$ /usr/bin/cmake -G Xcode .
-- CMAKE_SOURCE_DIR = /Users/alex/Desktop/ixc-v/IXCSoftswitch, CMAKE_BINARY_DIR = /Users/alex/Desktop/ixc-v/IXCSoftswitch
CMake Error at CMakeLists.txt:25 (MESSAGE):
Binary and source directory cannot be the same
-- Configuring incomplete, errors occurred!
How can I fix it? I'm new to CMake, samples appreciated.
CMake is used to produce out-of-source builds.
The idea here is that you don't mix the files created during compilation with the original source files. In practice, you usually run CMake from a new, empty build directory and give the path to the source directory as an argument.
cd IXCSoftswitch
mkdir build
cd build
cmake -G Xcode ..
All of the files generated by CMake (which could be a lot) will now go into the build subdirectory, while your source directory stays clean of build artifacts.
The concept of out-of-source builds may seem strange at first, but it is actually a very convenient way of working once you get used to it.
From the root of your project directory.
cmake -G Xcode -H. -B_build
This is similar to the answer above. However, you are manually managing the out of source build. -B sets your target build directory (I happen to prefer _build). I tried looking up -H to double check, but couldn't find it. If memory serves, it specifies where your CMakeLists.txt lives.
I keep this command in a .sh/.bat file (depending). This way, I can keep my scripts that build my project in the root where a new person can easily find them.

Create multiple executables in XCode

There are various versions of this question but I havnt been able to find what Im looking for.
I have a bunch of cpp files in the OpenCV samples directory. I would like to create a project in XCode that can compile and build all the executables. I opened a new project, added all the sample source files, included the correct header files and the right link paths and then got stuck because I dont know how to make the project build and link executables for each relevant cpp file. Im guessing Im doing this completely wrong so any help would be appreciated.
In Xcode 4, click on the Project icon in the navigator which should bring up a Projects and Targets section in the main pane. There should be an "Add Target" button at the bottom. Just add a new target and then specify which source files you want in the "Compile Sources" section in the "Build Phases" tab.
Xcode 3 is should be similar. Add a new target and tell it which source files to use.
HTH
You don't need XCode to build samples, OpenCV on Mac OS X makes use of cmake to generate makefiles which, in turn, are being consumed by gcc.
Please read:
http://opencv.willowgarage.com/wiki/Mac_OS_X_OpenCV_Port
So, assuming you have cmake installed, in OpenCV source directory run:
sudo cmake -D BUILD_EXAMPLES=ON -G "Unix Makefiles" .
You have Makefile in your 'samples' directory now and can compile samples:
cd samples
sudo make
The sample binaries will be put in the 'bin' directory. Voilà!

Is it possible to automatically generate Xcode projects?

Simple question. Are there any tools for generating Xcode projects from the command line? We use SCons to build our cross-platform application, but that doesn't support intrinsic Xcode project generation. We'd like to avoid creating the project manually, since this would involve maintaining multiple file lists.
Look at CMake. You can generate XCode projects from it automatically. I found a previous StackOverflow question about its usage here. To get it to generate an XCode project, you use it as such:
CMake -G xcode
You can use premake (http://industriousone.com/premake) to generate Xcode projects. It can also generate Visual Studio projects.
For the benefit of anyone who lands on this question, I’ve actually just pushed an Xcode project file generator for SCons up to Bitbucket.
I think that your question should be "Is there a way to generate an XCode project from a SCons one?". I suppose, by your asking and by reading the others, that the answer is 'no'.
SCons people should know it better. I think they will be happy if you contribute a SCons Xcode project generator.
In the meantime you may choose to switch to CMake or to create your XCode project by hand that, given a good source tree organization, may be the best pragmatic solution.
qmake in the Qt toolchain generates Xcode projects. You can at least download it and take a look at its source here (LGPL).
You can generate a XCode project using the python based build system called waf. You need to download and install waf with the xcode6 extension:
$ curl -o waf-1.9.7.tar.bz2 https://waf.io/waf-1.9.7.tar.bz2
$ tar xjvf waf-1.9.7.tar.bz2
$ cd waf-1.9.7
$ ./waf-light --tools=xcode6
That will create a waf executable which can build your project. You need to configure how to generate your XCode project inside a file called wscript that should reside in your project folder. The wscript file uses Python syntax. Here's an example of how you could configure your project:
def configure(conf):
# Use environment variables to set default project configuration
# settings
conf.env.FRAMEWORK_VERSION = '1.0'
conf.env.ARCHS = 'x86_64'
# This must be called at the end of configure()
conf.load('xcode6')
# This will build a XCode project with one target with type 'framework'
def build(bld):
bld.load('xcode6')
bld.framework(
includes='include',
# Specify source files.
# This will become the groups (folders) inside XCode.
# Pass a dictionary to group by name. Use a list to add everything in one
source_files={
'MyLibSource': bld.path.ant_glob('src/MyLib/*.cpp|*.m|*.mm'),
'Include': bld.path.ant_glob(incl=['include/MyLib/*.h', 'include'], dir=True)
},
# export_headers will put the files in the
# 'Header Build Phase' in Xcode - i.e tell XCode to ship them with your .framework
export_headers=bld.path.ant_glob(incl=['include/MyLib/*.h', 'include/MyLib/SupportLib'], dir=True),
target='MyLib',
install='~/Library/Frameworks'
)
There are a bunch of settings you can use to configure it for your project.
Then to actually generate the XCode project, cd into your project folder where the wscript is and run your waf executable like
$ ./waf configure xcode6
A promising alternative to CMake which can generate Xcode projects is xmake. I haven’t tried it yet, but it looks good from the documentation.
Install xmake, create a simple project file (xmake.lua):
target("test")
add_files("src/*.cpp")
Then you can either do a command-line build:
xmake
or create an Xcode project:
xmake project -k xcode
Note that currently xmake seems to invoke CMake to generate the Xcode project, although they say they plan to add native Xcode project generation at some point.
You could use Automator to generate them for you.
I checked and there is no prebuilt action.
Therefore you would have to record your actions with Automator to do this.

Building Qt Xcode Projects From the Command Line

I've been playing around with Qt for a few hours now. I found that qmake produces Xcode project files on Mac OS X instead of good ol' makefiles. I don't want to launch Xcode every time I want to build "Hello, world".
How do I make qmake generate regular makefiles or, if that's something that cannot be done on the Mac, how do I compile .xcodeproj files from the command line?
I tried xcodebuild -project myProject -alltargets. I get a lot of output followed by Abort trap.
$ man xcodebuild
So a typical command might be something like:
$ xcodebuild -project myProject.xcodeproj -alltargets
The open-source Qt binary installers for OS X from Trolltech default to creating .xcodeproj files when you run qmake. I don't use XCode for editing so it is a pain to open it to compile the project.
To compile your projects from Terminal.app, just set an environment variable of QMAKESPEC to macx-g++
If you want to just compile a certain project from the terminal, go into that directory and run
qmake -spec macx-g++
When you run qmake, this will create a Makefile which you can use by running make.
Try the following way. It has to work.
xcodebuild -project myProject.xcodeproj -alltargets
I have used this method in most of my projects.
Looking at this part of your back trace:
#
2008-12-18 20:40:52.333 xcodebuild[1070:613] [MT] ASSERTION FAILURE in /SourceCache/DevToolsBase/DevToolsBase-921/pbxcore/FileTypes/PBXCFBundleWrapperFileType.m:174
#
Details: path should be a non-empty string, but it's an empty string
#
Object:
#
Method: -subpathForWrapperPart:ofPath:withExtraFileProperties:
#
Thread: {name = (null), num = 1}
This implies that something, maybe one of your configuration variables, is blank when it needs to refer to a file. What I'm wondering is if maybe you have an extra target in your project that doesn't work, so that building with -alltargets is what's causing your problem.
I tested xcodebuild without any arguments on one of my projects just now - it did a default build of my project without errors. What happens if you try it without arguments?

Resources