I have three projects in XCode.
ProjectA, a pure C project
ProjectB, a objective C project
ProjectC, a mix of objective C and Swift
ProjectA is a static library project used in ProjectB.
Header files from projectA are in copy file target, it is in ProjectB\lib\ProjectA.a and \lib\include folder.
ProjectB is linked as a sub project in ProjectC.
ProjectC imports some header file from ProjectB.
Those header files in ProjetB have imported header files in ProjectA.
My problem is, ProjectA is not visible in ProjectC. ProjectC compilation fails since ProjectC imports headers from ProjectB and B has header files imported from projectA.
What is the way to solve this C->A dependency on .h files?
In ProjectA.
file DesTypes.h:
// some types are defined here.
In ProjectB.
ProjectA.a is linked as static lib.
File CryptoUtil.h :
#import DesTypes.h
// some function prototypes used types from Destypes. The .h is in search header path. So no problem.
I need the import here since some function prototype uses the types from DesTypes.h.
Otherwise importing DesTypes.h in CryptoUtil.m file would solve the problem.
In ProjectC
File MyViewController.h:
#import CryptoUtli.h
// Here I get compile error for chaining. I do not use anything from DesType.h.
Related
I am trying to package C code into a Swift module, let's call it CModule. Once I have put it into the base folder of a project (SwiftModule) and configured the Search Paths, I could get auto-complete to work in the Swift files, as well as detect errors/warnings.
The problem is, it won't recognize the module when it's imported, and Jump to Definition results in the following error:
Couldn't Generate Swift Representation
Error (from SourceKit):
"Could not load module: CModule"
CModule contains the following files:
src folder, containing the implementation and an umbrella header exposing all required functions (by importing other sub-headers) such
that main_header.h imports componentA.h, componentB.h etc
module.modulemap, containing only:
module CModule [system] {
header "src/main_header.h"
export *
}
The CModule folder is then put at the root of the SwiftModule project.
The framework and import search paths are defined as follows: $(PROJECT_DIR)/CModule (recursive)
Considering auto-complete and others work, I am confident the import search path is set correctly. No idea about the framework search path.
Additional details:
There are no spaces anywhere in my paths.
SwiftModule is a "framework" that will eventually be put on CocoaPods.
There are no public headers but the base SwiftModule header. CModule headers are all defined as Project headers.
I am trying to find a way of wrapping an existing C library with an Apple .framework structure. The key sticking point is avoiding the need to specify the Search Headers field in Settings.
Typically in a framework you specify something like:
#import <Foundation/Foundation.h>
where Foundation is the framework name and the .h file is an umbrella header.
When testing with existing code, for the sake of argument OpenSSL, the project is using #include <openssl/file.h> internally to refer to its files. Once you want to place this inside a framework for convenience every include naturally needs to be changed to <NameOfFramwork/openssl/file.h> or you must add the $(SRCROOT)/Path/To/Frameworks/NameOfFramework.framework/Headers to the search path. This is terribly inconvenient and kills a lot of the value of the framework format. It only becomes worse when you want to wrap multiple SDK versions of the library in an XCFramework.
I'm wondering specifically if the ModuleMap can help avoid the need to change the #includes? I've added a modulemap I'm creating as a test.
Experimental module.modulemap
framework module LibreSSL [extern_c] {
umbrella header "LibreSSL.h"
export *
module * { export * }
explicit module LibreSSL_Private [extern_c] {
umbrella "Headers/include"
link "LibreSSL"
export *
}
}
One unsatisfying solution is to switch to an XCFramework comprised of Libraries instead of Frameworks.
xcodebuild \
-create-xcframework \
-library <path/to/library> \
-header <path/to/headers> \
-output MyCool.xcframework
Then header search paths work correctly, but you lose the .modulemap and the nice framework structure that keeps everything together neatly per SDK build.
I have an iOS project-ProjectX (not created by me) which is able to access declaration from a .h file without using #include "someHeader.h".
In ProjectX, I could just create an empty File and refer to a declaration in "someHeader.h", which I find perplexing. Example:
#import <Foundation/Foundation.h>
#implementation Empty:NSObject
SOME_TYPE_FROM_SOME_HEADER_H x;
#end
and the compiler automatically knows where the definition is?!
I have since tried to create an identical project, duplicating all the project settings, adding static libraries/files, etc. but to no avail.
Any ideas on what I might have missed out or what do I need to configure in the project to achieve this?
As mentioned by Rishab, I was missing a precompiled header (.pch) file. In the project, a pch imported a static library which contained the header file. Therefore, I was able to call the definitions directly.
I have a static framework B which I use inside a framework A. B has a class C I want to also expose in A, i.e. in the illustration below I want the App to be able to use the class.
App > framework A > static framework B > class C
If I try to include the header for C in the public headers of A, I get 'duplicate interface definition'. But as the code is compiled into A from B, I just need to expose the class interface – presumably that will allow the App to link correctly...?
I achieved this through a bit of a workaround.
When a static library/framework is compiled into another framework/static library, its symbols are still exposed (verify through nm <binary> | grep <symbol>). This means you just need your App code to know about those symbols – i.e. including the header for class C in framework A.
In your headers in framework A, you need to include the class C header. When compiling the framework you need to use "ClassC.h", but when the framework is being used you need <FrameworkName/ClassC.h>, as that is its location in the framework included by the App. To do this you use a macro which is removed in a run script phase.
So, in your framework A target build phases, make sure your header for Class C is in the 'public' section of your copy headers phase, then add a 'Run Script' phase and paste the following:
TARGET_MACRO="TARGET_FRAMEWORK"
cd -P "$BUILT_PRODUCTS_DIR/$WRAPPER_NAME/Headers/"
perl -0pi -e "s/\#if ${TARGET_MACRO}.*?\#else\r?\n?(.*?)\r?\n?\#endif/\1/sg" *.h
Change `TARGET_FRAMEWORK" to be whatever, or leave it.
In Framework A build settings, under preprocessor macros, add TARGET_FRAMEWORK=1 for all build configurations.
Then, in your Framework A headers that include Class C, use:
#if TARGET_FRAMEWORK
#import "ClassC.h"
#else
#import <FrameworkA/ClassC.h>
#endif
I have objective-c project and I added swift files in it. i created bridge file and imported swift file in some header files without problems.
But I need to import some header files to swift files by adding them in the "<project-name>-Bridging-Header.h" file.
If I put header file in that bridge file and this header file was import swift file before; Xcode give me error message: "file not found" for the swift bridge file.
i.e:
I have project name called: "ProjectBlaBla"
I have header file called "readingPage.h"
I have swift file called: "readingSwift.swift"
swift bridge file's name: "ProjectBlaBla-Swift.h"
I created header bridge file: "ProjectBlaBla-Bridging-Header.h"
I imported "ProjectBlaBla-Swift.h" in "readingPage.h" file without problem and used swift classes inside objective-c
when I import "readingPage.h" in the "ProjectBlaBla-Bridging-Header.h", I got error message in "readingPage.h" said: "ProjectBlaBla-Swift.h file not found"
any suggestions ?
thanks
You are not able to reference -Swift.h files directly or indirectly in -Bridging-Header.h files.
If you open -Swift.h, you will see a line near the top, in my case line 99: #import "/Users/.../...-Bridging-Header.h", meaning -Swift.h already imports -Bridging-Header.h, so importing back creates a circular dependency.
To avoid this, any header you import in -Bridging-Header.h must use forward references to Swift classes or protocols it uses as described in answers to this question.
In short, if readingPage.h uses a Swift class named MySwiftClass you should:
Remove any references to -Swift.h from readingPage.h.
Import -Swift.h in readingPage.m
Insert #class MySwiftClass; into readingPage.h before the class is used, letting Objective-C know that such a class exists and is declared elsewhere.
Check whether the bridging header path is correct. On the left, select your project name -> TARGETS -> Build Settings -> search for Objective-C Bridging Header. Refer the photo below.
Two options
Import the "ProjectBlaBla-Swift.h" inside the "readingPage.m" file instead of "readingPage.h" file
Create a new PCH file named "Prefix.pch" and import "ProjectBlaBla-Swift.h" inside the "Prefix.pch" file.
Note: Prefix.pch is a precompiled header which makes compiling faster. You do not need to reimport any files that are imported in prefix.ch file.