OS X 上的未定义符号“_clone”
Undefined symbol "_clone" on OS X
代码:
#include <stdio.h>
#include <sched.h>
#include <stdlib.h>
#include <sys/wait.h>
#define _GNU_SOURCE
void *stack_memory()
{
const int stackSize = 65536;
void* stack = (void*)malloc(stackSize);
if (stack == NULL) {
printf("%s\n", "Cannot allocate memory \n");
exit(EXIT_FAILURE);
}
return stack;
}
int jail(void *args)
{
printf("Hello !! - child \n");
return EXIT_SUCCESS;
}
int main()
{
printf("%s\n", "Hello, world! - parent");
clone(jail, stack_memory(), SIGCHLD, 0);
return EXIT_SUCCESS;
}
错误:
Undefined symbols for architecture x86_64: "_clone", referenced
from:
_main in docker-4f3ae8.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code
1 (use -v to see invocation)
Linux 没有在符号前加上前缀 _
所以你没有使用 Linux.
但是 clone(2)
系统调用是 Linux-specific,根据 its man page。
clone() is Linux-specific and should not be used in programs intended
to be portable.
可能您正在使用 OS X 或其他东西。而且您正在编译为 C,因此调用 un-declared 函数不是 compile-time 错误(只是一个大警告)。这就是为什么它是链接器错误而不是 compile-time 错误(并且您忽略了编译器警告。)
顺便说一句,#define _GNU_SOURCE
after including header files 是没有意义的。您必须定义 feature-request 宏 before 包括 headers 以让它们为 GNU-only 函数定义原型,以防尚未默认的情况。
代码:
#include <stdio.h>
#include <sched.h>
#include <stdlib.h>
#include <sys/wait.h>
#define _GNU_SOURCE
void *stack_memory()
{
const int stackSize = 65536;
void* stack = (void*)malloc(stackSize);
if (stack == NULL) {
printf("%s\n", "Cannot allocate memory \n");
exit(EXIT_FAILURE);
}
return stack;
}
int jail(void *args)
{
printf("Hello !! - child \n");
return EXIT_SUCCESS;
}
int main()
{
printf("%s\n", "Hello, world! - parent");
clone(jail, stack_memory(), SIGCHLD, 0);
return EXIT_SUCCESS;
}
错误:
Undefined symbols for architecture x86_64: "_clone", referenced from: _main in docker-4f3ae8.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
Linux 没有在符号前加上前缀 _
所以你没有使用 Linux.
但是 clone(2)
系统调用是 Linux-specific,根据 its man page。
clone() is Linux-specific and should not be used in programs intended to be portable.
可能您正在使用 OS X 或其他东西。而且您正在编译为 C,因此调用 un-declared 函数不是 compile-time 错误(只是一个大警告)。这就是为什么它是链接器错误而不是 compile-time 错误(并且您忽略了编译器警告。)
顺便说一句,#define _GNU_SOURCE
after including header files 是没有意义的。您必须定义 feature-request 宏 before 包括 headers 以让它们为 GNU-only 函数定义原型,以防尚未默认的情况。