0

I have the source code for a video decoder which is written in C. The code was successfully compiled and executed on MAC terminal (which uses GCC compiler). Now I'm trying to create an application on Xcode with the same source code. Only the GUI for the application is written in Objective-C.

When I tried to execute on Xcode (which uses LLVM compiler), I'm getting a lot of errors in the C code. The only other compiler option that Xcode provides is LLVM GCC 4.2. I compiled using that and found that it is not able to recognize the code written in Objective-C.

Is there any compiler that can be used for both Objective-C and C? Can GCC 4.2 compiler be used for compiling Objective-C code?

How to tell Xcode to compile using GCC 4.2 compiler?

Kindly help. Thanks in advance!

Ereka
  • 103
  • 1
  • 10
  • What kinds of errors are you getting in the C code? – Bob Murphy Mar 04 '13 at 08:34
  • 1
    Yes, this question contains no helpful information! Please be more specific.. – nielsbot Mar 04 '13 at 08:35
  • I know you said it's C code, but remember that if you are also mixing Objective-C with C++, you need to rename the files so they use the .mm extension. – Ricard Pérez del Campo Mar 04 '13 at 08:41
  • For eg.: typedef unsigned char BOOL; gives the error - Redefinition of typedef BOOL is invalid in C. Since I'm using an existing C code (which has around 20 source files) I'm not able to replace all the places where BOOL is used with "unsigned char" manually. I wasn't getting this error with GCC compiler. – Ereka Mar 04 '13 at 08:44
  • @Ricard - Oh alright, but I'm not using any C++ code. – Ereka Mar 04 '13 at 08:46
  • If you include any .h files from a .m file, those files will be compiled in ObjC mode. If your existing C sources aren't ObjC compatible (they define BOOL differently for example) you can compile them into a static library and link that. – nielsbot Mar 04 '13 at 08:52
  • Why is there a typedef for BOOL? – nielsbot Mar 04 '13 at 08:53
  • Oh ok.. Typedef for BOOL is used to follow certain coding standards that were mentioned for us to follow. – Ereka Mar 04 '13 at 09:03
  • Have a look at my updated answer for a possible solution to your redefinition problem – Sebastian Mar 04 '13 at 09:20

2 Answers2

2

Both LLVM and GCC can be used to compile C, C++ and Objective-C code.

My guess is that some compiler flags are set incorrectly.

Jasper Blues
  • 28,258
  • 22
  • 102
  • 185
  • Compilation is happening but LLVM throws errors that were not there when compiled using GCC. Is there any way to specify Xcode to use only GCC compiler? – Ereka Mar 04 '13 at 08:50
  • There is a way. You can set a build flag "CC" pointing to the compiler. – Jasper Blues Mar 04 '13 at 08:58
  • I thought it was just a matter of setting the CC flag (did this b4 with custom clang), but it looks a little more complex: http://hamelot.co.uk/programming/add-custom-compiler-to-xcode/ – Jasper Blues Mar 04 '13 at 09:25
  • That looks pretty complex! But I'll try and see if it works. Thanks for the effort! – Ereka Mar 04 '13 at 09:32
0

Objective-C is a strict superset of ANSI C, so any compiler that can compile Objective-C code can also compile (ANSI) C code. If your library does not use standard C, then you might run into problems.

The same is true for Objective-C++, which is a strict superset of C++.

If, like in your case, you have a conflicting typedef in your header file, you can use conditional compilation to hide the typedef when compiling Objective-C code:

#ifndef __OBJC__
typedef unsigned char BOOL;
#endif
Sebastian
  • 7,670
  • 5
  • 38
  • 50