将SDL2添加到android项目时,dlopen失败(返回NULL)

时间:2019-04-23 03:44:07

标签: android cmake sdl

我在Android Studio3.4上使用 cmake3.6(NDK19) SDL2.0.9 添加到我的Android项目中,但出现了此错误:

" E/SDL: nativeRunMain(): Couldn't load library /data/app/org.libsdl.app-eOed871MljqNS6cZDl_DBg==/lib/arm64/libmain.so"

调试后,我在SDL2 / src / core / androd / SDL_android.c中找到此行( library_handle为NULL ):

library_handle = dlopen(library_file, RTLD_GLOBAL);
if (library_handle) {
...
} else {
        __android_log_print(ANDROID_LOG_ERROR, "SDL", "nativeRunMain(): Couldn't load library %s", library_file);
}

以下是我的 CMakeLists.txt

cmake_minimum_required(VERSION 3.4.1)

file(GLOB my_source ${CMAKE_SOURCE_DIR}/*.cpp ${CMAKE_SOURCE_DIR}/*.c)

add_library(main
        SHARED
        main.cpp)

include_directories(${CMAKE_SOURCE_DIR}/include)

add_subdirectory(${CMAKE_SOURCE_DIR}/SDL2)
include_directories(${CMAKE_SOURCE_DIR}/SDL2/include)

find_library(log-lib
        log)

target_link_libraries(
        main
        SDL2
        GLESv1_CM
        GLESv2
        -landroid
        ${log-lib})

和我的 main.cpp

#include <android/log.h>

#define LOG_TAG "gmyffplay"
#define LOGV(...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))

#include "SDL.h"

extern "C" {

int main(int argc, char *argv[]) {

    char *video_path = argv[1];
    LOGI("video_path  : %s", video_path);

    SDL_Window *window;
    SDL_Renderer *renderer;
    SDL_Event event;

    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        LOGE("Could not initialize SDL - %s", SDL_GetError());
        return 1;
    }

    window = SDL_CreateWindow("SDL_Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 0, 0,
                              SDL_WINDOW_SHOWN);
    renderer = SDL_CreateRenderer(window, -1, 0);
    SDL_Surface *bmp = SDL_LoadBMP("image.bmp");
    SDL_SetColorKey(bmp, SDL_TRUE, 0xffffff);
    SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, bmp);
    SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
    while (1) {
        if (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                break;
            }
        }
        SDL_SetRenderDrawColor(renderer, 0, 133, 119, 255);
        SDL_RenderClear(renderer);
        SDL_RenderCopy(renderer, texture, NULL, NULL);
        SDL_RenderPresent(renderer);
    }
    SDL_FreeSurface(bmp);
    SDL_DestroyTexture(texture);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}
}

我确定library_file存在,apk中的libmain.so和libSDL2在libmain.so之前加载。

任何帮助我的人都会感激

编辑:

这是我的项目架构

enter image description here

2 个答案:

答案 0 :(得分:2)

我在android-ndk github issues上找到了一个解决方案,似乎发现了用于动态链接 libdl.so 库的库存在问题,其中声明了 dlopen 函数

因此您需要更改:
find_library(ANDROID_DL_LIBRARY dl)
SDL2 / CMakeLists.txt

中的find_library(ANDROID_DL_LIBRARY NAMES libdl.so dl)

答案 1 :(得分:1)

目前,我仅找到一种解决方案:将NDK降级到r18b(r19c-不起作用)。然后dlopen开始加载库。我无法确定课程dlerror到底发生了什么,总是返回(空),没有其他尝试从其他共享库加载共享库的情况。您可以在以下位置找到旧的ndk版本:https://developer.android.com/ndk/downloads/older_releases.html卸载并解压缩然后添加到您的local.properties文件中:

ndk.dir=/home/<your_path>/android-ndk-r18b