在項(xiàng)目中使用到了一個(gè)庫, 里面調(diào)用了 system. system 調(diào)用主要是為了獲取一個(gè)返回值 stat.
const char *cmd = luaL_optstring(L, 1, NULL);
int stat = system(cmd);
if (cmd != NULL)
return luaL_execresult(L, stat);
else {
lua_pushboolean(L, stat); /* true if there is a shell */
return 1;
}
然后在 Xcode9 里面, 當(dāng)前 system 已經(jīng)被移除了... 因?yàn)?baseSDK 升級到了 iOS 11.0:
__swift_unavailable_on("Use posix_spawn APIs or NSTask instead.", "Process spawning is unavailable")
__API_AVAILABLE(macos(10.0)) __IOS_PROHIBITED
__WATCHOS_PROHIBITED __TVOS_PROHIBITED
int system(const char *) __DARWIN_ALIAS_C(system);
按照上面的說法, 需要用 posix_spawn API 或者是 NSTask 來代替 system.
故參考前人的解決方式, 準(zhǔn)備替換掉 system 的調(diào)用.
首先引入頭文件和一個(gè)外部變量:
#include <spawn.h>
extern char **environ;
然后再替換掉之前的 system 調(diào)用:
替換前:
static int os_execute (lua_State *L) {
const char *cmd = luaL_optstring(L, 1, NULL);
int stat = system(cmd);
if (cmd != NULL)
return luaL_execresult(L, stat);
else {
lua_pushboolean(L, stat); /* true if there is a shell */
return 1;
}
}
替換后:
static int os_execute (lua_State *L) {
const char *cmd = luaL_optstring(L, 1, NULL);
pid_t pid;
char* argv[] =
{
cmd,
NULL
};
int result = posix_spawn(&pid, argv[0], NULL, NULL, argv, environ);
waitpid(pid, NULL, 0);
if (cmd != NULL)
return luaL_execresult(L, result);
else {
lua_pushboolean(L, result); /* true if there is a shell */
return 1;
}
}
這個(gè)只是臨時(shí)解決方案, 最好還是給作者提一個(gè) issue...