creating an XCode project with existing code
Asked Answered
H

4

33

I'm in the process of porting a Linux application to Mac. I have different files with the source code that can get compiled and linked using the standard Makefile.

I'm going to be porting that code to Mac and continue writing code in C (sorry, no obj-c). Is there a way to create a project on XCode, add the existing code so I can use XCode and the IDE, compile and debug the code and generate Mac Makefiles?

Thanks for the help

Heddi answered 9/5, 2011 at 20:22 Comment(1)
Are you using carbon to run the app on the mac? or is it a command line tool? Also Xcode should be able to handle your make file. However I do not think it can actually generate makefilesIreland
M
39

New Project -> Other -> External Build System

(in new project)

Expand "Targets"

select the target the template created

press return

edit the target settings:

  • by default, it uses /usr/bin/make for invocation. if you want to use some other build system, then you'll have more to configure.
  • for a Makefile, you will need to set the 'Directory' field of the build settings window to the directory which contains the appropriate makefile to build.

note that you'll lose some integration when using a makefile.

you can regain some of that by adding the sources to the project (drag and drop), and not associating them with a target.

to improve navigation and code completion, you may want to create a second (dummy) target (such as a static library) so the ide parses your programs. you would then add the sources to the dummy static library, and set the makefile as a 'dependency' of the static library (so it gets built, and so it gets built first).

Medicate answered 9/5, 2011 at 20:31 Comment(5)
Thank you. I assume that if I point to the directory with my source it'll read the makefile from there? Also, how do i add the source files to the project so that are read and understood by XCode?Heddi
Thanks. I tried dragging and dropping the files into the new target created in the project (static BSD lib) but xcode will not accept it. How can I add them? I don't see a "source" in the project tree.Heddi
oh - you drop the files into the project (in Groups and Files if on Xc3, or the Project Navigator in Xc4), not into the target itself. then xcode asks if you want to add them to a target. to use the menus: 1) select the project in Groups and Files (Xc3) or the Project Navigator (Xc4) 2) Xc3: Project->Add to Project... Xc4: File->Add Files...Medicate
So, I have a Makefile target, then a BSD static lib target. I added the source code files to the project (called TestProject). I still can't do anything with those files, I mean, I might as well open them in text edit and it would be the same. Am I doing something wrong? How can I tie those source files to the static lib target? What's the difference between project, target, workspace... I'm using XCode 4Heddi
Oh, I got it. Great, so now if I hit "Build" it'll run make on the Makefile, correct?Heddi
M
2

The Apple Developer docs have a section on porting makefile based projects into XCode.

"Porting UNIX/Linux Applications to OS X"

This subsection is most relevant: "Building makefile projects with XCode"

https://developer.apple.com/library/content/documentation/Porting/Conceptual/PortingUnix/preparing/preparing.html#//apple_ref/doc/uid/TP40002849-BBCJABGC

Michell answered 24/5, 2017 at 10:25 Comment(1)
The 3T dollar company with an enviable product excellence and usability record either doesn't know or can't afford to create proper documentation. The posted web page explicitly states the instructions are for the latest version of Xcode. I'm running 13.2.1 and there's no "Project" menu, nor is there a "New Target" menu. Turns out the "Project" menu disappeared at least 10 years ago in version 4! The documentation hasn't been updated since. This is an unacceptable ball drop you come to expect from Microsoft!Enid
J
0

You can build using your existing makefiles and create a wrapper project with a custom target with a 'Run Script' build phase that just calls down to your makefile. This means that you'll also be able to use the debugger, but you probably won't get the full benefit of the editor with autocompletion etc.

Juggler answered 9/5, 2011 at 20:30 Comment(0)
N
-4

To import C code into Xcode:

  1. start xcode
  2. new project
  3. pick something like Standard Tool. Should read c command line tool.
  4. drag you file or files onto main project window. This is the first window that comes up when you create a new project. My project name is xaBitHoist. I dropped the files on xaBitHoist > src.

just build. Seems to default to main.c

to change start program or add argues Project > New Project Executable.

the online help for xcode is good.

Good luck.

Robert

Nonunion answered 1/5, 2012 at 21:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.