Using C++ namespaces in objective C - objective-c

I am working on a project that requires a third party library implemented in C++. I have successfully added library to my xcode project, but the problem is that the classes in library contains namespaces and when I try to access methods via namespaces, the XCode generates an error that: "utils undeclared". "utils" is the namespace I am trying to use.
My question is that is there a way to use C++ namespaces in ObjectiveC?
The code I am using to call the method is:
utils::method();
I have tried renaming my ObjectiveC ".m" file to ".mm" file, but the problem remains the same.

We are using C++ libraries in Objective-C and have no problem using C++ namespaces. As Mustafa has indicated, you need to change the Objective-C file extension to .mm to get XCode to recognize the file as Objective-C++. Then you just need to #include (not #import) the C++ headers containing the C++ namespace declarations - this is as you would normally do for 'normal' C++.

Related

Use Swift classes in Bridging Objective C files

I have project which is written on Swift but some part of project is written on Objective-C++ for integration with C++ library. My .mm files don't see Swift classes. I have done all steps from Can't use Swift classes inside Objective-C but still can't fix it. Auto-generated "project-Swift.h" generates with errors. I've tried to delete pieces of code in which error occurred and it worked well, but this "project-Swift.h" regenerates every time when I clean the project. Here is screenshot with errors link. Thanks.
This looks like you're not including the headers that define the missing symbols, like NSViewController, before including your bridging header.

C++ file in Swift/Objective C

i am currently developing a simple app on my own.
I want to use a library from some sample app. The library consists of .mm files (C++ code) and runs perfectly on the original project.
However, when i copy the library to my file (Objective C), it seems that my other object files do not see the file. All the classes and protocol defined in this library are not recognised at all.
Errors are such as "No type or protocol name", "Unknown typename "classname" " when i try to use the library classes and protocols.
I have spent quite a lot time searching, but to no veil.
Thanks in advance.
Objective-C simply won't understand the C++ language constructs like class, etc. If you want to use this Objective-C++ code in your app then your app also needs to become Objective-C++ which can be done simply by renaming all your source files from *.m to *.mm.
In the case of Swift, however, you cannot expose C++ to it at all, and can only integrate it with C or Objective-C, so you are therefore forced to create a pure Objective-C wrapper for the C++ code (i.e. implementation in .mm but exposing no C++ types in its header file).

List of all modules

From the Swift documentation:
Any Objective-C framework (or C library) that’s accessible as a module
can be imported directly into Swift. This includes all of the
Objective-C system frameworks—such as Foundation, UIKit, and
SpriteKit—as well as common C libraries supplied with the system.
Where can I find the full list of available modules? I'm particularly interested in the common C libraries part.
It's the same modules that can be imported in Objective-C using the #import statement (except the Swift module, which is the Swift standard library that is only available in Swift and is always imported in Swift anyway). So what you can do to discover them is:
Open an Objective-C project, turn support for modules on in build settings
type #import ... and type some letter, delete it, and the autocomplete should show you a list of all the modules you can import.
From what I can tell, besides the standard Cocoa system frameworks (like Foundation), the only potentially useful ones are:
Darwin - most of the C standard library, plus many POSIX things
ObjectiveC (also imported by Foundation) - the Objective-C runtime library (#import <objc/*>)
Dispatch (also imported by Foundation) - the dispatch library (#import <dispatch/dispatch.h>)

How to set a single header file as C++ compiler in Xcode?

There are some C++ files in my Objective-C project. In order to use STL in header files, I set the project setting to compile source as Objective-C++ in "Build Setting".
But now, I want to use RegexKitLite in my project. I found out RegexKitLite give a compilation error in the Objective-C++ mode,but does compile correctly in the Objective-C mode or when determined automatically in "According to File Type" mode.
My questions is:
How to set a single header file to compile as C++? (I set the File Type as "C++ Header" in Identity and Type, but no effect is seen.).
How to use RegexKitLite from "Objective-C++"?
Header files are not a compilation unit (they are not compiled by themselves, but just included in other files), so setting the type does not have any influence on compilation.
If you include the header from some C++-compiled file, it will be compiled with C++ compiler.
Similarly, if you include the exactly the same header file from Objective-C file, it will be compiled with Objective-C compiler.
Headers are not specific by defaut.
.cpp use cpp compiler and .mm obj c.
So if you have an error it may be because you use a c++ that can not be compiled in objective c (but I don t think it is possible)
Better guess you use a header including obj c declarations in a C++ header.
separate your code to only include c++ compliant code in the cpp files

Mixing C++ and Objective C

Where can i find a concrete document or a dos and donts documentation on using C++ with Objective-C?
Apple seems to have removed that document from their website and i am all puzzled with collating bits of information from blogs and questions posted here.
Anyone can guide about the same.
When do we use .mm file, while mixing syntax or while using an object in .m file which belongs to a C++ class ?
While passing objects between functions belonging to two different language like passing objective-c object to a function in cpp file is it necessary to collect it in void * or can I use (ObjectiveC inteface)*?
You need to use Objective-C++ whenever you are either #include/#importing or directly writing both Objective-C and C++ code in the same file. It's usually obvious with explicit code; the #includes are often less so, and you need to take care to avoid "leaking" one of the languages into too much of the other. Your example of #importing a C++ header file is clear-cut: you can only do that with Objective-C++. Note that if your Cplusplus was a struct type, you could forward-declare it as such instead of #importing a C++ header.
If you do this in a header, it will "poison" the header to only work in that mode. You'll need to actively avoid this, or your whole project will soon end up with only .mm files. I have documented some techniques in this article and previously, in this earlier article. For newer versions of Objective-C, you can also add ivars to classes in category extensions. This means you can define C++-typed ivars in your .mm file, not the header, which allows .m files to #import it.
For your second question (Please only ask one question at a time): the id type is defined in the objc.h header file in terms of C and thus will work in both Objective-C and C++. Likewise, the Objective-C runtime API is exposed in terms of C functions, which will work from C++, too. If you actually want to send messages and access properties on Objective-C objects with the canonical syntax from C++ code, you'll need to switch that file to Objective-C++.
Use .mm files when you have a c++ syntax in your code or when including file(s) which contain c++ code.
Mixing C++ with objective-c may be a bit confusing but if you think pointer-wise than it is not a big deal. Treat C++ object instance methods as you would in C++ and the same goes for objective c objects.