0

I'm writing some C++ code in iOS (objective-c++) and I would like to use libcpr to do some http requests. I would like to let XCode compile libcpr along with my source code and not generate a static .so before hand. Is this possible? If so could you detail the specific steps I would follow?

This is what I have in my mind:

  1. Add libpcr as a git submodule
  2. ??? Somehow tell Xcode to compile and include the source/header files in the submodule
  3. In my Obj-C code #include <cpr/cpr.h> and use the functions directly

Any pointers whatsoever are much appreciated.

[Edit 1]

At the very worst I would accept hooking CMake step into Xcode so it generates a .a or a .so that I can statically link. But really a noob with C++ tooling, so any help is greatly appreciated.

Oscar Franco
  • 5,691
  • 5
  • 34
  • 56
  • You would need to add the source files of libcpr just as any other source file you use to your project (or makefile, if XCode happens to use them – never worked with XCode myself, so you'll need to find out on your own). I personally would rather go the way with the library – as a *separate* project; this way you won't need to re-compile it when using it in *another* project. – Aconcagua Jul 29 '22 at 11:58
  • That's an oversimplification, since the library is build with CMake and probably does not just compile with Xcode, and I also would like more specific instructions but thanks for your answer – Oscar Franco Jul 29 '22 at 11:59
  • Well, CMake actually is a tool to produce *makefiles* – so you'd locate the generated makefile and call `make -f /path/to/makefile` on it. Depending on the targets defined, possibly rather `make all -f ...` and `make install -f ...`. You might need add some OS dependent tweaks as well, but not aware of iOS specifics myself. – Aconcagua Jul 29 '22 at 12:04
  • Are you *cross-compiling*, by the way? – Aconcagua Jul 29 '22 at 12:06
  • 2
    There are a few different ways to do this. IMO if libcpr is build with CMAKE, its best not to try to add the source directly into a xcode project. CMAKE does have a Xcode generator (https://cmake.org/cmake/help/latest/generator/Xcode.html) so it could generate you the library project, which you can import as a sub project into your workspace. – mani Jul 29 '22 at 12:08
  • @mani that sounds like what I need. Would you mind giving more detailed instructions? – Oscar Franco Jul 29 '22 at 12:48
  • @mani I agree, that's what I would do. I don't know how to do the CMAKE -> Xcode project bit, but including the generated library project in your main project is trivial, OP. – Paul Sanders Jul 29 '22 at 13:00
  • I have managed to use a Xcode toolchain. That has generated a cpr.xcodeproj, now I only need to figure out how to add it as a subproject... – Oscar Franco Jul 29 '22 at 13:20
  • Create an Xcode workspace (`File —> New —> Workspace`). Drag your .xcodeproj into the workspace. Add `libcpr` as a dependency. Detailed instructions on how to create workspace: https://www.dev2qa.com/how-to-create-xcode-workspace-and-add-new-exist-project-into-it/. Add project as depedency: https://stackoverflow.com/questions/4219054/xcode-adding-a-project-as-a-build-dependency – mani Jul 29 '22 at 16:01
  • I managed to add libcpr as a dependency but the headers are not found (when I try #include ). I modified on Xcode the User Header Search Paths and even then no luck – Oscar Franco Jul 29 '22 at 17:13
  • Add the relative path to `cpr/include` to header search path in your project. And then include with `#include "cpr/cpr.h"` << note quotes i.e relative path include and not module includes. Without setting up a test project it’s hard to tell what’s going on. If you continue to run into issues, please setup a minimal project and push to github – mani Jul 29 '22 at 19:22
  • I have created a repo with my current progress. https://github.com/ospfranco/libcprtest/tree/main/ios After trying to compile the lib again, outside of the repository, this time I got a dylib library. I have added it to the library and pasted the cpr repo so I can include the headers. The app compiles but now it crashes upon start. Any help would be greatly appreciated! – Oscar Franco Jul 30 '22 at 19:11

0 Answers0