C++示例
Linux C++ template使用示例:
#include <iostream> #include <cstring> using namespace std; template <class T> T& swaps(T &a,T &b,int n){ for(int i = 0;i<n;i++){ a[i] = a[i] + b[i]; cout<<"i:"<<i<<" "<<a[i]<<endl; } return a; } int main(void){ int a[5] = {1,2,3,4,5}; int b[5] = {100,200,300,400,500},*p; for(int i = 0;i<2;i++){ p = swaps(a,b,2); } for(;p<a+5;p++){ cout<<*p<<endl; } return 0; } //out: i:0 101 i:1 202 i:0 201 i:1 402 201 402 3 4 5
Linux shell生成动态字符串
cmd
#!/bin/sh cn=10 dir="/data/bin/dksCmd" lns="dksCmd" for((i=1;i<=cn;i++)); do tp=$(printf "%02d" $i) cmd_src=${dir}${tp} lnst=${lns}${tp} echo $cmd_src $lnst touch $cmd_src chmod +x $cmd_src ln -s $cmd_src $lnst done
lib
#!/bin/sh cn=10 lib_dir="/data/lib/libdks" lns="libdks" so_suffix=".so" for((i=1;i<=cn;i++)); do tp=$(printf "%02d" $i) lib_src=${lib_dir}${tp}${so_suffix} lnst=${lns}${tp}${so_suffix} echo $lib_src $lnst touch $lib_src chmod +x $lib_src ln -s $lib_src $lnst done
Linux 生成动态so并调用
add.c //库的源文件
int dks_add(int a,int b){ return a+b; }
main.c //main源文件
#include <stdio.h> #include <stdlib.h> int main(void){ int a = 20; int b = 30; int c = dks_add(a,b); printf("line:%d c:%d\n",__LINE__,c); return 0; }
bash编译命令
gcc -fPIC -shared add.c -o libadd01.so
gcc -o main main.c -L./ -ladd01