Oscar Franco
Oscar Franco

Reputation: 6230

How to include Xcode sub-project headers?

I'm trying to compile libcpr for iOS. I have built cpr using CMake using the Xcode generator. This has generated an .xcodeproj I have imported (read drag&drop) into my Xcode project. The problem lies when I try to import the library headers, they are not found:

// currently with quote notation but also tried with bracket notation
#import "cpr/cpr.h" // NOT FOUND

You can see a more detailed steps on how I built the project in the respective github issue.

I can see the subproject contains not only the headers for libcpr, but also for libcurl and zlib (which are also necessary):

enter image description here

All the information I can find, regarding Xcode and library headers, seems to be some manual steps like copying the header files, which is at the very least prone to errors. Another option seems to be modifying the User Header Search Paths property in the build settings. I tried setting the User Header Search Paths and Header Search Paths to point to the folders:

enter image description here

I have tried many values in there: Libraries/**, $(PROJECT_DIR)/../cpr/build (I have added the libcpr repo as a submodule, so that's the build output folder) and many other variations. Nothing works.

Here is the repo with my current progress. To generate the artifacts:

That generates the artifact and you should be able to open the .xcodeworkspace file in the iOS folder from the root repo.

Any Help would be greatly appreciated!

Upvotes: 2

Views: 887

Answers (1)

andrestone
andrestone

Reputation: 96

Great findings, Oscar!

So, the issue is really Xcode not knowing where to find the header files, and the solution (as you already found out) is simply to use the right search path(s). You have to use HEADER_SEARCH_PATHS (USER_HEADER_SEARCH_PATHS only works for quote includes).

diff --git a/ios/libcprtest.xcodeproj/project.pbxproj b/ios/libcprtest.xcodeproj/project.pbxproj
index f16a88c..38d64da 100644
--- a/ios/libcprtest.xcodeproj/project.pbxproj
+++ b/ios/libcprtest.xcodeproj/project.pbxproj
@@ -599,6 +599,7 @@
                                        "\"$(PODS_ROOT)/boost\"",
                                        "\"$(PODS_ROOT)/Headers/Private/React-Core\"",
                                        "\"$(PODS_TARGET_SRCROOT)/include/\"",
+                                       "\"$(SRCROOT)/../cpr\"/**",
                                );
                                INFOPLIST_FILE = libcprtest/Info.plist;
                                LD_RUNPATH_SEARCH_PATHS = (
@@ -619,7 +620,7 @@
                                PRODUCT_NAME = libcprtest;
                                SWIFT_OPTIMIZATION_LEVEL = "-Onone";
                                SWIFT_VERSION = 5.0;
-                               USER_HEADER_SEARCH_PATHS = "Libraries/**";
+                               USER_HEADER_SEARCH_PATHS = "";
                                VERSIONING_SYSTEM = "apple-generic";
                        };
                        name = Debug;
@@ -672,6 +673,7 @@
                                        "\"$(PODS_ROOT)/boost\"",
                                        "\"$(PODS_ROOT)/Headers/Private/React-Core\"",
                                        "\"$(PODS_TARGET_SRCROOT)/include/\"",
+                                       "\"$(SRCROOT)/../cpr\"/**",
                                );
                                INFOPLIST_FILE = libcprtest/Info.plist;
                                LD_RUNPATH_SEARCH_PATHS = (
@@ -691,7 +693,7 @@
                                PRODUCT_BUNDLE_IDENTIFIER = "org.reactjs.native.example.$(PRODUCT_NAME:rfc1034identifier)";
                                PRODUCT_NAME = libcprtest;
                                SWIFT_VERSION = 5.0;
-                               USER_HEADER_SEARCH_PATHS = "Libraries/**";
+                               USER_HEADER_SEARCH_PATHS = "";
                                VERSIONING_SYSTEM = "apple-generic";
                        };
                        name = Release;

Alternatively, you could add just the include directories (instead of recursively searching the vendor source / build folder).

// from repo root
find ./cpr -type d -name include | xargs -I {} echo "\"\$(SRCROOT)/.{}\""
"$(SRCROOT)/../cpr/include"
"$(SRCROOT)/../cpr/build/include"
"$(SRCROOT)/../cpr/build/_deps/curl-src/plan9/include"
"$(SRCROOT)/../cpr/build/_deps/curl-src/include"

Cheers!

Upvotes: 1

Related Questions