使用Objective Sharpie为OpenCV 3.4 iOS库生成C#绑定失败求助
Let's break down the problems you're facing and walk through practical solutions to generate valid C# bindings for OpenCV 3.4's iOS framework.
Core Issues Identified
- Sharpie expects a framework umbrella header named
[FrameworkName].h(in your caseopencv2.h), but OpenCV usesopencv.hppinstead. - Directly binding the
.afile fails because Sharpie needs header files to parse the API, not just the binary. - Binding the raw
opencv.hppfile breaks because Clang can't resolve theopencv2/...include paths without explicit search directories.
Solution 1: Match Sharpie's Default Umbrella Header Expectation
This is the simplest fix—we'll create a dummy umbrella header that points to OpenCV's actual opencv.hpp:
- Navigate to
opencv2.framework/Headers - Create a new file named
opencv2.hwith this content:#include "opencv.hpp" - Return to the directory containing
opencv2.framework, then run:sharpie bind -sdk iphoneos11.2 -framework opencv2.framework
Sharpie will now find its expected umbrella header, which seamlessly forwards to OpenCV's real header file.
Solution 2: Explicitly Specify Umbrella Header & Include Path
If you don't want to modify the original framework, use this command to directly target opencv.hpp and tell Clang where to find dependent headers:
sharpie bind opencv2.framework/Headers/opencv.hpp \ -sdk iphoneos11.2 \ -I opencv2.framework/Headers \ -namespace OpenCV \ -output OpenCVBindings
-I opencv2.framework/Headers: Fixes the "file not found" errors foropencv2/xxx.hppincludes-namespace OpenCV: Sets a clean C# namespace for your bindings-output OpenCVBindings: Organizes generated files into a dedicated directory
Solution 3: Handling Source-Compiled OpenCV Frameworks
If you built OpenCV from source with extra modules:
- Double-check your build configuration ensures the
opencv.hppumbrella header is generated in the framework'sHeadersdirectory - Verify all module headers are nested under the
opencv2subdirectory insideHeaders - Apply either Solution 1 or 2 above to generate bindings
Post-Generation Notes
OpenCV has a lot of complex C++ constructs (templates, macros, custom types) that Sharpie might not parse perfectly. After generating the bindings, you'll likely need to:
- Clean up errors in
ApiDefinitions.cs(e.g., remove unsupported template syntax, adjust type mappings) - Fix enum or struct definitions in
StructsAndEnums.csthat were misinterpreted - Add
[Static]or[Internal]attributes where needed to resolve binding conflicts
内容的提问来源于stack exchange,提问作者ConstantLearning

