Waiting~G

一起走过的日子

Archive for 十一月, 2014

C++中万能makefile编写

without comments

Makefile中只要把INCLUDE 和LIBS填写好就好;其他的都是万能的;INCLUDE 和LIBS如何填写呢?

其实很简单,INCLUDE 这里填写的是.c文件编译到.o文件过程需要的头文件;LIBS这里填写的是.o到可执行文件过程中需要的依赖动态连接文件

CC = gcc
CXX = g++
CFLAGS = -g -Wall
WBL_PATH = /usr/local/mysql/lib

INCLUDE = profile.pb.cc $(WBL_PATH )/libmysqlclient.a -I/usr/local/mysql-5.0.81-linux-x86_64-glibc23/include

#############################################

# 自动计算文件的依赖性

.%.d: %.cpp
$(CC) $(INCLUDE) -MM $< > $@
@$(CC) $(INCLUDE) -MM $< | sed s/”^”/”\.”/ | sed s/”^\. “/” “/ | \
sed s/”\.o”/”\.d”/ >> $@
%.o: %.cpp
$(CXX) $(CFLAGS) $(INCLUDE) -c $<

.%.d: %.c
$(CC) $(INCLUDE) -MM $< > $@
@$(CC) $(INCLUDE) -MM $< | sed s/”^”/”\.”/ | sed s/”^\. “/” “/ | \
sed s/”\.o”/”\.d”/ >> $@
%.o: %.c
$(CC) $(CFLAGS) $(INCLUDE) -c $<

##############################
EXE_C_TEST=./C_TEST
all : $(EXE_C_TEST)

LIBS:=profile.pb.cc $(WBL_PATH )/libmysqlclient.a -lcppclient -pthread -lrt -lz -lprotobuf

OBJS_C_TEST:=C_TEST.o
DEPS = $(patsubst %.o, .%.d, $(OBJS_MAIN) )
#####################################################################################

$(EXE_C_TEST): $(OBJS_C_TEST)
$(CXX) $(CFLAGS) -o $@ $^ $(LIBS)

install:
echo “install. do nothing…”

clean:
rm -vf *.o .*.d $(EXE_C_TEST)

-include $(DEPS)

Written by Skyma

十一月 26th, 2014 at 12:53 下午

Posted in 学.Linux

C++ PB协议使用

without comments

==========================================
首先看一下下面的pb协议文件,安装protobuffer后进行编译
==========================================
package profiledata;
message  ClomnValue{
required  bytes  qualifier = 1;  // qualifier name
required  bytes  value = 2;  // value
}
message Request{
optional string tbname = 1;      // table name
repeated bytes rowkey = 5;  //row key
repeated ClomnValue filter = 10;   // match column value
enum Operator{
MUST_PASS_ALL=0; // and
MUST_PASS_ONE=1; // or
}
optional  Operator operator = 11; // default MUST_PASS_ALL
optional int32 limit = 12;      // get rows count ,when scan table
}
message  Value{
required  bytes  qualifier = 1;  // qualifier name
optional  fixed64  timestamp = 2;  // timestamp
optional  bytes  value = 3;  // value
}
message  Row{
required  bytes  rowkey = 1;     // row key
repeated  Value  values = 2;     // all values
}
message  Response{
required int32  returnType = 1;   // return code
optional string returnDesc = 2;  //
repeated  Row   result = 4;      //
}

Read the rest of this entry »

Written by Skyma

十一月 26th, 2014 at 12:45 下午

Posted in 学.Linux

C++中对字符串处理的几个封装函数

without comments

1、//去掉字符串左右的空格

string& trim(string &s)
{
if (s.empty())
{
return s;
}
s.erase(0,s.find_first_not_of(” “));
s.erase(s.find_last_not_of(” “) + 1);
return s;
}

2、//字符串分割函数

std::vector<std::string> split(std::string str,std::string pattern)
{
std::string::size_type pos;
std::vector<std::string> result;
str+=pattern;//扩展字符串以方便操作
int size=str.size();

for(int i=0; i<size; i++)
{
pos=str.find(pattern,i);
if(pos<size)
{
std::string s=str.substr(i,pos-i);
result.push_back(s);
i=pos+pattern.size()-1;
}
}
return result;
}

Read the rest of this entry »

Written by Skyma

十一月 26th, 2014 at 12:31 下午

Posted in 学.Linux

C++多线程源码

without comments

#include <sys/types.h>
#include <unistd.h>
//这里多线程执行的函数为多个参数,必须使用结构体传入
struct thread_func_args{
Vec_Res *vec_res;
int threadNum;
};
void* thread_fun(void* arg){
//取出服务对象
thread_func_args *func_args;
func_args = (struct thread_func_args *)arg;
int threadNum = (int)func_args->threadNum;
cout<<“this is test…….”
}
int main(){
const int thread_num = 2;
pthread_t *threads = new pthread_t[thread_num];
thread_func_args func_args_arr[ThreadNum];
for (int i = 0; i < thread_num; ++i){
func_args_arr[i]=func_args;
//注意这里,必须现在外层定义一个数字,然后进行赋值使用,不然每一次循环会覆盖地址。
int ret = pthread_create(&threads[i], NULL, thread_fun, &(func_args_arr[i]));
if (ret){ cout << “thread ” << i << ” creating error !” << endl; }
}
//等待所有线程退出
for (int i = 0; i < thread_num; ++i)
{
pthread_join(threads[i], NULL);
}
delete[] threads;
return 0;
}

Written by Skyma

十一月 26th, 2014 at 12:18 下午

Posted in 学.Linux