2

I installed GNUStep and gobjc in Ubuntu 16.04. I can compile Objective-C code like this:

gcc codefile.m `gnustep-config --objc-flags` -lobjc -lgnustep-base -o codefile

But I want to compile Objective-C++ code in GCC. How can I do it?

1 Answer 1

6

Documentation of objc/objc++ dialects in GCC of your version of gcc / gobjc don't list specific option to select the dialect variant.

Just use standard file extension ".mm" or ".M" (additionally you can replace gcc with g++ to automatically add C++ libraries into linking stage; both variants untested):

gcc codefile.mm `gnustep-config --objc-flags` -lobjc -lgnustep-base -o codefile
g++ codefile.mm `gnustep-config --objc-flags` -lobjc -lgnustep-base -o codefile

And ObjC++ is "simply source code that mixes Objective-C classes and C++ classes"

Gcc will select mode based on file extension, there is huge table of file extensions and corresponding language modes with Objective-C for .m and Objective-C++ for .mm/.M: https://github.com/gcc-mirror/gcc/blob/gcc-4_7_4-release/gcc/gcc.c#L913

static const struct compiler default_compilers[] =
{
  /* Add lists of suffixes of known languages here.  If those languages
     were not present when we built the driver, we will hit these copies
     and be given a more meaningful error than "file not used since
     linking is not done".  */
  {".m",  "#Objective-C", 0, 0, 0}, {".mi",  "#Objective-C", 0, 0, 0},
  {".mm", "#Objective-C++", 0, 0, 0}, {".M", "#Objective-C++", 0, 0, 0},
  {".mii", "#Objective-C++", 0, 0, 0},
 . . .
  /* Next come the entries for C.  */
  {".c", "@c", 0, 0, 1},

There is also -x option of g++ with allowed argument of "objective-c++" to manually select language dialect (gcc -x objective-c++ ... or g++ -x objective-c++): https://github.com/gcc-mirror/gcc/blob/1cb6c2eb3b8361d850be8e8270c597270a1a7967/gcc/cp/g%2B%2Bspec.c#L167

case OPT_x:
 . . . 
      && (strcmp (arg, "c++") == 0
      || strcmp (arg, "c++-cpp-output") == 0
      || strcmp (arg, "objective-c++") == 0

It is documented at https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html

 -x language

Specify explicitly the language for the following input files (rather than letting the compiler choose a default based on the file name suffix). This option applies to all following input files until the next -x option. Possible values for language are:

      c  . . .  c++ . . .
      objective-c  objective-c-header  objective-c-cpp-output
      objective-c++ objective-c++-header objective-c++-cpp-output
Sign up to request clarification or add additional context in comments.

3 Comments

My experience with Objective-C++ is limited to macOS, but the file needs to be named with extension .mm, otherwise it won't work. I think this is the correct answer.
@osgx Thanks for your detail information. I could compile Objective-C++ with g++ like this: g++ codefile.mm `gnustep-config --objc-flags` -lobjc -lgnustep-base -o codefile.
@H.Al-Amri With clang on macOS you can use the -ObjC++ flag when compiling. This will override the filename based mode detection.

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.