Getting XCode to include, compile and link existin

2020-08-11 04:47发布

问题:

I'm trying to 'import' my existing C++ codebase into my XCode iOS project.

Problem is, xCode won't compile (and therefore not link) my code. This is what I've done so far:

  • File > Add files to "Project". I don't tick 'Copy files' since it's a shared codebase for multiple platforms and multiple copies of source code is not an option.
  • I've added appropriate path to the "Header Search Path" project settings. Including header files works, I can #include "stuff.h" from my Objective-C++ (.mm) file.

However, I can't find any way of actually telling XCode to compile the .cpp files?

A few points:

  • I'm not looking for a way to pre compile it into a library and linking that way
  • The project is a standard iOS Objective-C project. The C++ code is called from it (that's not a problem)

Update

I've added the codebase directory to the target's "Compile Sources". I actually have to click "Add other..." in the dialog that pops up, since I can't select the directory reference to my codebase.

When I add it, XCode asks me if I want to "Create external build system project". I leave it ticked and the two fields with the default values "/usr/bin/make" and "$(ACTION)". However, my codebase doesn't get compiled during the build. Do I have to write my own Makefile ?

If I don't select "Create external build system project" then a directory reference is created under the target, but not compiled during the build and a warning is emitted:

warning: no rule to process file '$(PROJECT_DIR)/../codebase'
of type folder for architecture i386

回答1:

Even if you don't check Copy Files you have to check Add to Targets when adding the files to the project.

If you forgot to do that, you can select the target, go to the Build Phases tab and add the files to Compile Sources.



回答2:

If you go to your Targets -> Build Phases -> Compile Sources -> "+" -> "Add Others" -> (select your external source folder) -> Select: Create Group for any added folders -> Unchecked: Copy items into destination.

Now your folder will be in XCode, mimic the structure. If you add source files to that folder in XCode the source files will be added to your external source directory just fine. So the ease of using multiple IDEs and having to manage source linkage is solved.

The catch. You still have to manually add files into the "Compile Sources" area. This is rather simple. Just click and drag your source files from the group directory in XCode right into the Compile Sources window. So there is an extra step when you add a source file now. Forgetting to add them to the Compile Sources window will yield unresolved symbols compile errors.