Midaef
Midaef

Reputation: 13

Undefined symbol "_clone" on OS X

Code:

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

Error:

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)

Upvotes: 1

Views: 192

Answers (1)

Peter Cordes
Peter Cordes

Reputation: 364468

Linux doesn't prefix symbols with a leading _ so you're not using Linux.

But the clone(2) system call is Linux-specific, according to its man page.

clone() is Linux-specific and should not be used in programs intended to be portable.


Probably you're using OS X or something. And you're compiling as C, so calling an un-declared function isn't a compile-time error (just a big warning). This is why it's a linker error instead of a compile-time error (and you ignored compiler warnings.)

And BTW, #define _GNU_SOURCE after including header files is pointless. You have to define feature-request macros before including headers to get them to define prototypes for GNU-only functions in cases where that's not already the default.

Upvotes: 0

Related Questions