Xcode9下iOS Objective-C项目Lua库system函数不可用问题求助
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 sometimesloslib.cin standard Lua distributions) - Find the function that calls
system()—usually the implementation ofos_execute - Replace the
system()call with a custom implementation usingposix_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.cthat callsystem() - 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

