NewGen

iOS - swift 프로젝트에 C++ 소스 임포트 해서 사용하기./ import c++ source in the same swift project. 본문

IOS

iOS - swift 프로젝트에 C++ 소스 임포트 해서 사용하기./ import c++ source in the same swift project.

Deep Learning 2020. 12. 26. 21:50

* 비트연산, 대규모 행렬, 어레이 등등... swift로는 약간 모자른 로직을 구현하기 위해서 c++ 을 사용하거나, object-c를 이용해야 함.

* Bit operations, large-scale matrices, arrays, etc... In order to implement the logic that is slightly insufficient in Swift, you must use c++ or use object-c.

* 이미 c++로 구현을 하였다면 이를 swift에서 소스자체로 임포트 해서 빌드하여 사용하는법을 기술함.

If it has already been implemented in C++, it describes how to import and build it from swift as the source itself.

* 대략적인 순서는 이렇다

Using Steps....

  c++ --> object-c or object-c++로 wrapper구현 -> 브리징헤더에서 wrapper를 참조 -> swift에서 사용.

c++ --> obj-c or obj-c++ wrapper for c++ -> import to bridging header -> use in swift.

how to set bridging header....

solodevlab.tistory.com/1189

 

iOS - Swift + Object-C , Bridging Header, 브리징헤더

* Swift로 프로젝트를 생성해서 작업하다가, object-c 로 소스를 짜야 할때가 있슴. * 이때, object-c 소스를 임포트하고, 연결해서 Swift 소스에 붙여서 사용해야 함. 1) 파일추가를 해주고, 2) 새로 생성

solodevlab.tistory.com

 

1) 간단한 c++ 소스를 구현하기 위해서 파일을 생성함.(Create a file to implement a simple c++ source.)

 

2) cpp, h 파일 생성하고 간단히 내용을 기술함.(Create cpp and h files and simply describe the contents)

 

 

3) 소스 내용. :  대략 스트링 문자열을 저장하고 가져오는 초 간단 함수를 만듬.(Source content. : Create a super simple function to store and retrieve roughly a string string.)

 

 

4) object-c or object-c++로 wrapper 소스 구현. : 여기서는 object-c++ (hpp , mm 파일로 만듬)

아래처럼 두 파일을 만들어줌.(Create two files as below)

 

5) wapper 소스를 구현함.(Write wrapper code.)

 

 

6) wrapper 소스 구현 후 헤더를 브리징헤더에 임포트 시켜주어야 함.(After implementing the wrapper source, the header must be imported into the bridging header.)

 

7) swift에서 불러서 사용하면 된다.(Use in swift..)

 

8) 콘솔에 나오는 로그를 보면.. 위에 첨부된 각 소스에서 디버그 메세지를 표시하는 부분을 보면 호출 순서를 알 수 있슴.

If you look at the log on the console... You can see the order of calls by looking at the part that displays debug messages from each source attached above.

각 언어간 타입을 맞추어 주는 작업을 wrapper함수에서 해야 함.

The work of matching the types between languages should be done in the wrapper function.

 ==> c++ 에서 처리되는 string 형과 object-c에서 사용하는 NSString 을 맞추어 주는 작업정도를 해주어야 정확한 결과를 얻을 수 있슴.

 ==> Accurate results can be obtained only by matching the string type handled in c++ with the NSString used in object-c.

 

 

아무렇게나 만든 연습용 소스 첨부.

mvvmTest.zip
0.06MB

Comments