본문으로 바로가기

[KMM] Windows dll 빌드 방법 및 함수 설정 방법

category My space 2025. 1. 12. 17:54
반응형

Kotlin Multiplatform을 사용하여 Windows에서 사용할 수 있는 DLL을 생성하고 사용하는 방법은 다음과 같습니다:

## DLL 생성

1. Kotlin Multiplatform 프로젝트 설정:
   - `build.gradle.kts` 파일에서 Windows 타겟을 지정하고 공유 라이브러리를 생성하도록 설정합니다:

```kotlin
kotlin {
    mingwX64("native") { // Windows 타겟 지정
        binaries {
            sharedLib {
                baseName = "mylib" // DLL 파일명 지정
            }
        }
    }
}
```

2. Kotlin 코드 작성:
   - `nativeMain` 소스 세트에 DLL로 노출할 함수를 작성합니다.
   - `@CName` 어노테이션을 사용하여 C에서 호출 가능한 함수명을 지정합니다:

```kotlin
@CName("myFunction")
fun myFunction(): Int {
    return 42
}
```

3. DLL 빌드:
   - 터미널에서 다음 명령을 실행하여 DLL을 생성합니다:
     ```
     ./gradlew linkNative
     ```
   - 생성된 DLL은 `build/bin/native/releaseShared/` 디렉토리에 위치합니다.

## Windows에서 DLL 사용

1. 헤더 파일 생성:
   - Kotlin 컴파일러가 자동으로 생성한 C 헤더 파일(`mylib_api.h`)을 사용합니다.

2. C/C++ 프로그램에서 DLL 로드 및 사용:
   - 헤더 파일을 포함하고 DLL을 로드하여 함수를 호출합니다:

```c
#include <windows.h>
#include "mylib_api.h"

int main() {
    HMODULE hDLL = LoadLibrary("mylib.dll");
    if (hDLL != NULL) {
        typedef int (*MyFunction)();
        MyFunction myFunction = (MyFunction)GetProcAddress(hDLL, "myFunction");
        if (myFunction != NULL) {
            int result = myFunction();
            printf("Result: %d\n", result);
        }
        FreeLibrary(hDLL);
    }
    return 0;
}
```

3. 컴파일 및 실행:
   - C/C++ 프로그램을 컴파일하고 실행합니다. DLL 파일이 실행 파일과 같은 디렉토리에 있어야 합니다.

이 방법을 통해 Kotlin Multiplatform에서 생성한 함수를 Windows DLL로 만들고 C/C++ 프로그램에서 사용할 수 있습니다[1][2][14].

Citations:
[1] https://juyeop.tistory.com/72
[2] https://discuss.kotlinlang.org/t/calling-a-dll-in-my-kotlin-multiplatform-project/13616
[3] https://ca.indeed.com/career-advice/career-development/how-to-open-dll-files
[4] https://en.wikipedia.org/wiki/Dynamic-link_library
[5] https://velog.io/@ams770/Android%EC%99%80-iOS%EC%97%90%EC%84%9C-Jetbrains-Compose%EB%A5%BC-%EC%82%AC%EC%9A%A9%ED%95%98%EC%97%AC-UI-%EC%BD%94%EB%93%9C-%EA%B3%B5%EC%9C%A0%ED%95%98%EA%B8%B0-2
[6] https://kotlinlang.org/docs/native-dynamic-libraries.html
[7] https://learn.microsoft.com/en-us/troubleshoot/windows-client/setup-upgrade-and-drivers/dynamic-link-library
[8] https://velog.io/@mraz3068/Compose-multiplatform-apply-font-by-moko-resources
[9] https://www.youtube.com/watch?v=M1JWWHc50Kw
[10] https://hik-coding.tistory.com/175
[11] https://youtrack.jetbrains.com/issue/KT-51726/Kotlin-Multiplatform-konan-llvm-1110-windows-unable-to-find-library
[12] https://stackoverflow.com/questions/68782815/skeleton-for-kotlin-native-multiproject-with-dll-library-for-backend-and-fronten
[13] https://deview.kr/data/deview/session/attach/6_Kotlin%20Multiplatform%20Mobile%EC%9D%84%20%ED%99%9C%EC%9A%A9%ED%95%9C%20%EB%8D%B0%EB%A7%88%EC%97%90%EC%B9%B8%20%EB%93%9C%EB%9D%BC%EC%9D%B4%EB%B2%84%EC%95%B1%20%EA%B0%9C%EB%B0%9C%20%EC%9D%B4%EC%95%BC%EA%B8%B0.pdf
[14] https://proandroiddev.com/kotlin-native-use-kotlin-in-c-and-apple-framework-part-2-495a7952f831?gi=ae10bb42dd65
[15] https://www.jetbrains.com/help/kotlin-multiplatform-dev/multiplatform-create-first-app.html
[16] https://book.kotlincn.net/text/whatsnew1530.html
[17] https://www.youtube.com/watch?v=XXkxkruDRmA
[18] https://www.indeed.com/career-advice/career-development/how-to-open-dll-files
[19] https://stackoverflow.com/questions/66000859/how-are-dlls-usually-packaged-with-applications
[20] https://www.fortect.com/fix-dll-errors/run-dll-file/
[21] https://learn.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=msvc-170&redirectedfrom=MSDN
[22] https://stackoverflow.com/questions/35401050/how-to-use-a-dll
[23] https://kb.blackbaud.com/knowledgebase/articles/Article/48280

반응형

'My space' 카테고리의 다른 글

맥북 설치후 할일  (1) 2022.10.20
Multi Theme 사용  (0) 2020.08.31
app signing 및 출시  (0) 2020.08.14
calendar 관련 자료  (0) 2020.08.13
Proguard 적용  (0) 2020.08.13