first_file.cpp

本文介绍了一个简单的Windows应用程序实例,包括窗口类注册、消息处理、文件读写等操作,并针对不同版本的Windows进行了适配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-5572165936844014&dt=1194442938015&lmt=1194190197&format=336x280_as&output=html&correlator=1194442937843&url=file%3A%2F%2F%2FC%3A%2FDocuments%2520and%2520Settings%2Flhh1%2F%E6%A1%8C%E9%9D%A2%2FCLanguage.htm&color_bg=FFFFFF&color_text=000000&color_link=000000&color_url=FFFFFF&color_border=FFFFFF&ad_type=text&ga_vid=583001034.1194442938&ga_sid=1194442938&ga_hid=1942779085&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="allowtransparency"> 
#include <windows.h> 
#include "First_File.h"


#if defined (WIN32)
 #define IS_WIN32 TRUE
#else
 #define IS_WIN32 FALSE
#endif

#define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
#define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
#define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32

HINSTANCE hInst;   // current instance

LPCTSTR lpszAppName  = "MyApp";
LPCTSTR lpszTitle    = "My Application";

BOOL RegisterWin95( CONST WNDCLASS* lpwc );

int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
                      LPTSTR lpCmdLine, int nCmdShow)
{
   MSG      msg;
   HWND     hWnd;
   WNDCLASS wc;

   // Register the main application window class.
   //............................................
   wc.style         = CS_HREDRAW | CS_VREDRAW;
   wc.lpfnWndProc   = (WNDPROC)WndProc;      
   wc.cbClsExtra    = 0;                     
   wc.cbWndExtra    = 0;                     
   wc.hInstance     = hInstance;             
   wc.hIcon         = LoadIcon( hInstance, lpszAppName );
   wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
   wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
   wc.lpszMenuName  = lpszAppName;             
   wc.lpszClassName = lpszAppName;             

   if ( IS_WIN95 )
   {
      if ( !RegisterWin95( &wc ) )
         return( FALSE );
   }
   else if ( !RegisterClass( &wc ) )
      return( FALSE );

   hInst = hInstance;

   // Create the main application window.
   //....................................
   hWnd = CreateWindow( lpszAppName,
                        lpszTitle,   
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT, 0,
                        CW_USEDEFAULT, 0, 
                        NULL,             
                        NULL,             
                        hInstance,        
                        NULL              
                      );

   if ( !hWnd )
      return( FALSE );

   ShowWindow( hWnd, nCmdShow );
   UpdateWindow( hWnd );        

   while( GetMessage( &msg, NULL, 0, 0) )  
   {
      TranslateMessage( &msg );
      DispatchMessage( &msg ); 
   }

   return( msg.wParam );
}


BOOL RegisterWin95( CONST WNDCLASS* lpwc )
{
   WNDCLASSEX wcex;

   wcex.style         = lpwc->style;
   wcex.lpfnWndProc   = lpwc->lpfnWndProc;
   wcex.cbClsExtra    = lpwc->cbClsExtra;
   wcex.cbWndExtra    = lpwc->cbWndExtra;
   wcex.hInstance     = lpwc->hInstance;
   wcex.hIcon         = lpwc->hIcon;
   wcex.hCursor       = lpwc->hCursor;
   wcex.hbrBackground = lpwc->hbrBackground;
   wcex.lpszMenuName  = lpwc->lpszMenuName;
   wcex.lpszClassName = lpwc->lpszClassName;

   // Added elements for Windows 95.
   //...............................
   wcex.cbSize = sizeof(WNDCLASSEX);
   wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName,
                            IMAGE_ICON, 16, 16,
                            LR_DEFAULTCOLOR );
   
   return RegisterClassEx( &wcex );
}

LPCTSTR lpBuffer = "This is the string that will be written to the file.";

LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
static HANDLE hFile;

   switch( uMsg )
   {
      case WM_CREATE :
              {
                 DWORD dwWritten;

                 // Open/create a file.
                 //....................
                 hFile = CreateFile( "FILE.DAT", GENERIC_READ | GENERIC_WRITE,
                                     0, NULL, OPEN_ALWAYS,
                                     FILE_ATTRIBUTE_NORMAL, NULL );

                 // Write a string to the file.
                 //............................
                 WriteFile( hFile, lpBuffer, strlen( lpBuffer )+1,
                            &dwWritten, NULL );
              }
              break;

      case WM_COMMAND :
              switch( LOWORD( wParam ) )
              {
                 case IDM_TEST :
                        {
                           char  szBuf[128];
                           DWORD dwRead;

                           // Make sure the file pointer
                           // is at the beginning of the file.
                           //.................................
                           SetFilePointer( hFile, 0, NULL, FILE_BEGIN );

                           // Read the string from the file and display it.
                           //..............................................
                           if ( ReadFile( hFile, szBuf, strlen( lpBuffer )+1,
                                          &dwRead, NULL ) )
                           {
                              MessageBox( hWnd, szBuf, "Read File", MB_OK );
                           }
                        }
                        break;

                 case IDM_ABOUT :
                        DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
                        break;

                 case IDM_EXIT :
                        DestroyWindow( hWnd );
                        break;
              }
              break;
     
      case WM_DESTROY :
              // Close the file.
              //................
              CloseHandle( hFile );

              PostQuitMessage(0);
              break;

      default :
            return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
   }

   return( 0L );
}


LRESULT CALLBACK About( HWND hDlg,          
                        UINT message,       
                        WPARAM wParam,      
                        LPARAM lParam)
{
   switch (message)
   {
       case WM_INITDIALOG:
               return (TRUE);

       case WM_COMMAND:                             
               if (   LOWORD(wParam) == IDOK        
                   || LOWORD(wParam) == IDCANCEL)   
               {
                       EndDialog(hDlg, TRUE);       
                       return (TRUE);
               }
               break;
   }

   return (FALSE);
}

[1/3] Building CXX object .llt/src_llt/sd8911/hidsp0/testcase/alg/CMakeFiles/src_llt-sd8911-hidsp0-testcase-alg.dir/odsp_test_tx_fde.cpp.obj FAILED: .llt/src_llt/sd8911/hidsp0/testcase/alg/CMakeFiles/src_llt-sd8911-hidsp0-testcase-alg.dir/odsp_test_tx_fde.cpp.obj "D:\Program Files (x86)\mingw64\bin\g++.exe" @.llt\src_llt\sd8911\hidsp0\testcase\alg\CMakeFiles\src_llt-sd8911-hidsp0-testcase-alg.dir\odsp_test_tx_fde.cpp.obj.rsp -MD -MT .llt/src_llt/sd8911/hidsp0/testcase/alg/CMakeFiles/src_llt-sd8911-hidsp0-testcase-alg.dir/odsp_test_tx_fde.cpp.obj -MF .llt\src_llt\sd8911\hidsp0\testcase\alg\CMakeFiles\src_llt-sd8911-hidsp0-testcase-alg.dir\odsp_test_tx_fde.cpp.obj.d -o .llt/src_llt/sd8911/hidsp0/testcase/alg/CMakeFiles/src_llt-sd8911-hidsp0-testcase-alg.dir/odsp_test_tx_fde.cpp.obj -c ../../../src_llt/sd8911/hidsp0/testcase/alg/odsp_test_tx_fde.cpp In file included from ../../../src_llt/sd8911/hidsp0/testcase/alg/odsp_test_tx_fde.hpp:10, from ../../../src_llt/sd8911/hidsp0/testcase/alg/odsp_test_tx_fde.cpp:27: D:/8911/hlt/thirdparty/googletest/googletest-release-1.11.0/googletest/include/gtest/gtest.h:1928: warning: "FAIL" redefined In file included from D:/8911/src/cbb/base/common.h:12, from D:/8911/hlt/src_llt/common/llt_common.hpp:18, from ../../../src_llt/sd8911/hidsp0/testcase/alg/odsp_test_tx_fde.cpp:25: D:/8911/src/cbb/base/error_code.h:144: note: this is the location of the previous definition ../../../src_llt/sd8911/hidsp0/testcase/alg/odsp_test_tx_fde.cpp:41:1: error: expected initializer before 'using' compilation terminated due to -Wfatal-errors. [2/3] Building CXX object .llt/src_llt/sd8911/hidsp0/testcase/CMakeFiles/src_llt-sd8911-hidsp0-testcase.dir/llt_ecs_msa.cpp.obj In file included from D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/types/AnyCast.h:25, from D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/IsEqual.h:21, from D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/ChainingMockHelper.h:22, from D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/mockcpp.hpp:23, from D:/8911/hlt/comm/llt_support/llt_head_files.h:25, from D:/8911/hlt/comm/llt_support/llt_support.h:11, from ../../../src_llt/sd8911/hidsp0/testcase/llt_ecs_msa.cpp:1: D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/Ignore.h: In member function 'bool mockcpp::Ignore::operator==(const mockcpp::Ignore&) const': -- D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/ApiHookHolderFactory.h:32:14: required from 'static mockcpp::ApiHookHolder* mockcpp::ApiHookHolderFactory::create(F*) [with F = unsigned char()]' D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/ApiHookMocker.h:33:48: required from 'mockcpp::InvocationMockBuilderGetter mockcpp::mockAPI(const string&, API*) [with API = unsigned char(); std::__cxx11::string = std::__cxx11::basic_string<char>]' ../../../src_llt/sd8911/hidsp0/testcase/llt_ecs_msa.cpp:568:5: required from here D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/ApiHookGenerator.h:67:33: warning: unused parameter 'api' [-Wunused-parameter] D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/ApiHookGenerator.h: In instantiation of 'static bool mockcpp::ApiHookGenerator<F, 0>::freeApiHook(void*) [with F = unsigned char()]': D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/ApiHookGenerator.h:53:49: recursively required from 'static bool mockcpp::ApiHookGenerator<F, Seq>::freeApiHook(void*) [with F = unsigned char(); unsigned int Seq = 9]' D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/ApiHookGenerator.h:53:49: required from 'static bool mockcpp::ApiHookGenerator<F, Seq>::freeApiHook(void*) [with F = unsigned char(); unsigned int Seq = 10]' D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/ParameterizedApiHookHolder.h:44:47: required from 'mockcpp::ParameterizedApiHookHolder<F>::~ParameterizedApiHookHolder() [with F = unsigned char()]' D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/ParameterizedApiHookHolder.h:42:4: required from here D:/8911/hlt/thirdparty/mockcpp/mockcpp-30e1bf9/include/mockcpp/ApiHookGenerator.h:82:35: warning: unused parameter 'hook' [-Wunused-parameter] ninja: build stopped: subcommand failed. [build] make build end. For more detailed information, please check ./BUILD.LOG. Build Fail! 什么问题
最新发布
07-27
<think>我们面对的是一个C++编译错误日志。从日志中提取关键信息: 1. **第一个错误出现在编译`odsp_test_tx_fde.cpp`时:** ``` In file included from ../../../src_llt/sd8911/hidsp0/testcase/alg/odsp_test_tx_fde.hpp:10, from ../../../src_llt/sd8911/hidsp0/testcase/alg/odsp_test_tx_fde.cpp:27: D:/8911/hlt/thirdparty/googletest/googletest-release-1.11.0/googletest/include/gtest/gtest.h:1928: warning: "FAIL" redefined In file included from D:/8911/src/cbb/base/common.h:12, from D:/8911/hlt/src_llt/common/llt_common.hpp:18, from ../../../src_llt/sd8911/hidsp0/testcase/alg/odsp_test_tx_fde.cpp:25: D:/8911/src/cbb/base/error_code.h:144: note: this is the location of the previous definition ``` 这里有一个警告:`gtest.h`中重新定义了`FAIL`,而之前已经在`error_code.h`中定义过。这可能会导致问题,因为宏重定义。 2. **紧接着出现致命错误:** ``` ../../../src_llt/sd8911/hidsp0/testcase/alg/odsp_test_tx_fde.cpp:41:1: error: expected initializer before 'using' compilation terminated due to -Wfatal-errors. ``` 这个错误发生在`odsp_test_tx_fde.cpp`的第41行,错误信息是:在`using`之前期望一个初始化器(可能是分号缺失或前面的语句不完整)。由于编译选项`-Wfatal-errors`,遇到错误就停止编译。 3. **另一个文件`llt_ecs_msa.cpp`的编译过程中出现了一堆警告,但并没有错误(只是警告),所以不是导致构建失败的直接原因。但是这些警告也需要关注。** **问题分析:** 1. **宏重定义警告:** `FAIL`在`gtest.h`和`error_code.h`中都被定义了。这可能会导致后续代码行为异常。通常,gtest中的`FAIL`是一个测试用的宏,而项目中的`FAIL`可能是错误码。如果两个头文件都被包含,且顺序导致gtest后包含,那么`FAIL`就会被重新定义,产生警告。如果项目中有代码依赖于`FAIL`是错误码,那么就会出错。 2. **语法错误:** 在`odsp_test_tx_fde.cpp`的第41行,出现了一个语法错误。错误信息提示在`using`之前期望一个初始化器,这通常意味着前面一行可能缺少分号,或者前面有一个不完整的声明。 **解决方案步骤:** 1. **解决语法错误:** - 打开文件`odsp_test_tx_fde.cpp`,定位到第41行(以及前一行)。 - 检查第40行(或附近)是否缺少分号,或者是否有其他语法错误(比如函数声明没有正确结束,类定义缺少分号等)。 - 例如,可能在第40行有一个类或结构体定义,后面没有加分号,然后紧接着第41行是一个`using`语句,这就会导致错误。 2. **解决宏重定义问题:** - 由于`FAIL`在两个头文件中都被定义,我们需要避免重定义。可以尝试以下方法: - 如果项目中的`error_code.h`定义的`FAIL`并不需要在所有地方使用,可以考虑在包含gtest的头文件之前#undef掉`FAIL`。 - 或者调整头文件包含顺序,确保`error_code.h`在`gtest.h`之后包含(但这样可能会让gtest的`FAIL`被覆盖,同样有问题)。 - 最好的方式是在使用gtest的测试文件中,确保在包含gtest头文件之前不包含`error_code.h`。如果必须包含,可以在包含gtest头文件之前使用`#undef FAIL`,这样gtest就可以重新定义它而不会产生警告。 但是注意,这个警告可能并不是导致编译失败的直接原因,因为警告不会停止编译,而错误会。所以优先解决语法错误。 3. **检查其他警告:** 虽然其他警告没有导致编译失败,但应该关注,特别是`unused parameter`警告,可能表示代码中有潜在问题。 **具体操作:** 1. 打开`odsp_test_tx_fde.cpp`,找到第41行,检查前面的代码(比如第40行)是否有语法错误。常见的错误包括: - 类或结构体定义后面缺少分号。 - 函数声明或变量声明后面缺少分号。 - 括号不匹配等。 2. 修复该语法错误。 3. 对于宏重定义,可以在包含gtest头文件之前,加入: ```cpp #ifdef FAIL #undef FAIL #endif ``` 这样,在包含gtest时,就不会有重定义的警告。但要注意,这样做之后,在测试文件中,`FAIL`就是gtest的宏了。如果测试文件中同时需要用到`error_code.h`中的`FAIL`(作为错误码),那么就需要避免使用相同的名字,或者使用其他方式。 4. 如果修复后仍然有错误,再检查其他问题。 由于构建日志中第一个错误是语法错误,并且导致了编译终止,因此修复这个语法错误是首要任务。 §§相关问题§§ 1. C++编译过程中如何处理宏重定义?如何避免宏重定义冲突? 2. 在C++项目中,如何正确包含头文件以避免宏冲突和重复定义? 3. 常见的C++语法错误有哪些?如何快速定位和修复? 4. 编译选项`-Wfatal-errors`的作用是什么?在项目中应如何使用? 5. 如何解决第三方库(如gtest)与项目自有头文件之间的宏定义冲突?</think>### 编译错误分析及解决方案 #### 主要错误原因 1. **宏重定义冲突** ```plaintext D:/8911/hlt/thirdparty/googletest/googletest-release-1.11.0/googletest/include/gtest/gtest.h:1928: warning: "FAIL" redefined D:/8911/src/cbb/base/error_code.h:144: note: this is the location of the previous definition ``` - Google Test 的 `FAIL` 宏与项目自定义的 `FAIL` 宏冲突 - 导致编译器无法确定使用哪个定义 2. **语法错误** ```plaintext ../../../src_llt/sd8911/hidsp0/testcase/alg/odsp_test_tx_fde.cpp:41:1: error: expected initializer before 'using' compilation terminated due to -Wfatal-errors. ``` - 源文件 `odsp_test_tx_fde.cpp` 第41行存在语法错误 - `-Wfatal-errors` 选项使编译器遇到第一个错误即终止 3. **Mockcpp 模板警告** ```plaintext warning: unused parameter 'api' [-Wunused-parameter] warning: unused parameter 'hook' [-Wunused-parameter] ``` - Mockcpp 框架生成的模板代码有未使用参数 - 属于警告而非错误,但需注意潜在问题 #### 解决方案步骤 1. **解决宏冲突** 在包含 Google Test 头文件前取消 `FAIL` 定义: ```cpp // 在包含 gtest.h 之前添加 #ifdef FAIL #undef FAIL // 取消项目自定义的 FAIL 宏 #endif #include <gtest/gtest.h> ``` 2. **修复语法错误** 检查 `odsp_test_tx_fde.cpp` 第41行及上下文: ```cpp // 示例修复(需根据实际代码调整) // 错误行可能类似: // class MyClass { ... } // 缺少分号 // using namespace std; // 正确形式: class MyClass { ... }; // 添加分号 using namespace std; ``` 3. **处理编译器警告** - 方案1:关闭特定警告(在CMake中添加) ```cmake add_compile_options(-Wno-unused-parameter) ``` - 方案2:修改Mockcpp调用(推荐) ```cpp // 在函数参数中添加 [[maybe_unused]] 属性 void myFunc([[maybe_unused]] int unusedParam) { ... } ``` #### 完整修复流程 1. 在 `odsp_test_tx_fde.hpp` 头部添加宏取消定义 2. 检查并修复 `odsp_test_tx_fde.cpp` 第41行语法 3. 清理构建缓存后重新编译: ```bash cmake --build . --clean-first ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值