0

I feel like an idiot with all of this, but I am trying to implement some C code that I have into my project, but I keep having unexplained issues.

From my past experience, I could create a .h and a .c file, implement the functions in the .c and declare them in the .h. Then I could import the .h into another file to have visible access to the functions.

I've included the .c in the compile sources list and added it as a target, but I am being told that it can't find the functions. Okay. Then I tried using a .h and .m pair, but now I am getting a SIGABRT in my main() without any error description.

Here is an example (not sample) of my .m code:

static NSArray *_myArray

NSArray *myArray(){return (_myArray) ? : (_myArray = [NSArray arrayWithObjects:@1, @2, @3, nil]);}

I've already #imported all necessary framework classes, as well.

So, at the end of it all, I just want to know the steps for creating the necessary files and implementing my C code. I'm hoping to get that answer, rather than a work around, as I'm sure that it's possible to include C files into an XCode project.

9
  • That doesn't look like legal code in any way, does it even compile? Commented Jun 13, 2013 at 17:19
  • @CarlNorum the poster mentions above that code snippet that this is from his ".m" (read: Objective C) code. Commented Jun 13, 2013 at 17:20
  • @mah, it's not valid Objective-C code. That's my point. I was trying to suggest the problem by asking if it compiles (it won't). Commented Jun 13, 2013 at 17:21
  • @CarlNorum I had this working when I was using it as a basic header and only implementing it into one class. Commented Jun 13, 2013 at 17:21
  • I don't think so. Your function definition, if that's what it is, doesn't have a parameter list, so it can't be compiled. Whatever you thought you had working wasn't this code. Commented Jun 13, 2013 at 17:22

1 Answer 1

2

Xcode is happy to let you use C sources in your project; you only need to provide them in your project(of course) and also make sure they're specified for your target(not always intuitive).

Click your project name in the navigator so you can access the Build Rules for your project. From there, expand the "Compile Sources" section, and make certain your C source is listed.

If it is, you'll need to examine the full compiler output to understand what is happening.

Sign up to request clarification or add additional context in comments.

7 Comments

So, if I wanted, I should also be able to build a header class to clearly state to my project that those functions DO exist? If so, how would I link them, if at all?
Headers do not get linked, they get inspected when some other file is being compiled. If your sources are all in the list of files to be compiled, they will also be linked into the final output.
Okay. Thank you! I guess I'll just try to remake my files?
Just ensure they're in your "Compile Sources" build rule :) Sometimes when you add files to an Xcode project, Xcode doesn't add them to the target for compilation, leading to the problem you're seeing.
Because I forgot, can I include any Objective-C code in a C file? If so, what flags do I need to include?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.