【转载】ACE6+VS2010编译

转载自:http://blog.csdn.net/caoren642424136/article/details/6818689

在此仅为个人备份。

 

原文:

vs2010 ACE-6.0.0

 

2. 设置环境变量

添加环境变量:ACE_ROOT="F:\ACE\ACE_wrappers"  这是ACE解压后的目录

添加环境变量:Path=“%ACE_ROOT%\lib”

3. 编译ACE,生成dll

ACE_ROOT\ace目录下新建config.h文件,文件内容:

  1. // config.h  
  2. #define ACE_HAS_STANDARD_CPP_LIBRARY 1 // 用于标准C++跨平台  
  3. #include "ace/config-win32.h" // 在WIN32环境下使用ACE  
  4. #define ACE_USE_WCHAR  
  5. #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写的代码

View Code
  1     // serverMain.cpp  
  2       
  3     // 如果是WIN32平台上报错  
  4     // f:\ace\ace_wrappers\ace\config-win32-common.h(23): fatal error C1189: #error :  Please define WIN32 in your project settings.  
  5     // 可以文件头添加WIN32的预定义解决  
  6     #ifndef WIN32  
  7     #define WIN32  
  8     #endif  
  9       
 10     // 针对Debug版和Release版进行不同预处理  
 11     #ifdef _DEBUG  
 12     #pragma comment(lib,"ACEd.lib")  
 13     #else  
 14     #pragma comment(lib,"ACE.lib")  
 15     #endif  
 16       
 17     #include <ace\Log_Msg.h>  
 18     #include <ace\Time_Value.h>  
 19     #include <ace\SOCK_Acceptor.h>  
 20     #include <ace\SOCK_Stream.h>  
 21     #include <ace\OS_NS_stdlib.h>  
 22       
 23     #define SIZE_DATA 18  
 24     #define SIZE_BUF 1024  
 25     #define NO_ITERATIONS 5  
 26       
 27     class Server  
 28     {  
 29     public:  
 30         Server (int port): server_addr_(port),peer_acceptor_(server_addr_)  
 31         {  
 32             data_buf_= new char[SIZE_BUF];  
 33         }  
 34           
 35         //Handle the connection once it has been established. Here the  
 36         //connection is handled by reading SIZE_DATA amount of data from the  
 37         //remote and then closing the connection stream down.  
 38         int handle_connection()  
 39         {  
 40             // Read data from client  
 41             for(int i=0;i<NO_ITERATIONS;i++)  
 42             {  
 43                 int byte_count=0;  
 44                 if( (byte_count=new_stream_.recv_n (data_buf_, SIZE_DATA, 0))==-1)  
 45                     ACE_ERROR ((LM_ERROR, "%p\n", "Error in recv"));  
 46                 else  
 47                 {  
 48                     data_buf_[byte_count]=0;  
 49                     ACE_DEBUG((LM_DEBUG,"Server received %s \n",data_buf_));  
 50                 }  
 51             }  
 52             // Close new endpoint  
 53             if (new_stream_.close () == -1)  
 54                 ACE_ERROR ((LM_ERROR, "%p\n", "close"));  
 55             return 0;  
 56         }  
 57           
 58         //Use the acceptor component peer_acceptor_ to accept the connection  
 59         //into the underlying stream new_stream_. After the connection has been  
 60         //established call the handle_connection() method.  
 61         int accept_connections ()  
 62         {  
 63             if (peer_acceptor_.get_local_addr (server_addr_) == -1)  
 64                 ACE_ERROR_RETURN ((LM_ERROR,"%p\n","Error in get_local_addr"),1);  
 65             ACE_DEBUG ((LM_DEBUG,"Starting server at port %d\n",  
 66                 server_addr_.get_port_number ()));  
 67             // Performs the iterative server activities.  
 68             while(1)  
 69             {  
 70                 ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT);  
 71                 if (peer_acceptor_.accept (new_stream_, &client_addr_, &timeout)== -1)  
 72                 {  
 73                     ACE_ERROR ((LM_ERROR, "%p\n", "accept"));  
 74                     continue;  
 75                 }  
 76                 else  
 77                 {  
 78                     ACE_DEBUG((LM_DEBUG,  
 79                         "Connection established with remote %s:%d\n",  
 80                         client_addr_.get_host_name(),client_addr_.get_port_number()));  
 81                     //Handle the connection  
 82                     handle_connection();  
 83                 }  
 84             }  
 85         }  
 86     private:  
 87         char *data_buf_;  
 88         ACE_INET_Addr server_addr_;  
 89         ACE_INET_Addr client_addr_;  
 90         ACE_SOCK_Acceptor peer_acceptor_;  
 91         ACE_SOCK_Stream new_stream_;  
 92     };  
 93       
 94     int main (int argc, char *argv[])  
 95     {  
 96         if(argc<2)  
 97         {  
 98             ACE_ERROR((LM_ERROR,"Usage %s <port_num>", argv[0]));  
 99             ACE_OS::exit(1);  
100         }  
101         Server server(ACE_OS::atoi(argv[1]));  
102         server.accept_connections();  
103         return 0;  
104     }  

 

 

自己补充:

在VS2010->[View]->[Property Manager] 随便打开哪个配置,找到一个[Microsoft.Cpp.Win32.User], 在[Common Properties]第2行[User Macros]中点[Add Macro]在[Name]中填入名字(如上文中的“ACE_ROOT”), 在Value:中填入ACE的绝对地址(如上文中的F:\ACE\ACE_wrappers)。(我没有点选下面的"Set this macro as an environment variable in the build ...."选项)。

另:我编译好的.dll , 只有拷贝到自己的工程项目的工作目录内,才可以找到“Aced.dll”, 现在还不清楚为什么。 否则就找不到,但是却能识别Aced.lib的位置。

 

 

posted @ 2012-08-31 14:52  TxnLoop  Views(2592)  Comments(0)    收藏  举报