How to create a command-line program from an existing Objective-C program, using shared source code?

I'm trying to port a working OSX app (“OriginalApp”) from a GUI interface to a command-line interface, using the same objective-C source code within the same Xcode project, so I can build it either way, while maintaining only one copy of each shared source code file.


Here is what I've done so far:

  1. within the Project in Xcode (7.1), File->New->Target, select Command Line Tool, [Next], enter new command-line name for target (cli_app) This creates a new folder called "cli_app", at the same level as “OriginalApp"
  2. rename the generated main.m to main.mm (will use some C++ functionality)
  3. add a couple new objective-C classes to adapt the CLI interface to the existing code (these are added in the new cli_app folder)
  4. edit contents of main.mm to use new adapter classes
  5. add existing files from “OriginalApp” (right-click on new target's folder in Project Navigator pane, click "add files to ‘OriginalApp' " - new subfolder “OriginalApp” appears in Project Navigator under cli_app.


I am hitting a brick wall here. Whatever I do, I'm getting compiler errors: the compiler is not recognizing some of built-in classes when called by the original source code from the new cli_app target.

Errors include

  • "Unknown type name 'NSBitmapImageRep' "
  • "Use of undeclared identifier 'NSBitmapImageRep' "


The original app still builds fine. I have tried setting the include paths on the new target to match the old one (no help), and making sure the Build phases match (especially the link with binary libraries, but this is not a linker error, so I don't think that's the issue.)


I figure this is a Build settings issue on the new cli_app target, and probably something simple. Any ideas?

If I had to guess I’d say that you’re source files aren’t

#import
ing all the frameworks they depend on and, in your app target, that problem is being masked by the prefix header (see the Prefix Header build setting,
GCC_PREFIX_HEADER
).

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

@eskimo: indeed - that was the problem. The original target used a precompiled header that held the imports; the new target did not. Thanks -

indeed - that was the problem.

For my sample projects I disable prefix headers to avoid problems like this. I realise that can radically increase compile-time on a real app. My recommendation in that case is that you periodically disable the prefix file to flush out errors like this. Or not have the prefix file set in your Release build configuration, so you see these errors when you build for release (which is generally slow anyway).

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
How to create a command-line program from an existing Objective-C program, using shared source code?
 
 
Q