You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xcode9下iOS Objective-C项目Lua库system函数不可用问题求助

Fixing 'system' is unavailable Error in Lua Library for iOS 11+ (Xcode 9)

Hey there, let's work through this issue. The system() function was restricted in iOS 11 and later, which breaks the third-party Lua library that worked perfectly in Xcode 8/iOS 10. Here are practical solutions to get your project running again:

1. Replace system() with iOS-Allowed APIs in Lua's Source Code

The root issue is that Lua's os.execute() (and related functions) rely on system() under the hood. You can modify the Lua library's source to use posix_spawn—a supported alternative for spawning processes on iOS.

Step-by-Step:

  • Locate the file mentioned in the error: ioslib.c (or sometimes loslib.c in standard Lua distributions)
  • Find the function that calls system()—usually the implementation of os_execute
  • Replace the system() call with a custom implementation using posix_spawn

Here's a sample replacement function you can use:

#include <spawn.h>
#include <sys/wait.h>
#include <errno.h>

int custom_system(const char *command) {
    pid_t processId;
    char *arguments[] = {"/bin/sh", "-c", (char *)command, NULL};
    int exitStatus;

    // Spawn the shell process to execute the command
    int spawnResult = posix_spawn(&processId, "/bin/sh", NULL, NULL, arguments, NULL);
    if (spawnResult != 0) {
        // Return error code if spawn fails
        return -1;
    }

    // Wait for the process to finish and get exit status
    waitpid(processId, &exitStatus, 0);
    return WEXITSTATUS(exitStatus);
}

Then update the original code where system() was called to use custom_system() instead. For example, if the original line was:

int result = system(cmd);

Change it to:

int result = custom_system(cmd);

2. Use a Pre-Adapted Lua Library

If you don't want to modify source code yourself, look for a Lua distribution that's already patched for iOS 11+. Options include:

  • LuaJIT: Check if the latest version has addressed this restriction (note: ensure compatibility with your Objective-C project)
  • Community-maintained Lua forks for iOS that explicitly remove dependencies on system()

3. Disable Unused system()-Dependent Features

If your project doesn't actually use Lua's command-execution functions (like os.execute), you can:

  • Comment out the sections of ioslib.c that call system()
  • Compile Lua with the OS module disabled (check the Lua build configuration flags to exclude this module)

Important Notes:

  • Keep iOS sandbox restrictions in mind: Even with posix_spawn, you can only execute system binaries within your app's sandbox and can't access restricted system resources.
  • After modifying the Lua source, recompile the library for your Xcode project to apply the changes.

内容的提问来源于stack exchange,提问作者Harry Boy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:48:19