Objective-C++ equivalent for the Objective-C function vm_deallocate - objective-c

I have some Objective-C code that I'd like to convert to Objective-C++. To changed the extension of the source files from .m to .mm, and I set the filetype as Objective-C++ Source. However, now my project fails to build. Its giving an error on lines that I call vm_deallocate.
Here is the line:
vm_deallocate(mach_task_self(), (vm_address_t)prevInfo, prevInfo);
And the exact error is:
Use of undeclared identifier vm_deallocate
Is vm_deallocate restricted to Objective-C? Is there an equivalent Objective-C++ function?

No, since it's c.
This is more a header problem.
Make sure you have include in the right file
#include <mach/mach_init.h>
#include <mach/vm_map.h>
And it should compile without complain.

Related

Are you supposed to use a separate header file with .mm files?

In Xcode if you are mixing c++ with objective c are you not supposed to use separate header file from the implementation? How do you declare c++ instance variables if it is supposed to be separate? You can't #include a c++ header file into a non .mm file or else you get compile errors.
Nevermind I figured out that you can extend the interface in the .mm file

Use std::string as datamember in objective-c

I want to use a C++ string object in my Objective-C code instead of a NSString so I don't need to covert it (I need the std::string more often) and I tried doing it like this:
using namespace std;
#interface InstrumentGridViewController : UIViewController {
string* trackName; // also tried using std::string, didn't work
#property (nonatomic, assign) string* trackName;
}
I'm getting errors though, both the string* trackName; statement and the #property line give me Expected specifier-qualifier-list 'string'.
EDIT: I forgot to add #include <string> but adding this gives me the error String: no such file or directory
What matters is the source files this header file is included by. If this header file is included in a normal Objective-C source file (.m extension) the compiler will choke as you described. If you check the errors carefully in Xcode you can see exactly which source file or files this header file failed to compile in.
This is why it is often suggested never to use C++ objects in header files intended to be consumed by Objective C code. C++ in header files tends to bleed all over your code base and force you to compile everything was Objective C++ (.mm extension). Note that with the modern runtime and class extensions it is very easy to work around this. Just move your:
#property (nonatomic, assign) string* trackName;
to a class extension in your InstrumentGridViewController.mm file and remove the trackName ivar declaration that is not needed in the modern runtime. Then your header is Objective C clean and can be included by the rest of your code. Rob Napier has more discussion on this in Wrapping C++ Final Edition.
If you want to compile everything as Obj-c++ and don't want to go through the tiresome process of changing all your file extensions, just provide the -x objective-c++ flag to the compiler (project settings -> other c flags) and link with the stdlibc++. That way you can use c++ all other your code without worrying

Adding a C++ Header to Objective-C project

I've got a strange error working with Photoshop connection API in OSx.
I need to include the header of a cpp file to my project... I start from the adobe example and I included the code in this way:
#include "PSCryptor.h"
which contains the PSCryptor class :
class PSCryptor
{
public:
...
As soon as i try to use PSCrypor object, like with this code
static PSCryptor *sPSCryptor = NULL;
I get this error:
Unknown type name 'class'; did you mean 'Class'?
Could you help me to understand which is my error?
The file is being included in Objective-C files — that is, they have the extension ".m" or they are specifically configured to be compiled as Objective-C (probably the former). Thus, the compiler tries to interpret the code as Objective-C, but C++ is not valid Objective-C, so it complains.
What you need to do is use Objective-C++ instead. Simple fix: change the extension of the files that use that header from ".m" to ".mm".

Compile cxx files in xcode

I am new at IPHone development , I included a library with .cxx as implementation class and a .h header but still i get error at compiling , Please any help?
I get errors at lines like below
class StackEvent;
The Objective-C class that is including any C or C++ code must be renamed with a ".mm" suffix rather than ".m" in the implementation class.
Any Objective-C file (.m) that includes any header containing C++ code, either directly or by chained #includes, will likely fail with compile errors.
You need to rename all your .m files to .mm if they can "see" your C++ header.
If that causes too much renaming, then you'll have to limit your #includes, for example by take the C++ #include out of your header file and putting it only in the .mm files that need it.

Implicit declaration of function - C99

I am currently using Xcode 4, and in my .pch file I have this macro:
#define localize(s) NSLocalizedString((s), nil).
When I try to use this macro in some .m file, I receive this warning: Implicit declaration of function 'localize' is invalid in C99.
This code compiles without a problem, but how can I fix this so I don't get a warning?
I had this problem when I did a global replace of NSLog with DLog. I foolishly included the
#define DLog(...) NSLog(...
statements, so I ended up with
#define DLog(...) DLog(...
which caused the warnings, and a linker error.
Implicit function declarations are those that the compiler sees the first time used as a function call (as opposed to those where a prototype or the function definition is seen first).
Apparently your code used localize(foo) but the macro definition was not visible. Possible reasons: you forgot to #include the file containing the localize macro or the precompilation of headers went south an did not include the localize macro so it was left unexpanded.
Another "foolish" mistake I ran into was the fact that my DLog was defined in the prefix header of the iOS target, so I had to copy it over to the prefix of the OSX target, as well...
I had this problem because I accidentally imported CocoaLumberjack like this:
#import <CocoaLumberjack/DDLog.h>
Apparently the CocoaLumberjack team modularized the code some more; and macros like DDLogError are now defined separately in their own header file.
I replaced the import statement with this and the error went away:
#import <CocoaLumberjack/CocoaLumberjack.h>
In my case only one file was giving this error. Turned out that I added it to the project's tests target membership (in the File Inspector on the right).