ubuntu中c的编译问题(有报错)

源代码如下:

 #include "/usr/local/include/pbc/pbc.h"
#include "/usr/local/include/pbc/pbc_test.h"
#include <stdio.h>
#include<string.h>
#include<time.h>



int main(int argc, char **argv) {
    pairing_t pairing;

    pbc_demo_pairing_init(pairing, argc, argv);


    double th1, th2, th_1, th_2, ta1, ta2, tm1, tm2, tm_1, tm_2, tp1, tp2, te1, te2;

    element_t x, x2, h;
    element_t P, H, P1, P2;
    element_t Q;


    element_init_Zr(x, pairing);
    element_init_Zr(x2, pairing);
    element_init_Zr(h, pairing);
    element_init_G1(P, pairing);
    element_init_G1(P1, pairing);
    element_init_G1(P2, pairing);
    element_init_G1(H, pairing);
    element_init_GT(Q, pairing);

    //hash到数
    th1 = pbc_get_time();
    element_from_hash(h, "message", 7);
    th2 = pbc_get_time();

    //hash到点
    th_1 = pbc_get_time();
    element_from_hash(P, "message", 7);
    th_2 = pbc_get_time();


    element_random(x);
    element_random(x2);
    element_random(P);
    element_random(P1);
    element_random(P2);
    element_random(H);

    //点加
    ta1 = pbc_get_time();
    element_add(P, P, H);
    ta2 = pbc_get_time();

    //数和点的乘法
    tm1 = pbc_get_time();
    element_mul_zn(P, P, x);
    tm2 = pbc_get_time();

    //点和点的乘法
    tm_1 = pbc_get_time();
    element_mul_zn(P, P1, P2);
    tm_2 = pbc_get_time();

    //对运算weil pairing
    tp1 = pbc_get_time();
    pairing_apply(Q, P1, P1, pairing);
    tp2 = pbc_get_time();

    //模幂运算modular expone
    te1 = pbc_get_time();
    element_pow_zn(H, P2, x2);
    te2 = pbc_get_time();


    printf("%f\t%f\t%f\t%f\t%f\t%f\t%f\n", th2-th1, th_2-th_1, ta2-ta1, tm2-tm1, tm_2-tm_1, tp2-tp1, te2-te1);


    //FILE *fp;
    //fp=fopen("data.txt", "a");

    //fclose(fp);

    element_clear(x);
    element_clear(P);
    element_clear(H);

    pairing_clear(pairing);

    return 0;
}

先编译运行gcc -Wall -o main main.c -L. -lpbc -lgmp是没有问题的,接下来编译运行./main < /home/gaokai/下载/pbc-0.5.14/param/a.param的时候,便有了以下报错:
./main: error while loading shared libraries: libpbc.so.1: cannot open shared object file: No such file or directory
本人新手,对这一块懂得很少,还请大神详细给我说一下是哪里出了问题,该怎么解决。万分感谢!!!

error while loading shared libraries: libpbc.so.1: cannot open shared object file: No such file or directory

这是一个常见的报错,叫做找不到动态库。

解决方法简单讲,就是设置LD_LIBARRY_PATH环境变量,将libpbc.so.1所在路径添加上即可。

详解:

假如main.c所在的目录为 /path/to/main

你在编译的时候,用“-L.”给出了“-lpbc”的路径,这样编译才可以找到这个库,然后编译通过的,否则也是会报错。

但是编译后,这个./main程序连接的是一个动态库。

你可以用“ldd ./main”查看,会发现它连接的libpbc.so.1 not found。

所以我们要添加一下路径,添加方法:

export LD_LIBRARY_PATH=/path/to/main:$LD_LIBRARY_PATH

注意把/path/to/main改为实际的路径。

然后你再次ldd ./main 看看,是不是有变化?

如果只是这一个问题的话,以上方法99.9%可以解决。

http://deepfuture.iteye.com/blog/595646