克隆系统调用OS X没有链接 - 未定义的符号

Maj*_*ter 2 c gcc fork clone ld

我想clone在OS X上使用系统调用.这是一个Unix系统调用,所以它不应该是一个问题,对吧?我已经成功地尝试使用fork,vfork以及其他类似的功能.这是我正在尝试的程序:

#include <sched.h>  //Clone resides here
#include <stdlib.h> //standard library
#include <stdio.h>
#include <time.h>
#include <limits.h>
#include <sys/shm.h>
#include <errno.h>
#include <sys/sem.h>
#include <signal.h>
#include <sys/types.h>

int helloWorld();

int main(int argc, char *argv[])
{
    int (*functionPointer)() = &helloWorld; //The first argument of clone accepts a pointer to a function which must return int
    void **childStack = (void**)malloc(1024); //We will give our child 1kB of stack space

    clone(functionPointer, childStack, 0, NULL); //First arugment is the function to be called, second one is our stack, CLONE_VM means to share memory, last NULL PID description

    return 0;
}

int helloWorld()
{
    printf("Hello (clone) world!\r\n");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译gcc -o test my_file.c给出:

Undefined symbols for architecture x86_64:
  "_clone", referenced from:
      _main in ccG3qOjx.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

随意忽略评论,因为我只是在学习.还有一件事..如果我试图传入CLONE_VM参数,它甚至没有编译给我错误:

my_file.c: In function ‘main’:
my_file.c:12: error: ‘CLONE_VM’ undeclared (first use in this function)
my_file.c:12: error: (Each undeclared identifier is reported only once
my_file.c:12: error: for each function it appears in.)
Run Code Online (Sandbox Code Playgroud)

我错过了#include吗?如果是这样,哪一个?

我做错了什么以及如何解决?

Hen*_*rik 6

clone是特定于Linux的,所以对于OS X你会坚持使用fork,或者如果你可以使用它来使用线程.