linux clone() 函数导致奇怪的编译错误,为什么?
linux clone() function leads to weird compilation error, why?
下面是一个简短的程序,我试图了解 "clone" 函数的实际工作原理。
#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编译它说:
$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
我也试过了
g++ testClone.cpp -lpthread
也不编译。为什么?
这与clone
无关,你对errno
的声明是错误的。请改用 #include <errno.h>
。
下面是一个简短的程序,我试图了解 "clone" 函数的实际工作原理。
#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编译它说:
$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
我也试过了
g++ testClone.cpp -lpthread
也不编译。为什么?
这与clone
无关,你对errno
的声明是错误的。请改用 #include <errno.h>
。