{"id":17011786,"url":"https://github.com/flaribbit/love2d-c-library-template","last_synced_at":"2026-05-05T00:31:19.754Z","repository":{"id":137699610,"uuid":"264804939","full_name":"flaribbit/love2d-c-library-template","owner":"flaribbit","description":"主要解决安卓问题","archived":false,"fork":false,"pushed_at":"2020-05-18T02:32:56.000Z","size":213,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-22T13:26:07.326Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/flaribbit.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-18T02:32:16.000Z","updated_at":"2024-08-10T01:35:04.000Z","dependencies_parsed_at":null,"dependency_job_id":"048bc409-2287-4f32-9d39-c84cf5cba87d","html_url":"https://github.com/flaribbit/love2d-c-library-template","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/flaribbit/love2d-c-library-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flaribbit%2Flove2d-c-library-template","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flaribbit%2Flove2d-c-library-template/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flaribbit%2Flove2d-c-library-template/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flaribbit%2Flove2d-c-library-template/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/flaribbit","download_url":"https://codeload.github.com/flaribbit/love2d-c-library-template/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flaribbit%2Flove2d-c-library-template/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265451510,"owners_count":23767771,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-14T06:08:00.556Z","updated_at":"2026-05-05T00:31:19.707Z","avatar_url":"https://github.com/flaribbit.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"游戏项目中遇到的狗屎问题，分享一下解决经验，以一个最基本的程序为例：\n#### `test.c`\n```c\n#include \"lua.h\"\n#include \"lualib.h\"\n#include \"lauxlib.h\"\n\nstatic int about(lua_State *L){\n    lua_pushstring(L,\"test by flaribbit\");\n    return 1;\n}\n\nstatic const struct luaL_Reg funcList[]=\n{\n    {\"about\", about},\n    {0, 0}\n};\n\nint luaopen_test(lua_State *L)\n{\n    luaL_register(L, \"test\", funcList);\n    return 1;\n}\n\n```\n在windows系统和linux系统中，可以直接使用gcc编译，注意链接`lua51.dll`或者`libluajit.so`，记得改掉下面命令中的`path/to/`\n```bash\ngcc test.c path/to/lua51.dll -s -O2 -DNDEBUG -o test.dll\n```\n\n```bash\ngcc test.c path/to/libluajit.so -s -O2 -DNDEBUG -o libtest.so\n```\n然后在lua中调用`require\"test\"`就可以导入使用了，打包游戏的时候记得把库放在外面，和`love.dll`放在一起，不要塞进`game.love`或者`exe`\n\nandroid系统就比较恶心了，自备NDK，以下教程写于windows系统，在`test.c`所在的目录创建`Android.mk`和`Application.mk`，下面的代码中记得修改`path/to/`，为liblove.so所在的目录（可以直接解压love2d的安装包拿）\n\n#### `Android.mk`\n```\nLOCAL_PATH := $(call my-dir)\ninclude $(CLEAR_VARS)\nLOCAL_MODULE := test\nLOCAL_SRC_FILES := test.c\nLOCAL_LDFLAGS := -Lpath/to/$(TARGET_ARCH_ABI) -llove\nLOCAL_C_INCLUDES := include\ninclude $(BUILD_SHARED_LIBRARY)\n```\n\n#### `Application.mk`\n```\nAPP_CPPFLAGS := -frtti \nAPP_LDFLAGS := -latomic\nAPP_ABI := armeabi-v7a arm64-v8a\nAPP_PLATFORM := android-16\nAPP_OPTIM := release\n```\n然后调用`ndk-build`编译，日常记得修改`path/to/`\n```bash\npath/to/ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=Application.mk APP_BUILD_SCRIPT=Android.mk\n```\n会报可能错误链接的warning，问题不大，编译完成后会在`libs`文件夹里得到两个`libtest.so`\n\n最后编写lua程序，这里有个巨坑，`*.so`必须放到`/data/data/package.name`里面才能被`require`正确加载，否则会爆类似下面的神秘错误\n\n```\nError\n\nerror loading module 'test' from file '/sdcard/libtest.so':\ndlopen failed: library \"/sdcard/libtest.so\" needed or dlopened by \"/data/app/org.love2d.android.embed-cfg2TKQ-XsSj13FxWVvTUw==/lib/arm64/liblove.so\" is not accessible for the namespace \"classloader-namespace\"\n\nTraceback\n[C]: at 0x7a3d813a7c\n[C]: in function 'require'\n/sdcard/prog.lua:2: in main chunk\n[C]: in function 'require'\nmain.lua:2: in main chunk\n[C]: in function 'require'\n[C]: in function 'xpcall'\n[C]: in function 'xpcall'\n```\n\n那么问题来了，如何放到`/data/data/package.name`里面呢…？\n\n把`*.so`文件打包进`game.love`，然后用下面的代码copy到save文件夹里，也算是个办法吧。\n```lua\npackage.cpath='/data/data/org.love2d.android.embed/files/save/archive/lib?.so;'..package.cpath\nlove.filesystem.write(\"libtest.so\", love.filesystem.read(\"libtest.so\"))\nrequire \"test\"\n```\n\nit just works\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflaribbit%2Flove2d-c-library-template","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflaribbit%2Flove2d-c-library-template","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflaribbit%2Flove2d-c-library-template/lists"}