Mixing Objective-C and C++ in Xcode

  • I am new to Xcode and Mac development.

    I have a program in Objective-C that compiled up to the point where I had
    to integrate pre-written C++ code.

    I am trying to compile a program that mixes C++ code and Objective-C. The
    errors generated when I compile make it look as though Xcode does not
    understand what a "class" is. Errors like this

    error: parse error before "WebMOReader"

    where WebMOReader is a class definition in a .h file like this

    #include <fstream.h>
    #include "MolecularIsosurface.h"

    class WebMOReader
    {
    private:
    ...

    and so forth. It doesn't matter whether I have the includes at the top of
    the file or not. It just doesn't like "class Blah" for whatever reason.

    I have included all the header files in a group that is a part of the
    project, along with the corresponding source code (with a .cpp suffix).

    Is there something special I have to do to make Xcode understand C++?

    Is the suffix of the header file fooling it into thinking it is a Obj-C
    header? What should I name it if so?

    Thanks.

    Albion

    Albion E. Baucom
    http://rna.ucsc.edu/albion
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • On 1. Jul 2004, at 18:37, Albion Baucom wrote:

    > I have included all the header files in a group that is a part of the
    > project, along with the corresponding source code (with a .cpp suffix).

    Use .mm instead:
    http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/
    3objc_language_overview/chapter_3_section_10.html
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • On your implementation file webMOReader.m  to webMOReader.mm  mm is the
    suffix for objective-c++ code.  If your header is included from that
    file then it should se it as a obj=c
    ++ header i think.  I have several integrated c++ and obj-c files in my
    program. I hope that helps.

    On Thursday, July 1, 2004, at 11:37 AM, Albion Baucom wrote:

    > I am new to Xcode and Mac development.
    >
    > I have a program in Objective-C that compiled up to the point where I
    > had
    > to integrate pre-written C++ code.
    >
    > I am trying to compile a program that mixes C++ code and Objective-C.
    > The
    > errors generated when I compile make it look as though Xcode does not
    > understand what a "class" is. Errors like this
    >
    > error: parse error before "WebMOReader"
    >
    > where WebMOReader is a class definition in a .h file like this
    >
    > #include <fstream.h>
    > #include "MolecularIsosurface.h"
    >
    > class WebMOReader
    > {
    > private:
    > ...
    >
    >
    > and so forth. It doesn't matter whether I have the includes at the top
    > of
    > the file or not. It just doesn't like "class Blah" for whatever reason.
    >
    > I have included all the header files in a group that is a part of the
    > project, along with the corresponding source code (with a .cpp suffix).
    >
    > Is there something special I have to do to make Xcode understand C++?
    >
    > Is the suffix of the header file fooling it into thinking it is a Obj-C
    > header? What should I name it if so?
    >
    > Thanks.
    >
    > Albion
    >
    > Albion E. Baucom
    > http://rna.ucsc.edu/albion
    > _______________________________________________
    > cocoa-dev mailing list | <cocoa-dev...>
    > Help/Unsubscribe/Archives:
    > http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    > Do not post admin requests to the list. They will be ignored.
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • On Thu, 1 Jul 2004, myah wrote:

    > On your implementation file webMOReader.m to webMOReader.mm mm is the
    > suffix for objective-c++ code.  If your header is included from that
    > file then it should se it as a obj=c ++ header i think.  I have several
    > integrated c++ and obj-c files in my program. I hope that helps.

    on Thu, 1 Jul 2004, Allan Odgaard wrote:

    > Use .mm instead:
    > http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/
    > 3objc_language_overview/chapter_3_section_10.html

    Allan, Myah,

    Thanks for the replies. I tried this, but nothing changes. Here is the
    header.  Ill be darned if I can figure out what it is complaining about.
    This code happily compiles to object code at the command line with g++. No
    parse errors, no warnings.

    -------------------------------------------------------------------------
    #include <fstream.h>
    #include "MolecularIsosurface.h"

    class WebMOReader
    {
    public:
      WebMOReader();
      ~WebMOReader();

      MolecularIsosurface* SetFileName(char* filename);

      enum {
      INVALID,
      MO,
      DENSITY,
      ESP,
      ELECTROPHILIC,
      NUCLEOPHILIC,
      RADICAL
      } FileType;

    private:
      MolecularIsosurface* isosurface;

      int ReadHeader(ifstream &input);
      int ReadAtoms(ifstream& input);
      int ReadBonds(ifstream& input);
      int ReadAOOrder(ifstream& input);
      int ReadGTO(ifstream& input);
      int ReadSTO(ifstream& input);

      int ReadMO(ifstream& input);
      int ReadDensity(ifstream& input, int occupiedOnly);

      enum dOrbital {dx2,dy2,dz2,dxy,dxz,dyz};
      dOrbital dOrbitalOrder[6];
      enum fOrbital
                      {fx3,fy3,fz3,fxxy,fxxz,fyyx,fyyz,fzzx,fzzy,fxyz};
      fOrbital fOrbitalOrder[10];
    };
    -------------------------------------------------------------------------

    Here are just a "few" of the many errors that it generates for this file
    alone:

    error: parse error before "WebMOReader"  WebMOReader.h:4
    error: syntax error before '{' token    WebMOReader.h:5
    error: parse error before ':' token      WebMOReader.h:10
    error: parse error before '&' token      WebMOReader.h:10

    The source file is now named "WebMOReader.mm". Xcode is just not invoking
    the C++ compiler it seems.

    How about the groups in my project? Could this foul up the compilation? Is
    there anything special about group names in the project that would make
    Xcode behave like this?

    This is frustrating.

    Thanks for any more help.

    Albion
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • On Jul 1, 2004, at 12:14 PM, Albion Baucom wrote:
    > Thanks for the replies. I tried this, but nothing changes. Here is the
    > header.  Ill be darned if I can figure out what it is complaining
    > about.
    > This code happily compiles to object code at the command line with
    > g++. No
    > parse errors, no warnings.

    Are you including this header file from a C or Obj-C source file?

    - Steve
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • On Jul 1, 2004, at 5:56 PM, Albion Baucom wrote:
    > UI front end. So at some point I have a Obj-C header including a C++
    > header so I can access methods and data in that C++ code via the Obj-C

    You should likely be including the c++ header file from within the
    source file, not the Obj-C header. Also, any source file that is
    including the c++ header file needs to be compiled as c++ (or obj-c++).

    Let's say you have:
    foo.h
    foo.cpp
    bar.h
    bar.m

    and you want to use a class declared in foo in your bar.m. You should
    rename bar.m to bar.mm and bar.mm should #include "foo.h". bar.h Should
    not include foo.h, especially if it is going to be included by another
    objective-c source file. Otherwise that one would need to be .mm as
    well.

    I hope that made any sense.

    - Steve
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • >> UI front end. So at some point I have a Obj-C header including a C++
    >> header so I can access methods and data in that C++ code via the Obj-C
    >
    > You should likely be including the c++ header file from within the
    > source file, not the Obj-C header. Also, any source file that is
    > including the c++ header file needs to be compiled as c++ (or
    > obj-c++).
    >
    > Let's say you have:
    > foo.h
    > foo.cpp
    > bar.h
    > bar.m
    >
    > and you want to use a class declared in foo in your bar.m. You should
    > rename bar.m to bar.mm and bar.mm should #include "foo.h". bar.h
    > Should not include foo.h, especially if it is going to be included by
    > another objective-c source file. Otherwise that one would need to be
    > .mm as well.

    Informationally, you can also name your file bar.M (with a capitol M)
    and the system will use the objective-C++ compiler.  That's a bit less
    reliable than the ".mm" extension since HFS+ is case-insenstive and
    will treat foo.M and foo.m as the same file.  It works, nontheless.

    Another option is to bring up the project build settings and look under
    the "Copile Sources As" setting (use the search field if you like).  In
    this setting you can force the compiler to compile all your sources as
    objective-C++ regardless of their file type extensions.

    Another useful tool in working with Objective-C++ is to use compiler
    macros:

    #ifdef cplusplus
    ... insert ++ code here
    #endif

    #if __OBJC__
    ...insert objective-C code here...
    #endif

    the macros can shield your objective-C code from the C++ compiler or
    vice-versa.  This is NOT what the original poster was asking for (in
    that case the C++ should have been visible) but it is a handy technique
    in some cases.

    Scott
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • Bingo Steve! That is the ticket. Your explination made perfect sense.

    Of course I need to figure out how declare class variables that are
    classes from my C++ code without declaring them in the header interface
    decleration.

    Also I have to figure out why I get EXC_BAD_ACCESS faults. ;)

    Thanks again Steve!

    Albion

    On Thu, 1 Jul 2004, Steve Checkoway wrote:

    > On Jul 1, 2004, at 5:56 PM, Albion Baucom wrote:
    >> UI front end. So at some point I have a Obj-C header including a C++
    >> header so I can access methods and data in that C++ code via the Obj-C
    >
    > You should likely be including the c++ header file from within the
    > source file, not the Obj-C header. Also, any source file that is
    > including the c++ header file needs to be compiled as c++ (or obj-c++).
    >
    > Let's say you have:
    > foo.h
    > foo.cpp
    > bar.h
    > bar.m
    >
    > and you want to use a class declared in foo in your bar.m. You should
    > rename bar.m to bar.mm and bar.mm should #include "foo.h". bar.h Should
    > not include foo.h, especially if it is going to be included by another
    > objective-c source file. Otherwise that one would need to be .mm as
    > well.
    >
    > I hope that made any sense.

    Albion E. Baucom
    http://rna.ucsc.edu/albion
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
previous month july 2004 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  
Go to today