主题:[讨论]Code::Blocks 8.02怎样生成动态链接库?
本人是个菜鸟,刚刚开始学Linux编程,想试着生成调用一个动态链接库,从网上Copy了下面这段代码试试,结果出现图中的错误,请问在Code::blocks 8.02中如何设置,才能使用共享库。
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main(int argc, char **argv) {
void *handle;
double (*cosine)(double);
char *error;
handle = dlopen ("libm.so", RTLD_LAZY);
if (!handle) {
fprintf (stderr, "%s\n", dlerror());
exit(1);
}
dlerror(); /* Clear any existing error */
cosine = dlsym(handle, "cos");
if ((error = dlerror()) != NULL) {
fprintf (stderr, "%s\n", error);
exit(1);
}
printf ("%f\n", (*cosine)(2.0));
dlclose(handle);
return 0;
}
错误都是: Undefined reference to ‘dlXXX’
dlXXX包括dlopen,dlerror,dlsym,dlclose等。
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main(int argc, char **argv) {
void *handle;
double (*cosine)(double);
char *error;
handle = dlopen ("libm.so", RTLD_LAZY);
if (!handle) {
fprintf (stderr, "%s\n", dlerror());
exit(1);
}
dlerror(); /* Clear any existing error */
cosine = dlsym(handle, "cos");
if ((error = dlerror()) != NULL) {
fprintf (stderr, "%s\n", error);
exit(1);
}
printf ("%f\n", (*cosine)(2.0));
dlclose(handle);
return 0;
}
错误都是: Undefined reference to ‘dlXXX’
dlXXX包括dlopen,dlerror,dlsym,dlclose等。