标签:环境 ++ turn 编写 error obj loading strlen string
1, 编译,使用-shared和-fpic 生成动态链接库#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static void printline(int len)
{
int i;
for(i = 0;i<len;i++)
{
printf("=");
}
printf("\n");
}
void print(char * s)
{
int len = 0;
int i = 0;
if(!s || *s == ‘\0‘)
{
return ;
}
len = strlen(s);
printline(len);
printf("%s\n",s);
printline(len);
}
头文件:test.h
#ifndef __TEST_H__
#define __TEST_H__
void print(char * s);
#endif
编译库文件:
gcc test.c -shared -fpic -o libtest.so
2.编译测试代码
测试代码:main.c
#include "test.h"
int main()
{
char teststr[] = "hello world";
print(teststr);
return 0;
}
编译测试代码
gcc main.c -L./ -ltest -o main
3.运行
当运行时,发现找不到库文件
./main: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory
这个是linux库文件搜索路径的问题,有两个解决方式
root@GFD:~/workspace/so_test# export LD_LIBRARY_PATH=./
root@GFD:~/workspace/so_test# ./main
===========
hello world
===========
标签:环境 ++ turn 编写 error obj loading strlen string
原文地址:http://blog.51cto.com/6306331/2067710