实现一个so库文件名称为libupper.so提供给c/cpp调用

2015-07-20 17:07:13 ? 作者: ? 浏览: 3

要求:

实现一个so库文件名称为libupper.so,so文件中实现一个函数,函数名为void upper(const char *src, char *desc).

调用upper后将参数src所执行的字符串所有字符转化为大写字母,结构放入到desc字符串中。分别用c语言编写一个

程序test1,调用libupper.so中的upper函数。用c++语言辩词额一个程序test2,调用libupper.so中的upper函数。

//upper.c

?

#include 
  
   
void upper(const char *src, char *desc)
{
    int len = strlen(src);//获取字符串长度
    int i = 0;
    for (; i < len; i++)
    {
        if ((src[i]>='a')&&(src[i]<='z'))//如果为小写
        {
            desc[i] = src[i] - 0x20;//转化为大写
        } else
        {
            desc[i] = src[i];
        }
    }
}

  
//upper.h

?

?

#ifndef UPPER_H_
#define UPPER_H_
#ifdef __cplusplus
extern "C"{//c,cpp混合编程
#endif
void upper(const char *src, char *desc);
#ifdef __cplusplus
}
#endif
#endif /* UPPER_H_ */
//makefile

?

?

.SUFFIXES: .c .o
CC=gcc
SRCS=upper.c
    
OBJS=$(SRCS:.c=.o)
EXEC=libupper.so
all: $(OBJS)
    $(CC) -shared -o $(EXEC) $(OBJS) 
    @echo '-------------ok--------------'
.c.o:
    $(CC) -Wall -g -fPIC -o $@ -c $< 
clean:
    rm -f $(OBJS)
    rm -f core*
//test1.c test2.cpp

?

?

#include 
  
   
#include 
   
     #include 
    
      #include "upper.h" int main() { const char *s = "abc123def"; char buf[100]; memset(buf, 0, sizeof(buf)); upper(s, buf);//将s转化为大写字符串 printf("%s\n", buf); return 0; } 
    
   
  

?

make 编译生成libupper.so
wuyingqiang@ty-kf1:~/1$ ls Makefile test1.c test2.cpp upper.c upper.h
wuyingqiang@ty-kf1:~/1$ make
gcc -Wall -g -fPIC -o upper.o -c upper.c
gcc -shared -o libupper.so upper.o -------------ok--------------

gcc test1.c -o test1 -L. -lupper

g++ test2.cpp -o test2 -L. -lupper

执行程序的时候,总是报错找不到库文件。 wuyingqiang@ty-kf1:~/1$ ./test1 ./test1: error while loading shared libraries: libupper.so: cannot open shared object file: No such file or directory
解决方法: cd 回到宿主目录 vim .bash_profile
PATH=$PATH:$HOME/bin:. export PATH export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
然后保存文件
. .bash_profile 使其生效。




?

-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: