gabry
gabry

Reputation: 1422

Create a podspec for a C++ library

I'm having a few problems creating a podspec file to extract a C++ library from an iOS application.

Actually my main problem are headers, my C++ library depends on rapidjson that is included inside the library repository in an ext_inc directory ( the files are something like ${PROJECT_SOURCE_PATH}/ext_inc/rapidjson/*h).

If I include it in source files like this:

s.source_files = utils/*{cpp,h}', 'handler/*{cpp,h}','ext_inc/**/*h'

When I try to compile the library with:

pod lib lint --verbose report-base.podspec

I get errors like this:

 ../utils/json_serializer.h:8:10: fatal error: 'rapidjson/prettywriter.h' file not found

The errors are caused by the fact that headers are included as:

#include "rapidjson/prettywriter.h"

The compilation goes on if I change that to:

#include "prettywriter.h"

... but that is the wrong way to include an external library like rapidjson and it's not portable at all (the same library is also the core of the android application, so I should remain as crossplatform as possible).

I tried to avoid this problem using the podfile private_header_path and adding a xcconfig with the key:

'HEADER_SEARCH_PATHS' => 'ext_inc'

... but nothing works.

For what I have understood cocoapods build an xcode project from the podspec file, and uses module mapping to map all the source and header files like if they are all in a single directory, and in this context the fact that my headers include a path break everything... there is a way to "save" in the module map an header with a path?

Upvotes: 2

Views: 2192

Answers (1)

gabry
gabry

Reputation: 1422

After a few days struggling on this topic I've found a solution.

The solution requires multiple steps.

  • You must specify inside the podspec file that you want to preserve a path
  • then you have to specify in the header search path used in pod validation (pod lint / pod repo push) where to find the headers.
  • then you have to specify in the header search path of the final application where the headers are located.
  • pod lib lint/pod repo push should be run with the option --skip-import-validation

So given that you want to access rapidjson/prettywriter.h that is located inside ext_inc inside your pod project repository this is what I had to do:

note: spec is the object inside the ruby myproject.podspec

   spec.preserve_paths = 'ext_inc/**'
   spec.pod_target_xcconfig = {
       'HEADER_SEARCH_PATHS' => "\"" + __dir__ + "/ext_inc\""
   }
   spec.xcconfig = {
       'HEADER_SEARCH_PATHS' => "\"${PODS_ROOT}/report-base/ext_inc\""
   }

The strange thing is that I need to build with the ruby trick of using __dir__ since it was the only way to work both when using pod lib lint (local repository) that using pod repo push (cloned on the fly repository).

Upvotes: 2

Related Questions