我们项目组需要用到内存池,用到了ACE,以前只是接触过boost,没有用过ACE,我想先在win下试试ACE,
我的环境是:win7_32bit,vs2010旗舰版,安装ACE-6.2.0
转自http://blog.csdn.net/caoren642424136/article/details/6818689,作者的版本是6.0.0
1. 获取ACE-6.2.0.tar.gz
下载地址:http://download.dre.vanderbilt.edu/
选择full下zip格式的压缩包
2. 设置环境变量
添加环境变量:ACE_ROOT="F:\ACE\ACE_wrappers" 这是ACE解压后的目录
添加环境变量:Path=“%ACE_ROOT%\lib”
3. 编译ACE,生成dll
ACE_ROOT\ace目录下新建config.h文件,文件内容:
- // config.h
- #define ACE_HAS_STANDARD_CPP_LIBRARY 1 // 用于标准C++跨平台
- #include "ace/config-win32.h" // 在WIN32环境下使用ACE
- #define ACE_USE_WCHAR
- #define ACE_HAS_WCHAR // 支持unicode
// config.h
#define ACE_HAS_STANDARD_CPP_LIBRARY 1 // 用于标准C++跨平台
#include "ace/config-win32.h" // 在WIN32环境下使用ACE
#define ACE_USE_WCHAR
#define ACE_HAS_WCHAR // 支持unicode
打开ACE_ROOT\ace目录下项目ace_vc解决方案
vs2010打开ace_vc10.sln
vs2008打开ace_vc9.sln
vs2005打开ace_vc8.sln
用vs2010打开ace_vc10.sln之后,选中其中的一个项目ACE,进入项目-属性菜单,编辑配置属性-VC++ 目录
右侧的 包含目录添加$(ACE_ROOT),库目录添加$(ACE_ROOT)\lib,跟原有内容之间以分号隔开。
编译该项目即可在ACE_ROOT\lib下生成所需要的ACEd.dll和ACEd.lib文件(Debug版本的为ACEd.dll ACEd.lib,Release版本的为ACE.dll ACE.lib)
可以以此对该解决方案下其他项目做同样设置,编译生成自己需要的dll和lib。
4. 找到一段关于ACE的代码,进行编译,检验环境是否可用。
像普通C++项目一样,新建好这个项目之后,还是如同第3步,在项目--属性中,编辑配置属性-VC++ 目录
右侧的 包含目录添加$(ACE_ROOT),库目录添加$(ACE_ROOT)\lib,跟原有内容之间以分号隔开。
然后编译,运行。
5. 附录 《中篇:ACE程序员教程》中一段儿ACE写的代码
- // serverMain.cpp
- // 如果是WIN32平台上报错
- // f:\ace\ace_wrappers\ace\config-win32-common.h(23): fatal error C1189: #error : Please define WIN32 in your project settings.
- // 可以文件头添加WIN32的预定义解决
- #ifndef WIN32
- #define WIN32
- #endif
- // 针对Debug版和Release版进行不同预处理
- #ifdef _DEBUG
- #pragma comment(lib,"ACEd.lib")
- #else
- #pragma comment(lib,"ACE.lib")
- #endif
- #include <ace\Log_Msg.h>
- #include <ace\Time_Value.h>
- #include <ace\SOCK_Acceptor.h>
- #include <ace\SOCK_Stream.h>
- #include <ace\OS_NS_stdlib.h>
- #define SIZE_DATA 18
- #define SIZE_BUF 1024
- #define NO_ITERATIONS 5
- class Server
- {
- public:
- Server (int port): server_addr_(port),peer_acceptor_(server_addr_)
- {
- data_buf_= new char[SIZE_BUF];
- }
- //Handle the connection once it has been established. Here the
- //connection is handled by reading SIZE_DATA amount of data from the
- //remote and then closing the connection stream down.
- int handle_connection()
- {
- // Read data from client
- for(int i=0;i<NO_ITERATIONS;i++)
- {
- int byte_count=0;
- if( (byte_count=new_stream_.recv_n (data_buf_, SIZE_DATA, 0))==-1)
- ACE_ERROR ((LM_ERROR, "%p\n", "Error in recv"));
- else
- {
- data_buf_[byte_count]=0;
- ACE_DEBUG((LM_DEBUG,"Server received %s \n",data_buf_));
- }
- }
- // Close new endpoint
- if (new_stream_.close () == -1)
- ACE_ERROR ((LM_ERROR, "%p\n", "close"));
- return 0;
- }
- //Use the acceptor component peer_acceptor_ to accept the connection
- //into the underlying stream new_stream_. After the connection has been
- //established call the handle_connection() method.
- int accept_connections ()
- {
- if (peer_acceptor_.get_local_addr (server_addr_) == -1)
- ACE_ERROR_RETURN ((LM_ERROR,"%p\n","Error in get_local_addr"),1);
- ACE_DEBUG ((LM_DEBUG,"Starting server at port %d\n",
- server_addr_.get_port_number ()));
- // Performs the iterative server activities.
- while(1)
- {
- ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT);
- if (peer_acceptor_.accept (new_stream_, &client_addr_, &timeout)== -1)
- {
- ACE_ERROR ((LM_ERROR, "%p\n", "accept"));
- continue;
- }
- else
- {
- ACE_DEBUG((LM_DEBUG,
- "Connection established with remote %s:%d\n",
- client_addr_.get_host_name(),client_addr_.get_port_number()));
- //Handle the connection
- handle_connection();
- }
- }
- }
- private:
- char *data_buf_;
- ACE_INET_Addr server_addr_;
- ACE_INET_Addr client_addr_;
- ACE_SOCK_Acceptor peer_acceptor_;
- ACE_SOCK_Stream new_stream_;
- };
- int main (int argc, char *argv[])
- {
- if(argc<2)
- {
- ACE_ERROR((LM_ERROR,"Usage %s <port_num>", argv[0]));
- ACE_OS::exit(1);
- }
- Server server(ACE_OS::atoi(argv[1]));
- server.accept_connections();
- return 0;
- }