Search code examples
linuxgcccompilationclone

linux clone() function leads to weird compilation error, why?


I've got a short program which is below, I tried to learn how clone function actually works.

#include<stdio.h>
#include<sched.h>
#include<unistd.h>
#include<sys/types.h>

extern int errno;

int f(void*arg)
{
    pid_t pid=getpid();
    printf("child pid=%d\n",pid);
}
char buf[1024];
int main()
{
    int ret=clone(f,buf,CLONE_VM|CLONE_VFORK,NULL);
    if(ret==-1){
        printf("%d\n",errno);
        return 1;
    }
    printf("father pid=%d\n",getpid());
    return 0;
}

g++4.1.2 compiles it and say:

$ g++ testClone.cpp
/usr/bin/ld: errno: TLS definition in /lib64/libc.so.6 section .tbss mismatches non-TLS reference in /tmp/ccihZbuv.o
/lib64/libc.so.6: could not read symbols: Bad value
collect2: ld returned 1 exit status

I also tried

g++ testClone.cpp -lpthread

Doesn't compile either. Why?


Solution

  • This has nothing to do with clone, you declaration of errno is incorrect. Use #include <errno.h> instead.