我的c ++程序将不会编译后,我关闭源,然后再次打开它 [英] My c++ programs won't compile after I close the source then open it again

查看:581
本文介绍了我的c ++程序将不会编译后,我关闭源,然后再次打开它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编译它,它运行,然后我重新打开它,不工作了。
这发生在devc ++ 5.8.3和代码块中。

I compile it, it runs and then I reopen it and doesn't work anymore. This happens in devc++ 5.8.3 and codeblocks.

我的代码(注释默认来自IDE):

My code(the comments are default from the IDE):

    #include <windows.h>
    #include <cmath>
    #include <cstdlib>

    HPEN hPen;
    HDC hDC;
    PAINTSTRUCT Ps;

    void DrawLine(int x1, int y1, int x2, int y2, HPEN pen){
        SelectObject(hDC,pen);
        MoveToEx(hDC,x1,y1,NULL);
        LineTo(hDC,x2,y2);
    }
    void Draw(int originx, int originy, float value,int handlength){
        DrawLine(originx, originy, (handlength*cos(value*180/3.14))+originx, (handlength*sin(value*180/3.14))+originy,hPen);
        DrawLine(originx, originy, -(handlength*cos(value*180/3.14))+originx, -(handlength*sin(value*180/3.14))+originy,hPen);
    }
    /* This is where all the input to the window goes to */
    LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
        switch(Message) {

            /* Upon destruction, tell the main thread to stop */
            case WM_DESTROY: {
                PostQuitMessage(0);
                break;
            }
            case WM_PAINT: {
                int r;
                int g;
                int b;
                hDC = BeginPaint(hwnd, &Ps);
                for(int i=0;i<170;i=i+1){   
                r = rand() % 256;
                g = rand() % 256;
                b = rand() % 256;
                Sleep(10);
                hPen = CreatePen(PS_INSIDEFRAME, 1, RGB(r, g, b));
                Draw(320,240,i,180);

                }       
                EndPaint(hwnd, &Ps);
                break;
            }
            /* All other messages (a lot of them) are processed using default procedures */
            default:
                return DefWindowProc(hwnd, Message, wParam, lParam);
        }
        return 0;
    }

    /* The 'main' function of Win32 GUI programs: this is where execution starts */
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
        WNDCLASSEX wc; /* A properties struct of our window */
        HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
        MSG msg; /* A temporary location for all messages */

        /* zero out the struct and set the stuff we want to modify */
        memset(&wc,0,sizeof(wc));
        wc.cbSize        = sizeof(WNDCLASSEX);
        wc.lpfnWndProc   = WndProc; /* This is where we will send messages to */
        wc.hInstance     = hInstance;
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);

        /* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wc.lpszClassName = "WindowClass";
        wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */

        if(!RegisterClassEx(&wc)) {
            MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
            return 0;
        }

        hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Lines",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, /* x */
            CW_USEDEFAULT, /* y */
            640, /* width */
            480, /* height */
            NULL,NULL,hInstance,NULL);

        if(hwnd == NULL) {
            MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
            return 0;
        }

        /*
            This is the heart of our program where all input is processed and 
            sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
            this loop will not produce unreasonably high CPU usage
        */
        while(GetMessage(&msg, NULL, 0, 0) > 0) { /* If no error is received... */
            TranslateMessage(&msg); /* Translate key codes to chars if present */
            DispatchMessage(&msg); /* Send it to WndProc */
        }

        return msg.wParam;
    }

编译器日志:


  • C ++编译器:C:\Program Files(x86)\Dev-Cpp \MinGW64\bin\g ++。exe

  • 命令:g ++。exeC:\Users\Pall\Desktop\proj1\lines.cpp-oC:\Users\Pall\Desktop\proj1\ lines.exe-g3 -IC:\Program Files(x86)\Dev-Cpp \MinGW64\include-IC:\Program Files(x86)\Dev-Cpp\MinGW64 \x86_64-w64-mingw32 \include-IC:\Program Files(x86)\Dev-Cpp \MinGW64\lib\gcc\x86_64-w64-mingw32\4.8.1\\ \\include-IC:\Program Files(x86)\Dev-Cpp \MinGW64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c ++-L C:\ Program Files(x86)\Dev-Cpp \MinGW64\lib-LC:\Program Files(x86)\Dev-Cpp \MinGW64\x86_64-w64-mingw32 \\ \\ lib-static-libgcc -g3
    C:\Users\Pall\AppData\Local\Temp\ccRYW3Cy.o:在函数 DrawLine(int,int, int,int,HPEN __ *)':
    C:/Users/Pall/Desktop/proj1/lines.cpp:12:未定义引用
    __ imp_SelectObject'
    C:/ Users /Pall/Desktop/proj1/lines.cpp:13:未定义引用 __ imp_MoveToEx'
    C:/Users/Pall/Desktop/proj1/lines.cpp:14:未定义对 __ imp_LineTo'
    C:\Users\Pall\AppData\Local\Temp\ccRYW3Cy.o:在函数 WndProc(HWND __ *,unsigned int, unsigned long long,long long)':
    C:/Users/Pall/Desktop/proj1/lines.cpp:36:undefined对
    的引用__ imp_CreatePen'
    collect2.exe:错误:ld退回1退出状态

  • C++ Compiler: C:\Program Files (x86)\Dev-Cpp\MinGW64\bin\g++.exe
  • Command: g++.exe "C:\Users\Pall\Desktop\proj1\lines.cpp" -o "C:\Users\Pall\Desktop\proj1\lines.exe" -g3 -I"C:\Program Files (x86)\Dev-Cpp\MinGW64\include" -I"C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include" -I"C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.8.1\include" -I"C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++" -L"C:\Program Files (x86)\Dev-Cpp\MinGW64\lib" -L"C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\lib" -static-libgcc -g3 C:\Users\Pall\AppData\Local\Temp\ccRYW3Cy.o: In function DrawLine(int, int, int, int, HPEN__*)': C:/Users/Pall/Desktop/proj1/lines.cpp:12: undefined reference to__imp_SelectObject' C:/Users/Pall/Desktop/proj1/lines.cpp:13: undefined reference to __imp_MoveToEx' C:/Users/Pall/Desktop/proj1/lines.cpp:14: undefined reference to__imp_LineTo' C:\Users\Pall\AppData\Local\Temp\ccRYW3Cy.o: In function WndProc(HWND__*, unsigned int, unsigned long long, long long)': C:/Users/Pall/Desktop/proj1/lines.cpp:36: undefined reference to__imp_CreatePen' collect2.exe: error: ld returned 1 exit status


推荐答案

导致因为你需要链接库libgdi32.a,如果你使用DevC ++,点击工具 - >编译器选项,打开窗口时,选中添加这些命令到链接器命令行框和在下面的文本框添加文字:

this is caused because you need to link the library libgdi32.a, if you are using the DevC++, click in Tools->Compiler Options, when open the window, check the box "Add these commands to the linker command line" and in the below textbox add the text:

-lgdi32

您需要是这样的:

这篇关于我的c ++程序将不会编译后,我关闭源,然后再次打开它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆