Swift modulemap tree structure resolution - objective-c

I want to create a modulemap for a Swift iOS framework. It will be a wrapper around a c library. The problem is that the c library includes a file that is inside of another directory. For example "lib.h" has a following line #include "directory/function.h". But of course it can't be found. How can I solve it? The names are made up, but the problem is real. I will appreciate any answer, thanks.
Example structure
SwiftFramework.h
module.modulemap
clib
lib.h
directory
function.h
// module.modulemap file content
framework module SwiftFramework {
umbrella header "SwiftFramework.h"
module CLib [extern_c] {
header "lib.h"
}
}
// lib.h file content
#include "directory/function.h"

Related

'ProductModuleName-Swift.h' file not found for Subprojects

I am having the project(P1) and a sub-project(P2). P2 contains a Swift file (F2.swift) and P1 contains an Objective C file (F1.h and F1.m). I was trying to access F2 in F1. How can i do that?
I tried to #import <P2-Swift.h> in F1.m but it is throwing error 'P2-Swift.h' file not found
Edit:
P.S. The sub-project is static library.
XCode generates the header file for your static swift library next to your app's data in DerivedData folder:
/Users/user/Library/Developer/Xcode/DerivedData/
YourApp-gwvcbdfthcschccmeafalxjghrzo
Build
Intermediates.noindex
StaticLib.build
Debug-iphonesimulator
StaticLib.build
DerivedSources
Where 'StaticLib' is your library name.
So you should just set this path in your Build Settings > Header Search Paths:
$(OBJROOT)/StaticLib.build/$(CONFIGURATION)-$(PLATFORM_NAME)/StaticLib.build/DerivedSources
If you get Could not find or use auto-linked library errors later it means that your project is pure objc and you need to expand it for swift. To fix this just add empty swift file to your objc project and needed swift libraries will be included to your app.
You can add the product module name in your F1.m file and import in it like this #import "ObjCBridgingHeader-Swift.h"
This "ObjCBridgingHeader"is my Project's name and you can check this in the Product Module name.

How to force `Xcode-generated header` ... in a Swift Project (Call Swift from Obj-C)

Xcode does not generate this -Swift.h header file when the project is a Swift project. The documentation says:
When you import Swift code into Objective-C, you rely on an
Xcode-generated header file to expose those files to Objective-C. This
automatically generated file is an Objective-C header that declares
the Swift interfaces in your target. It can be thought of as an
umbrella header for your Swift code. The name of this header is your
product module name followed by adding "-Swift.h".
If I try to include the -Swift.h file anyway, it says file not found.
I've been able to generate this file before in an Objective-C project.
But I want to call Swift from Objective-C, in a Swift project.
How do I call Swift from Obj-C in a Swift project?
Thanks!
If the Objective-C file in which you import *-Swift.h is in the same Swift target that contains the Swift code you are trying to use in Objective-C, you should have no trouble importing it. To make sure you are using the correct name, check the Objective-C Generated Interface Header Name under Build Settings. If the target is a framework, you may need to import the -Swift.h header a little differently:
#import "YourFrameworkModuleName/YourFrameworkModuleName-Swift.h"
whereas in an application target you would just do
#import "YourAppModuleName-Swift.h"
This is something I found from my experience.

Adding objective c class that uses swift classes to bridging header Projectname_swift.h not found

I have an objective-c class that uses swift classes. It all works fine.
I wanted to import the objective-c class into a swift class, so I added its header file to the bridging header. All the sudden I got an error the Projectname_swift.h file is not found.
Any ideas how to resolve this issue?
Is it actually possible?
a circular reference has been created, making it so the Swift code is unable to compile (which leads to the canary error stating that the _Swift.h file is not found).
i have provided a more in depth answer to a similar questions here and here.
long story short, the documentation explicitly says not to this:
To avoid cyclical references, don’t import Swift code into an Objective-C header (.h) file. Instead, you can forward declare a Swift class or protocol to reference it in an Objective-C interface.
Forward declarations of Swift classes and protocols can only be used as types for method and property declarations.
in order to make your code compile again you will need to remove the #import "Projectname_Swift.h" line from the offending Objective-C header. ideally you can simply move the import statement into your .m file, however if you need to publicly expose the Swift class in your ObjC header, then you must forward declare it using #class SomeSwiftClass;.
Let the Xcode build the bridge file from Objective-C to Swift.
Create a temporary directory elsewhere. In there, you create a dummy Xcode Swift project, give the project name the same as your existing Current Project Name.
Then add new file, Objective-C (.m file). The XCode will prompt you to create a bridge header file, click on the create bridge file (the right most button).
Now you locate the header file location in Finder. Then drag into your Current Project of Interest, don't forget to checked the copy file if necessary option. Add necessary #import '.....' in the header file.
You should be good. If everything works fine, delete the dummy project.
Clean derived data. and then #import "ProjectName-Swift.h" in your objective c files.
Go to
Build Settings->Objective-C Generated Interface Header Name
and set the value to YourModule-Swift.h (this is usually already set, this is the filename you need to import on .m file #import "YourModule-Swift.h"
Go to Build Settings and search for "Defines Module", set both values to YES
Create a class in swift with prefix of #objc for example
#objc class mySwiftClass{...}
Build the project again
it will be better if you use error syntax or screen shot. you can simply try this
1. Goto your project on top of right navigation
2. select build settings from middle pain.
3. search for Objective-C bridging header
4. just below this you will find "Generated interface HeaderName"
5. add correct address of your swift file
6. clean and build the project.

Glew not compiling in Xcode

I tried using the Glew Library for shading and was trying to compile a simple program using the NSOpenGLView Class in Xcode 5. The program fails at the following code in glew.h
#if defined(__gltypes_h_)
#error gltypes.h included before glew.h
#endif
It says that Gltypes.h is included before glew.h.
My implementation file for OpenGLView has headers included in following fashion:
#include <glew.h>
#import "OpenGLView.h" // Includes Cocoa.h
#include "LoadShaders.h" // Includes <OpenGL/gl.h>
So, if glew.h is included in the beginning, why is the error coming here. None of the other headers are included before the inclusion of glew.h, so tests for them(like gl.h) are passing in glew.h. I am not able to find out here as to who is including Gltypes.h in this file.
Hope anyone has a clue to it here.
The precompiled header file (.pch) pulls Cocoa in. Edit it to include glew.h before Cocoa.h and you are good to go.

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".