2013年Erlang和C++杂七杂八日记

    写这篇杂七杂八日记有如下几个出发点:一、遇到问题是方便备查,提高效率;二、年末了,美好的一年又要过去了,总要留下点什么美好的回忆吧;三、昨天同事在问我问题的时候,说了一句至关灰常很经典的话“每次问你问题,老是问一点挤一点:)”,同时是一个甜甜的微笑,他的微笑老是那么滴迷人~~;四、其实在好久好久之前我就已经养成作杂记的习惯,只是没有很系统地整理,有点乱。不过整理后仍是那么有点乱,嘿嘿。html

    本“杂七杂八日记”记录了本人13年和Erlang、C++斗争过程当中的一些经验分享,C++是个千年老妖,Erlang这家伙也不嫩,也有20几岁了,都是很厉害的角色。本文将以“三板斧”的形式展开:安装配置、经验分享、参考资料。linux

    好了,废话说完,开始正题,按国际惯例,先来一条git

 

华丽丽的分割线-------------------------------------------------------------------------------------------------------------------------------------github

 

Erlangweb

安装配置:正则表达式

wget http://www.erlang.org/download/otp_src_R16B.tar.gz -P /usr/local/src

tar zxvf otp_src_R16B.tar.gz -C /usr/local/src
cd otp_src_R16B

./configure --prefix=/usr/local/erlang
注意,若是出现 crypto : No usable OpenSSL found, 须要安装OpenSSL: yum install openssl-devel
make 
make install

安装安装路径经过--prefix指定

安装好测试
ln -s /usr/local/erlang/bin/erl /usr/local/bin/erl
ln -s /usr/local/erlang/bin/erlc /usr/local/bin/erlc (若是不执行之后有可能会报错erlc: Command not found)
# erl
Erlang R16B (erts-5.10.1) [source] [64-bit] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false]shell

Eshell V5.10.1 (abort with ^G)
1> io:format("Hello World!\n").
Hello World!
ok
2>bash

 

Linux GUI下erlang没法启动toolbar的缘由
By admin | 2013 年 3 月 14 日 | Erlang, Linux 评论关闭
Erlang toolbar须要wxwidget,因此若是系统没有安装wxwidget将会致使toolbar没法启动:服务器

yum install wx*websocket

另外,有一些gui工具是使用tcl开发,建议在开发环境将tcl相关的工具所有安装上:

yum install tcl*

 

安装基于Eclipse的Erlang开发环境(Erlide)

安装Eclipse

下载地址:http://www.eclipse.org/downloads/,选择下载 Eclipse Classic 4.2, 182 MB

tar zxvf eclipse-SDK-4.2-linux-gtk.tar.gz
解压以后进入目录eclipse,直接打开eclipse可执行文件,便可使用。

安装Erlide

官方的安装文档:http://erlide.org/#installation

简单翻译意思以下:

安装Erlang R14B或更高版本,若是在Windows系统中,安装路径中不能有空格。
安装Eclipse3.6或更高版本。
若是您的网络使用一个代理服务器链接到互联网,请填写在相应的数据接口:Window → Preferences → General → Network connections
方法一:
打开Eclipse打开 Help → Install new software... ,在该对话框中输入http://erlide.org/update 回车。 按照对话框的提示选择须要安装的目标后重启Eclipse。
重启Eclipse以后,打开Windows → Erlang → Installed runtimes,在右侧点击 Add → Borewse...,浏览到Erlang安装目录。例如我这里选择的是:/usr/local/erlang/lib/erlang

方法二:
从这里直接下载安装文件,而后直接扔到features和plugins目录中,重启eclipse
http://sourceforge.net/projects/erlide/files/update/

到这里安装就所有完成来。

 

ZeroMQ 安装手册:
http://yifangyou.blog.51cto.com/900206/617212

wget http://download.zeromq.org/zeromq-3.2.2.tar.gz
tar -zxvf zeromq-3.2.2.tar.gz
cd zeromq-3.2.2
./configure --prefix=/usr/local/zeromq
make & make install

 

-----------------------------------------------------------

erlzmq 安装:(准确的说应该是编译)靠谱的作法应该是利用EMakefile来编译,执行shell命令: erl -make http://www.cnblogs.com/samis/articles/1841189.html
https://github.com/zeromq/erlzmq2

git clone https://github.com/zeromq/erlzmq2.git
cd erlzmq2
make

 

而后把ebin下的*.beam文件COPY到项目的ebin目录
把priv目录COPY到项目的priv


错误信息:
usr/bin/env: escript: No such file or directory

解决方案:
ln -s /usr/local/erlang/bin/escript /usr/sbin/escript

--------------------------------------------------------------

 

dump文件

erlang的dump文件能够经过webtool:start()查看

 

erlzmq

错误信息:
The on_load function for module erlzmq_nif returned {error,
{bad_lib,
"Function not found erlzmq_nif:socket/4"}}


解决方案:
从新编译erlzmq库
cd Super_logger/src
make
把 ../ebin目录下的erlzmq相关(erlzmq打头的).beam文件COPY到目标项目的 ebin目录

 

escript

错误信息:
usr/bin/env: escript: No such file or directory


解决方案:
ln -s /usr/local/erlang/bin/escript /usr/sbin/escript


http://blog.csdn.net/lijiecong/article/details/7107061

 

protobuf

库:https://github.com/basho/erlang_protobuffs

编译库,注意不是用 erl -make ,是用自带的make,它依赖于rebar

git clone https://github.com/basho/erlang_protobuffs.git
cd erlang_protobuffs
make

 

编译.proto文件
cd ebin
erl
protobuffs_compile:scan_file("simple.proto").

把生成的 simple_pb.beam 拷贝到 ebin 目录,simple_pb.hrl 拷贝到 include 目录

 

RabbitMQ

http://www.rabbitmq.com/

Erlang安装(RabbitMQ是用Erlang写的)
详见文档:Erlang/Erlang安装

xmlto (rabbitmq 编译安装时须要使用)

wget https://fedorahosted.org/releases/x/m/xmlto/xmlto-0.0.25.tar.gz --no-check-certificate
tar -zxf xmlto-0.0.25.tar.gz
cd xmlto-0.0.25
./configure --prefix=/usr/local/xmlto
make && make install

 

RabbitMQ安装

wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.1.3/rabbitmq-server-3.1.3.tar.gz
tar -zxf rabbitmq-server-3.1.3.tar.gz
cd rabbitmq-server-3.1.3
make
make TARGET_DIR=/usr/local/rabbitmq SBIN_DIR=/usr/local/rabbitmq/sbin MAN_DIR=/usr/local/rabbitmq/man install

 

注意在make install 时使用root权限
若是出现错误:xmlto: command not found,能够尝试从新安装xmlto: yum -y install xmlto
若是出现错误:/bin/sh: escript: command not found,能够尝试: ln -s /usr/local/erlang/bin/escript /usr/sbin/escript


启动RabbitMQ
cd /usr/local/rabbitmq/sbin
./rabbitmq-server start
或设置后台运行 nohup ./rabbitmq-server start > nohup.out&
成功启动后会有日下提示:
Activating RabbitMQ plugins ...
注意:RabbitMQ使用了5672端口

错误:
** Reason for termination ==
** {'module could not be loaded',
[{rabbit_net,send,[{file,"src/rabbitmq/amqp_network_connection.erl"},{line,169}]},
terminate: {{undef,
[{rabbit_net,send,

 

解决方案:

启动erlang 的,须要指定rabbitmq的库路径

erl.exe -pz ./ebin -pa ./ebin -s amqp_client -pa ./ebin/rabbit_common -s toolbar -sasl errlog_type error
就是这一段: -pa ./ebin/rabbit_common

 

CPU负载高问题

CPU负载高是正常的,Erlang就是很是占CPU的。Erlang自己在运行到多核就比较优秀,
以前我作过的项目,测试GameServer的时候,占用CPU达到了2400%,几乎把全部的核所有用完,响应仍是比较快。


有几个参考能够考虑一下:
+K true 启用epoll
-P 134217727 设置erlang的最大线程数
-smp enable 启用多核支持

http://www.erlang.org/doc/man/erl.html

 

erlang优点在于线程消息传递及网络层处理,业务逻辑比较弱。

以前余峰在一篇文章叫C1000K,也就是单机支持100万链接,这个也值得参考。

 

在eclipse中修改保存文件后不会自动编译:
在src目录里边执行make all报警告:
make: Warning: File `../ebin/gameserver_app.beam' has modification time 5.2 s in the future
make: Nothing to be done for `all'.
make: warning: Clock skew detected. Your build may be incomplete.

缘由及解决:

在LINUX中编译kernel时出现 make: warning: Clock skew detected. Your build may be incomplete.

缘由是我在更换主板BIOS包时将主板电池的电放了以后没有进入BIOS里面将时间更正过来,因此LINUX在编译时会出现这种错误。

出现这种错误的缘由分析以下:来源:Linux社区 做者:tedsky(感谢万能的百度)

在系统启动时,Linux操做系统将时间从CMOS中读到系统时间变量中,之后修改时间经过修改系统时间实现。为了保持系统时间与CMOS时间的一致 性,Linux每隔一段时间会将系统时间写入CMOS。因为该同步是每隔一段时间(大约是11分钟)进行的,在咱们执行date -s后,若是立刻重起机器,修改时间就有可能没有被写入CMOS,这就是问题的缘由。若是要确保修改生效能够执行以下命令。

解决步骤:

# date //输入命令查看时间

。。。。。。(显示的时间略)

# date -s 08/17/2010 //要修改为的目标时间,要注意顺序是 月/日/年

。。。。。。(显示的时间略)

# date -s 11:20:05 //要修改为的目标时间, 要注意顺序是 时:分:秒

。。。。。。(显示的时间略)

# date //输入命令查看时间

二 8月 17 11:20:05 CST 2010 //目标时间修改正确

# clock -w //这个命令强制把系统时间写入CMOS

# date //输入命令查看时间

二 8月 17 11:20:05 CST 2010 //目标时间修改正确

# make zImage //编译成功

*^-^*…………

 

官网:http://www.erlang.org

 

Misultin - 轻量级HTTP(S)服务器Erlang开发包

https://github.com/ostinelli/misultin

http://www.open-open.com/lib/view/open1340586499264.html

http://erlang-china.org/misc/websocket-and-erlang.html

 

华丽丽的分割线-------------------------------------------------------------------------------------------------------------------------------------

 

C++

Eclipse C++插件

咱们须要一个cdt,这个能够在Eclipse官网下载。

features
plugins
目录下的全部文件分析COPY到Eclipse对应的目录下。重启搞定!

 

PCRE正则库

PCRE是一个轻量级的函数库,比Boost之中的正则表达式库小得多。PCRE十分易用,同时功能也很强大,性能超过了POSIX正则表达式库和一些经典的正则表达式库。

和Boost正则表达式库的比较显示[2],双方的性能相差无几,PCRE在匹配简单字符串时更快,Boost则在匹配较长字符串时胜出---但二者差距很小,考虑到PCRE的大小和易用性,咱们能够认为PCRE更值得考虑。

PCRE被普遍使用在许多开源软件之中,最著名的莫过于Apache HTTP服务器和PHP脚本语言、R脚本语言,此外,正如从其名字所能看到的,PCRE也是perl语言的缺省正则库。

http://www.cnblogs.com/wangshi/archive/2011/11/09/2243702.html

编译command: g++ -o source source.cpp -I /usr/local/include/pcre/ -L /usr/local/lib/pcre/ -lpcrecpp

[1]pcre download and install. http://www.pcre.org/
[2]linux pcrecpp man page. http://linux.die.net/man/3/pcrecpp


安装:

wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.34/pcre-8.34.zip
unzip pcre-8.34.zip
cd pcre-8.34
./configure --prefix=/usr/local/pcre
make
make install

 

ZeroMQ

错误:
error while loading shared libraries: libzmq.so.3: cannot open shared object file: No such file or directory

作一个软连接:
sudo ln -s /usr/local/zeromq/lib/libzmq.so.3.0.0 /usr/lib/libzmq.so.3

若是还不能解决:执行 sudo /sbin/ldconfig 。由于ldconfig为动态连接库管理命令,通常在系统启动时运行,当安装了新的动态连接库后,须要手动执行此命令

-------------------------------------------------------------------------
一、先安装zeromq

wget http://download.zeromq.org/zeromq-3.2.2.tar.gz
tar zxvf zeromq-3.2.2.tar.gz && cd zeromq-3.2.2
./configure --prefix=/usr/local/zeromq
make && make install

作一个软连接(看上面)

二、程序
https://github.com/imatix/zguide/blob/master/examples/C%2B%2B/hwclient.cpp
http://blog.chinaunix.net/uid-23629988-id-3015856.html
http://www.cppfans.org/1018.html
http://c.chinaitlab.com/basic/858621.html

三、编译
/usr/bin/gcc hello.c -o hello -lzmq -L/usr/local/zeromq/lib -I/usr/local/zeromq/include

**********************************************************************************************

错误:zmq.hpp not found!

http://stackoverflow.com/questions/14233887/zmq-compilation-problems-for-c-examples

It looks zmq.hpp has been moved to another git repository beginning with ZMQ 3.2 which is available at https://github.com/zeromq/cppzmq. I have clonned this to same place where a previously extracted the ZMQ's tar ball. Then I created a symbolic link to zmq.hpp from the examples's C++ directory. Then examples compiled fine.

个人作法是把zmq.hpp Copy到/usr/local/zeromq/include目录下。搞定。

zmq.hpp能够从https://github.com/zeromq/cppzmq 下载

 

libconfig

http://www.hyperrealm.com/libconfig/

一、安装

wget http://www.hyperrealm.com/libconfig/libconfig-1.4.9.tar.gz
tar -vxf libconfig-1.4.9.tar.gz
cd libconfig-1.4.9
./configure --prefix=/usr/local/libconfig
make
make install

sudo ln -s /usr/local/libconfig/lib/libconfig++.so.9.1.3 /usr/lib/libconfig++.so.9

 

删除软链接:rm -f /usr/lib/libconfig++.so.9

./Super_logger_client: error while loading shared libraries: libconfig++.so.9: cannot open shared object file: No such file or directory

若是还不能解决能够尝试: sudo ldconfig -v

二、编译、连接时使用

C++程序中使用libconfig库:
包含头文件:#include <libconfig.h++>
指定使用命名空间:using namespace libconfig;
连接时指定:specify ‘-lconfig++’ as an argument to the linker.

/usr/bin/g++ sim_alg.cpp -o sim_alg -L/usr/local/libconfig/lib -I/usr/local/libconfig/include -lconfig++

三、可能遇到的错误:
terminate called after throwing an instance of 'libconfig::FileIOException'
what(): FileIOException

缘由是权限的问题。由于是用root帐号mount的,用普通用户执行就会报此错误。
但奇怪的是在命令行直接执行exe文件却又正常,诡异!!

解决方案:用.sh脚原本执行。下面脚本保存为一个文件,如GameRobotNew.sh
#!/bin/sh
filepath=$(cd "$(dirname "$0")"; pwd)
#echo $filepath
cd $filepath
./Debug/GameRobotNew

 

log4cplus

官网: http://sourceforge.net/projects/log4cplus/files/log4cplus-stable/

 

wget http://downloads.sourceforge.net/project/log4cplus/log4cplus-stable/1.1.2/log4cplus-1.1.2-rc2.zip
unzip log4cplus-1.1.2-rc2.zip
cd log4cplus-1.1.2-rc2
./configure --prefix=/usr/local/log4cplus
make
make install

 

安装成功后将在/usr/local目录或指定的目录下建立include和lib两个子目录及相应文件。其中include目录包含头文件,lib目录包含最终打包生成的静态和动态库。在动态链接log4cplus库时请使用-llog4cplus选项。

 

文档:
http://www.cnblogs.com/keepsimple/archive/2013/05/10/3071309.html
http://www.cppblog.com/API/archive/2011/04/02/143283.html


错误:
./GameRobotLib: error while loading shared libraries: liblog4cplus-1.1.so.7: cannot open shared object file: No such file or directory
作一个软连接:
sudo ln -s /usr/local/log4cplus/lib/liblog4cplus-1.1.so.7 /usr/lib/liblog4cplus-1.1.so.7

若是还不能解决:执行 sudo /sbin/ldconfig 。由于ldconfig为动态连接库管理命令,通常在系统启动时运行,当安装了新的动态连接库后,须要手动执行此命令

 

protobuf

http://code.google.com/p/protobuf/downloads/list

一、安装

wget https://protobuf.googlecode.com/files/protobuf-2.5.0.tar.bz2
tar -jxvf protobuf-2.5.0.tar.bz2
cd protobuf-2.5.0
./configure --prefix=/usr/local/protobuf
make
make install

 

错误:
error while loading shared libraries: libprotobuf.so.8: cannot open shared object file: No such file or directory

作一个软连接:
sudo ln -s /usr/local/protobuf/lib/libprotobuf.so.8.0.0 /usr/lib/libprotobuf.so.8


编译命令:
/usr/bin/g++ helloworldWrite.cpp helloworld.pb.cc -o helloworld -lprotobuf -I/usr/local/protobuf/include -L/usr/local/protobuf/lib
注意:要把生成的*.pb.cc文件链上。


编译.proto文件:/usr/local/protobuf/bin/protoc --cpp_out=. helloworld.proto

相关文章
相关标签/搜索