linux 练习五 如何生成库.so及被C和C++程序可用
来源:互联网 发布:新网域名转出到阿里云 编辑:程序博客网 时间:2024/06/06 02:36
题目:实现一个so库文件名称为listupper.so,so文件中实现一个函数,函数名为void upper(const char *src, char *desc),
调用update后将参数src所指向的字符串中所有字符转化为大写字母,结果放入desc字符串中。分别用C语言编写一个程序test1,
调用update后将参数src所指向的字符串中所有字符转化为大写字母,结果放入desc字符串中。分别用C语言编写一个程序test1,
调用libupper.so中的upper函数,用C++语言编写一个程序test2,调用libupper.so中的upper函数
upper.c #include <stdio.h>#include <string.h>#include <stdlib.h>#include "upper.h"int upper(const char * const src, char * const dst,int len){ int i = 0; if((0 == len) || (NULL == src) ||(NULL == dst)) { return -1; } if((src + len) > dst) { return -1; } for(i = 0; i < len; i++) { if((src[i] >= 'a') && (src[i] <= 'z')) { dst[i] = src[i] - ('a' - 'A'); } else { dst[i] = src[i]; } } return 0;}
upper.h
#ifndef __UPPER__#define __UPPER__#include <stdio.h>#ifdef __cplusplusextern "C"{ #endifextern int upper(const char * const src,char * const dst,int len);#ifdef __cplusplus} #endif#endif
//uppertest.c#include <stdio.h>#include <stdlib.h>#include <string.h>#include "upper.h"int main(int argc, char * argv[]){ char *src = "abvf#@13654"; char dst[100]; printf("src=%s\n",src); if(upper(src,dst,strlen(src)) < 0) { perror("upper"); exit(1); } else { printf("dst = %s\n",dst); }}
//uppertestcpp.cpp#include <stdio.h>#include <string.h>#include <iostream>using namespace std;#ifdef __cplusplus //C++程序 兼容调用C库extern "C"{#endif#include "upper.h"#ifdef __cplusplus //C++程序 兼容调用C库}#endifint main(int argc, char * argv[]){ char* s="atesg^*()"; char buf[30]; memset(buf,0,sizeof(buf)); printf("src=%s\n",s); if(upper(s,buf,strlen(s)) < 0) { cout<<"upper error!"<<endl; } else { printf("dst=%s\n",buf); }}
book@ubuntu:~/watchman$ gcc -fpic -shared -o libupper.so upper.c book@ubuntu:~/watchman$ export LD_LIBRARY_PATH=/home/book/watchmanbook@ubuntu:~/watchman$ gcc uppertest.c -L. -lupper -o uppertest.obook@ubuntu:~/watchman$ ./uppertest.o src=abvf#@13654dst = ABVF#@13654book@ubuntu:~/watchman$
book@ubuntu:~/watchman$ g++ uppertestcpp.cpp -L. lupper -o uppertestcppg++: error: lupper: 没有那个文件或目录book@ubuntu:~/watchman$ g++ uppertestcpp.cpp -L. -lupper -o uppertestcppuppertestcpp.cpp: In function ‘int main(int, char**)’:uppertestcpp.cpp:15:13: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] char* s="atesg^*()"; ^book@ubuntu:~/watchman$ ./uppertestcpp src=atesg^*()dst=ATESG^*()book@ubuntu:~/watchman$
阅读全文
0 0
- linux 练习五 如何生成库.so及被C和C++程序可用
- linux-C直接调用SO动态库和生成SO动态库的函数
- Linux下c和cuda混合编译,并生成动态链接库.so和使用
- Linux下c和cuda混合编译,并生成动态链接库.so和使用
- Linux下c和cuda混合编译,并生成动态链接库.so和使用
- c编译生成so 库
- Linux C:生成、调用静态库/动态库,.a,.so
- c 生成.so 文件及 python 调用.so
- linux C 生成与使用 so 文件
- c语言生成静态库.a和动态库.so
- JNI---在c中使用log 和生成so库
- linux c 链接库 so制作及调用
- 如何生成android端可用so
- Linux-(C/C++)动态链接库生成以及使用(libxxx.so)
- Linux-(C/C++)动态链接库生成以及使用(libxxx.so)
- linux下生成so库及调用
- C练习-(五)
- 转:Linux如何生成.so库文件
- Hadoop学习(四)——Hive学习2
- iCare Data Recovery Pro(数据恢复软件)官方破解版V8.0.5.0下载 | icare data recovery 注册码
- 关于android中cavas缩放函数scale后2个参数理解
- Android 使用updatefun 来自动更新
- Python库总结——配置相关库(ConfigParser,logging,sys,getopt)
- linux 练习五 如何生成库.so及被C和C++程序可用
- java分布式架构Dubbo+Zookeeper+ActiveMQ+Redis+Nginx+Netty+Oracle+RocketMQ+Linux,教学视频+项目实战
- 单点登录
- Integer to Roman--LeetCode
- hdu oj 1116 Play on Words(并查集+欧拉路径)
- Ubuntu下如何卸载Mysql数据库
- liunx 利用ssh传输文件
- 【Scikit-Learn 中文文档】密度估计
- 11.26语句 程序流程变量 if & switch 的总结