Re: Trying to compile an ObjectiveC and C++ program in XCode 3.1
Re: Trying to compile an ObjectiveC and C++ program in XCode 3.1
- Subject: Re: Trying to compile an ObjectiveC and C++ program in XCode 3.1
- From: Robert Douglas <email@hidden>
- Date: Thu, 11 Sep 2008 17:03:00 -0700
Thanks Johnathan. That was the problem, and #undef worked like a
charm. I also had a look at the preprocessor output as Scott suggested
and the lines had been converted to gobbledygook.
Rob
On 11-Sep-08, at 4:42 PM, Jonathan Prescott wrote:
You might want to take a look at the other header files you might be
including, including those included by the AppKit or other Apple
headers. MAX is a well-known MACRO definition for providing a max
"function" for C (and Objective-C) environments, and, if it is being
expanded by the C/C++ pre-processor (because the macro is defined in
some other header file from the C-domain like stdlib.h or stdio.h,
or something to that effect), the resulting C/C++ code is going to
be very confusing to the compiler.
In C-land, and Objective-C land, MAX is typically defined as :
# define MAX(x,y) ((x) > (y) ? (x) : (y))
in one or more of the stdxxx.h header files (or some other header
file, it's really all over the place!). The "MAX(x,y)" is replaced
by the expression on the right whenever the MAX(...) is seen,
including in your template definition. Which means the compiler is
attempting to compile:
template<class T>
inline const T& ((const T& a) > (const T& b) ? (const T&a) : (const
T&b)) (const T& a, const T& b) { ... }
which is utter nonsense to the C++ compiler (or Objective-C++
compiler).
You might want to 1) rename your MAX template function to "max" or
"max_of", or "Max", or something other than MAX all caps; or 2) put
something like
#if defined(MAX)
# undef MAX
#endif
before:
template<class T>
inline const T& MAX(....) { ... }
Macro definitions do not follow anything like C++ overloading rules,
scoping, etc.. They are simply text substitutions, which is why
what actually gets presented to the C++ compiler/Objective-C++
compiler (really, the same animal) is going to be some funny text
sequences that do not make sense.
Note that the same holds true for MIN. One should note that SQR
is NOT defined in stdxxx.h, which is why there is not a name
collision.
Jonathan
On Sep 11, 2008, at 6:51 PM, Robert Douglas wrote:
I'm in the same boat. Is the C++ code handled the same in both?
I've been trying to add some numerical recipes routines to my cocoa
app and I'm stymied by an apparent difference. The nr3.h header
compiles fine if I have it in a .cpp file, but not when it is in
a .mm one. The line
template<class T>
inline const T &MAX(const T &a, const T &b)
{return b > a ? (b) : (a);}
gives me these compile errors:
error: expected unqualified-id before '{' token
error: expected `)' before '{' token
error: invalid function declaration
error: expected unqualified-id before ')' token
etc.
Curiously, some other nearby lines compile fine. eg.
template<class T>
inline T SQR(const T a) {return a*a;}
Any suggestions?
Thanks,
Rob
On 11-Sep-08, at 2:15 PM, Randy Bradley wrote:
The “.mm” extension is required for source that contains both C++
and
Objective-C, thus called Objective-C++. Not required for pure C++
source.
A couple ideas:
1. Be sure to add the C++ source files to your target. (ie, look
for the
checkmark in the target column)
2. Any objective-c source that creates C++ declared objects also
must have
the “.mm” extension and must include the C++ header files.
I'm relatively new programming ObjectiveC and Cocoa, and I'm
trying to add
some C++ code to my existing ObjectiveC/Cocoa code using Xcode
3.1. I have my
C++ files in a separate .mm files (as the documentation says to),
and my C++
headers are in a xxx.h file. When building the code, I keep
getting my C++
'class' and everything associated with it as undefines.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden