为了测试,我写了3个文件,test.c, test.h,hello.c分别如下:

//test.h

#ifndef _TEST_H_

#define _TEST_H_
#include <stdio.h> void printHello(); #endif

//test.c

#include "test.h"

void printHello()
{
    printf("HelloWorld\n");
}

#hello.c

#include "test.h"

int main()
{
    printHello();
    return 0;
}

刚开始是这么写的:

gcc -fPIC  -shared test.c -o libtest.so
gcc -L ~/work/test -ltest hello.c -o hello

链接的时候总是出现undefined reference to xxx错误。

 

后来这么写:

gcc test.c -fPIC -shared -o libtest.so
gcc hello.c -L ~/work/test -ltest -o hello

则没有问题了。

 

Posted on 2017-07-04 17:35  小小旅行商  阅读(128)  评论(0编辑  收藏  举报