如何使这个C程序编译?
当我键入gcc gprof helper.c来编译程序时,会出现以下错误:如何使这个C程序编译?,c,linux,gcc,compilation,C,Linux,Gcc,Compilation,当我键入gcc gprof helper.c来编译程序时,会出现以下错误: gprof-helper.c: In function `wooinit': gprof-helper.c:38: error: `RTLD_NEXT' undeclared (first use in this function) gprof-helper.c:38: error: (Each undeclared identifier is reported only once gprof-helper.c:38:
gprof-helper.c: In function `wooinit':
gprof-helper.c:38: error: `RTLD_NEXT' undeclared (first use in this function)
gprof-helper.c:38: error: (Each undeclared identifier is reported only once
gprof-helper.c:38: error: for each function it appears in.)
这是程序文件:
#define _GNU_SOURCE
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <pthread.h>
static void * wrapper_routine(void *);
/* Original pthread function */
static int (*pthread_create_orig)(pthread_t *__restrict,
__const pthread_attr_t *__restrict,
void *(*)(void *),
void *__restrict) = NULL;
/* Library initialization function */
void wooinit(void) __attribute__((constructor));
void wooinit(void)
{
pthread_create_orig = dlsym(RTLD_NEXT, "pthread_create");
fprintf(stderr, "pthreads: using profiling hooks for gprof\n");
if(pthread_create_orig == NULL)
{
char *error = dlerror();
if(error == NULL)
{
error = "pthread_create is NULL";
}
fprintf(stderr, "%s\n", error);
exit(EXIT_FAILURE);
}
}
/* Our data structure passed to the wrapper */
typedef struct wrapper_s
{
void * (*start_routine)(void *);
void * arg;
pthread_mutex_t lock;
pthread_cond_t wait;
struct itimerval itimer;
} wrapper_t;
/* The wrapper function in charge for setting the itimer value */
static void * wrapper_routine(void * data)
{
/* Put user data in thread-local variables */
void * (*start_routine)(void *) = ((wrapper_t*)data)->start_routine;
void * arg = ((wrapper_t*)data)->arg;
/* Set the profile timer value */
setitimer(ITIMER_PROF, &((wrapper_t*)data)->itimer, NULL);
/* Tell the calling thread that we don't need its data anymore */
pthread_mutex_lock(&((wrapper_t*)data)->lock);
pthread_cond_signal(&((wrapper_t*)data)->wait);
pthread_mutex_unlock(&((wrapper_t*)data)->lock);
/* Call the real function */
return start_routine(arg);
}
/* Our wrapper function for the real pthread_create() */
int pthread_create(pthread_t *__restrict thread,
__const pthread_attr_t *__restrict attr,
void * (*start_routine)(void *),
void *__restrict arg)
{
wrapper_t wrapper_data;
int i_return;
/* Initialize the wrapper structure */
wrapper_data.start_routine = start_routine;
wrapper_data.arg = arg;
getitimer(ITIMER_PROF, &wrapper_data.itimer);
pthread_cond_init(&wrapper_data.wait, NULL);
pthread_mutex_init(&wrapper_data.lock, NULL);
pthread_mutex_lock(&wrapper_data.lock);
/* The real pthread_create call */
i_return = pthread_create_orig(thread,
attr,
&wrapper_routine,
&wrapper_data);
/* If the thread was successfully spawned, wait for the data
* to be released */
if(i_return == 0)
{
pthread_cond_wait(&wrapper_data.wait, &wrapper_data.lock);
}
pthread_mutex_unlock(&wrapper_data.lock);
pthread_mutex_destroy(&wrapper_data.lock);
pthread_cond_destroy(&wrapper_data.wait);
return i_return;
}
定义GNU源
#包括
#包括
#包括
#包括
#包括
静态void*wrapper_例程(void*);
/*原始pthread函数*/
静态int(*pthread\u create\u orig)(pthread\u t*\u restrict,
__常量pthread\u attr\u t*\u restrict,
无效*(*)(无效*),
void*_restrict)=NULL;
/*库初始化函数*/
void wooinit(void)uuuu属性_uuuu((构造函数));
void wooinit(void)
{
pthread_create_orig=dlsym(RTLD_NEXT,“pthread_create”);
fprintf(stderr,“pthreads:使用gprof的分析钩子”\n);
if(pthread\u create\u orig==NULL)
{
char*error=dlerror();
如果(错误==NULL)
{
error=“pthread_create为空”;
}
fprintf(stderr,“%s\n”,错误);
退出(退出失败);
}
}
/*我们的数据结构传递给包装器*/
typedef结构包装器
{
无效*(*开始例行程序)(无效*);
void*arg;
pthread_mutex_t lock;
pthread_cond_t wait;
结构itimerval itimer;
}包装纸;
/*包装器函数负责设置itimer值*/
静态void*wrapper_例程(void*data)
{
/*将用户数据放入线程局部变量中*/
void*(*start_例程)(void*)=((wrapper_t*)数据)->start_例程;
void*arg=((包装)数据)->arg;
/*设置配置文件计时器值*/
setitimer(ITIMER,PROF,&(wrapper,t*)数据)->ITIMER,NULL;
/*告诉调用线程我们不再需要它的数据*/
pthread_mutex_lock(&((wrapper_t*)数据)->lock;
pthread_cond_信号(&((wrapper_t*)数据)->等待;
pthread_mutex_unlock(&((wrapper_t*)数据)->lock;
/*调用实函数*/
返回启动程序(arg);
}
/*真正的pthread_create()的包装函数*/
int pthread_create(pthread_t*\u restrict thread,
__常量pthread\u attr\u t*\u restrict attr,
无效*(*开始例行程序)(无效*),
无效*(限制参数)
{
包装数据;
国际投资回报率;
/*初始化包装器结构*/
包装器_data.start_例程=启动_例程;
包装器_data.arg=arg;
getitimer(ITIMER_PROF和wrapper_data.ITIMER);
pthread_cond_init(&wrapper_data.wait,NULL);
pthread_mutex_init(&wrapper_data.lock,NULL);
pthread_mutex_lock(&wrapper_data.lock);
/*真正的pthread_create调用*/
i\u return=pthread\u create\u orig(线程,
attr,
&你的例行程序,
&包装器(U数据);
/*如果线程已成功生成,请等待数据
*释放*/
if(i_return==0)
{
pthread_cond_wait(&wrapper_data.wait,&wrapper_data.lock);
}
pthread_mutex_unlock(&wrapper_data.lock);
pthread_mutex_destroy(&wrapper_data.lock);
pthread_cond_destroy(&wrapper_data.wait);
返回i_返回;
}
如何消除这些错误?
RTLD\u NEXT
自古以来就在glibc中提供。您是否在构建Linux(或基于glibc的)系统?听起来不像 更新:
RTLD\u NEXT
是一个glibc
扩展。您不能在cygwin
上使用它,因为cygwin
!=<代码>glibc据我所知,
cygwin
也不支持LD_PRELOAD
,因此即使您成功构建了此库,它也不会非常有用。这看起来像是的副本,请参阅(有关此问题的更多详细信息)我想如果我把整个程序文件都包括进来,我会得到更好的答案。那么你为什么不在一小时前问的原始问题中添加“程序文件”的额外细节呢?为什么要开始一个全新的问题?@Phenom:发布整个源代码将包含许多无关的代码。最好发布一个最小的测试用例:只需要足够的代码来演示问题,不要更多。@Phenom:你应该早点说;cygwin不支持RTLD\U NEXT
(如2008年10月的消息)