다음을 통해 공유


고급(수동) 실제 예제

이 예제에서는 FacebookPOP 라이브러리를 사용합니다.

이 섹션에서는 Apple의 xcodebuild 도구를 사용하여 POP 프로젝트를 빌드한 다음 Objective Sharpie에 대한 입력을 수동으로 추론하는 바인딩에 대한 고급 접근 방식을 설명합니다. 이는 기본적으로 Objective Sharpie가 이전 섹션의 내부적으로 수행하는 작업을 다룹니다.

 $ git clone https://github.com/facebook/pop.git
Cloning into 'pop'...
   _(more git clone output)_

$ cd pop

POP 라이브러리에는 Xcode 프로젝트(pop.xcodeproj)가 있으므로 POP를 빌드하는 데만 사용할 xcodebuild 수 있습니다. 이 프로세스는 Objective Sharpie가 구문 분석해야 할 수 있는 헤더 파일을 생성할 수 있습니다. 이것이 바인딩 전에 빌드하는 것이 중요한 이유입니다. 빌드 xcodebuild 할 때 Objective Sharpie에 전달하려는 것과 동일한 SDK 식별자 및 아키텍처를 전달해야 합니다(그리고 Objective Sharpie 3.0은 일반적으로 이 작업을 수행할 수 있습니다.)

$ xcodebuild -sdk iphoneos9.0 -arch arm64

Build settings from command line:
    ARCHS = arm64
    SDKROOT = iphoneos8.1

=== BUILD TARGET pop OF PROJECT pop WITH THE DEFAULT CONFIGURATION (Release) ===

...

CpHeader pop/POPAnimationTracer.h build/Headers/POP/POPAnimationTracer.h
    cd /Users/aaron/src/sharpie/pop
    export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/Users/aaron/bin::/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/Users/aaron/.rvm/bin"
    builtin-copy -exclude .DS_Store -exclude CVS -exclude .svn -exclude .git -exclude .hg -strip-debug-symbols -strip-tool /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/strip -resolve-src-symlinks /Users/aaron/src/sharpie/pop/pop/POPAnimationTracer.h /Users/aaron/src/sharpie/pop/build/Headers/POP

...

** BUILD SUCCEEDED **

의 일부로 xcodebuild콘솔에 빌드 정보 출력이 많이 있습니다. 위에 표시된 것처럼 헤더 파일이 빌드 출력 디렉터리에 복사된 "CpHeader" 대상이 실행되었음을 확인할 수 있습니다. 이러한 경우가 많으며 바인딩을 더 쉽게 만듭니다. 네이티브 라이브러리 빌드의 일부로 헤더 파일은 종종 바인딩을 위해 구문 분석을 더 쉽게 수행할 수 있는 "공개적으로" 소모성 위치에 복사됩니다. 이 경우 POP의 헤더 파일이 디렉터리에 있다는 것을 알 수 있습니다 build/Headers .

이제 POP를 바인딩할 준비가 되었습니다. 아키텍처를 사용하여 SDK iphoneos8.1 용으로 arm64 빌드하고 싶은데 관심 있는 헤더 파일이 POP git 검사out 아래에 있다는 것을 알고 있습니다build/Headers. 디렉터리를 보면 build/Headers 다음과 같은 여러 헤더 파일이 표시됩니다.

$ ls build/Headers/POP/
POP.h                    POPAnimationTracer.h     POPDefines.h
POPAnimatableProperty.h  POPAnimator.h            POPGeometry.h
POPAnimation.h           POPAnimatorPrivate.h     POPLayerExtras.h
POPAnimationEvent.h      POPBasicAnimation.h      POPPropertyAnimation.h
POPAnimationExtras.h     POPCustomAnimation.h     POPSpringAnimation.h
POPAnimationPrivate.h    POPDecayAnimation.h

살펴보면 POP.h라이브러리의 기본 최상위 헤더 파일인 다른 파일을 #import확인할 수 있습니다. 이 때문에 Objective Sharpie에 전달 POP.h 하기만 하면 되며, clang는 백그라운드에서 나머지 작업을 수행합니다.

$ sharpie bind -output Binding -sdk iphoneos8.1 \
    -scope build/Headers build/Headers/POP/POP.h \
    -c -Ibuild/Headers -arch arm64

Parsing Native Code...

Binding...
  [write] ApiDefinitions.cs
  [write] StructsAndEnums.cs

Binding Analysis:
  Automated binding is complete, but there are a few APIs which have
  been flagged with [Verify] attributes. While the entire binding
  should be audited for best API design practices, look more closely
  at APIs with the following Verify attribute hints:

  ConstantsInterfaceAssociation (1 instance):
    There's no fool-proof way to determine with which Objective-C
    interface an extern variable declaration may be associated.
    Instances of these are bound as [Field] properties in a partial
    interface into a near-by concrete interface to produce a more
    intuitive API, possibly eliminating the 'Constants' interface
    altogether.

  StronglyTypedNSArray (4 instances):
    A native NSArray* was bound as NSObject[]. It might be possible
    to more strongly type the array in the binding based on
    expectations set through API documentation (e.g. comments in the
    header file) or by examining the array contents through testing.
    For example, an NSArray* containing only NSNumber* instances can
    be bound as NSNumber[] instead of NSObject[].

  MethodToProperty (2 instances):
    An Objective-C method was bound as a C# property due to
    convention such as taking no parameters and returning a value (
    non-void return). Often methods like these should be bound as
    properties to surface a nicer API, but sometimes false-positives
    can occur and the binding should actually be a method.

  Once you have verified a Verify attribute, you should remove it
  from the binding source code. The presence of Verify attributes
  intentionally cause build failures.

  For more information about the Verify attribute hints above,
  consult the Objective Sharpie documentation by running 'sharpie
  docs' or visiting the following URL:

    http://xmn.io/sharpie-docs

Submitting usage data to Xamarin...
  Submitted - thank you for helping to improve Objective Sharpie!

Done.

Objective Sharpie에 인수를 -scope build/Headers 전달했습니다. C 및 Objective-C 라이브러리는 바인딩하려는 API가 아닌 라이브러리의 구현 세부 정보인 다른 헤더 파일을 포함해야 #import #include 하므로 이 -scope 인수는 Objective Sharpie에게 디렉터리 내 -scope 의 파일에 정의되지 않은 API를 무시하도록 지시합니다.

-scope 인수는 클린 구현된 라이브러리에 대해 선택 사항이지만 명시적으로 제공하는 데는 아무런 해가 없습니다.

라이브러리의 헤더가 iOS SDK 헤더를 가져오는 경우(예: #import <Foundation.h>범위를 설정해야 함) Objective Sharpie는 가져온 iOS SDK 헤더에 대한 바인딩 정의를 생성하므로 바인딩 프로젝트를 컴파일할 때 오류가 발생할 가능성이 큰 바인딩이 생성됩니다.

또한 지정했습니다 -c -Ibuild/headers. 첫째, 인수는 -c Objective Sharpie에게 명령줄 인수 해석을 중지하고 후속 인수 를 clang 컴파일러에 직접 전달하도록 지시합니다. 따라서 -Ibuild/Headers CLANG가 POP 헤더가 있는 under build/Headers에서 포함을 검색하도록 지시하는 clang 컴파일러 인수입니다. 이 인수가 없으면 clang는 ing 파일을 POP.h #import찾을 위치를 알 수 없습니다. Objective Sharpie를 사용하는 거의 모든 "문제"는 clang에 전달할 내용을 파악하는 것으로 귀결됩니다.

바인딩 완료

이제 Objective Sharpie가 생성되고 Binding/StructsAndEnums.cs 파일이 생성되었습니다Binding/ApiDefinitions.cs.

이들은 바인딩에서 Objective Sharpie의 기본 첫 번째 패스이며, 몇 가지 경우에는 필요한 것일 수 있습니다. 그러나 위에서 설명한 대로 개발자는 일반적으로 Objective Sharpie가 완료된 후 생성된 파일을 수동으로 수정하여 도구에서 자동으로 처리할 수 없는 문제를 해결해야 합니다.

업데이트가 완료되면 이제 이러한 두 파일을 Mac용 Visual Studio 바인딩 프로젝트에 추가하거나 최종 바인딩을 생성하기 위해 btouch 또는 bmac 도구에 직접 전달할 수 있습니다.

바인딩 프로세스에 대한 자세한 설명은 전체 연습 지침을 참조 하세요.