C++跨平台例子

跨平台这里就定义了一个宏,如果该宏在windows上则定义,如果在Linux上则不定义。

跨平台开发只需要改一个地方即可:将#define __Win32__ 1注释掉即表示在linux上开发,如果将其保留,则表示在windows上开发 。

同时linux上需要多些一个makefile文件。

[cpp]  view plain  copy
  1. #pragma once  
  2.   
  3. #define __Win32__ 1  
  4.   
  5. #ifdef __Win32__  
  6. #include "targetver.h"  
  7. #include <stdio.h>  
  8. #include <tchar.h>  
  9. #include <iostream>  
  10. using namespace std;  
  11. #include <process.h>  
  12. #include <Windows.h>  
  13. #else  
  14.   
  15. #include <unistd.h>  
  16. #include<pthread.h>  
  17. #include <iostream>    
  18. using namespace std;   
  19.   
  20. #endif  


[cpp]  view plain  copy
  1. #include "common.h"  
  2. #include "OSThread.h"  
  3.   
  4.   
  5. int main(int argc, char* argv[])  
  6. {  
  7.     COSThread osThread;  
  8.     osThread.Start();  
  9.     cout << "press any key to exit\n" << endl;  
  10.     int n;  
  11.     cin >> n;  
  12.     return 0;  
  13. }  

[cpp]  view plain  copy
  1. #pragma once  
  2. #include "common.h"  
  3. class COSThread  
  4. {  
  5. public:  
  6.     COSThread(void);  
  7.     ~COSThread(void);  
  8.   
  9.     bool   Start();  
  10.   
  11. protected:  
  12.   
  13.   
  14. #ifdef __Win32__  
  15.     static unsigned int __stdcall _Entry(void* inThread);  
  16. #elif __linux__  
  17.     static void*    _Entry(void* inThread);  
  18.   
  19.  #endif  
  20.   
  21.   
  22. private:  
  23.   
  24. #ifdef __Win32__  
  25.     unsigned int          fThreadID;  
  26. #elif __linux__  
  27.     pthread_t       fThreadID;  
  28. #endif  
  29.   
  30. };  

[cpp]  view plain  copy
  1. #include "OSThread.h"  
  2.   
  3.   
  4. COSThread::COSThread(void)  
  5. {  
  6. }  
  7.   
  8.   
  9. COSThread::~COSThread(void)  
  10. {  
  11. }  
  12.   
  13. bool COSThread::Start()  
  14. {  
  15. #ifdef __Win32__  
  16.     unsigned int theId = 0; // We don't care about the identifier  
  17.     fThreadID = _beginthreadex( NULL,   // Inherit security  
  18.         0,      // Inherit stack size  
  19.         _Entry, // Entry function  
  20.         (void*)this,    // Entry arg  
  21.         0,      // Begin executing immediately  
  22.         &theId );  
  23. #elif __linux__  
  24.      pthread_attr_t* theAttrP = NULL;  
  25.     int err = pthread_create((pthread_t*)&fThreadID, theAttrP, _Entry, (void*)this);  
  26. #endif  
  27.     return true;  
  28. }  
  29.   
  30.   
  31. #ifdef __Win32__  
  32. unsigned int __stdcall COSThread::_Entry( void* inThread )  
  33. {  
  34.     int nCOunt = 0;  
  35.     while(true)  
  36.     {  
  37.         cout << "value:" << nCOunt++ << endl;  
  38.         Sleep(1000);  
  39.     }  
  40. }  
  41. #endif  
  42.   
  43. #ifdef __linux__  
  44. void* COSThread::_Entry( void* inThread )  
  45. {  
  46.     int nCOunt = 0;  
  47.     while(true)  
  48.     {  
  49.         cout << "value:" << nCOunt++ << endl;  
  50.         ::usleep(1000 * 1000);  
  51.     }  
  52.       
  53. }  
  54.   
  55. #endif  

[cpp]  view plain  copy
  1. main.out:  
  2.     g++ test123.cpp OSThread.cpp common.h -lpthread -o a.out  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值