克隆系统调用OS X不链接-未定义符号 [英] clone system call OS X not linking - undefined symbols

查看:161
本文介绍了克隆系统调用OS X不链接-未定义符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在OS X上使用clone系统调用.这是Unix系统调用,因此应该没有问题,对吧?我已经成功尝试使用forkvfork和其他类似功能.这是我正在尝试的程序:

#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;
}

使用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

请随意忽略这些评论,因为我只是在学习.还有一件事..如果我尝试在参数中传递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.)

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

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

解决方案

clone是特定于Linux的,因此对于OS X,您受困于fork,或者如果可以管理的话,则使用线程. >

I would like to use the clone system call on OS X. It's a Unix system call so it shouldn't be a problem, right? I have successfully tried using fork, vfork and other similar functions. Here is the program I'm trying:

#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;
}

Compiling with gcc -o test my_file.c gives:

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

Feel free to ignore the comments, since I'm just learning. And one more thing.. if I try to pass CLONE_VM in the arguments it doesn't even compile giving me the error:

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.)

Am I missing an #include? If so, which one?

What am I doing wrong and how to fix it?

解决方案

clone is specific to Linux, so for OS X you're stuck with fork, or use threads if you can manage with that.

这篇关于克隆系统调用OS X不链接-未定义符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆