在命令行中正确链接库 序言:我对C++很陌生,我刚刚开始认真编程。

在命令行中正确链接库 序言:我对C++很陌生,我刚刚开始认真编程。,c++,windows,mingw,C++,Windows,Mingw,文章前言:我试图为我在这篇文章中提到的函数/页面发布一个链接,但堆栈溢出对我大喊大叫,因为我没有足够的声誉发布两个以上的链接 我试图用Windows IAPI使用MINW和命令行在C++中制作一些简单的GUI。我正在尝试更改窗口背景,其中一个函数是CreateSolidBrush函数。这个函数需要gdi32库,但每次我试图编译/链接到这个库时,我都会收到一个错误,大意是“找不到那个库,sucka” 并提供有关MinGW库功能的有用信息。堆栈溢出帖子#5683058和#17031290描述了我认为

文章前言:我试图为我在这篇文章中提到的函数/页面发布一个链接,但堆栈溢出对我大喊大叫,因为我没有足够的声誉发布两个以上的链接

<>我试图用Windows IAPI使用MINW和命令行在C++中制作一些简单的GUI。我正在尝试更改窗口背景,其中一个函数是CreateSolidBrush函数。这个函数需要gdi32库,但每次我试图编译/链接到这个库时,我都会收到一个错误,大意是“找不到那个库,sucka”

并提供有关MinGW库功能的有用信息。堆栈溢出帖子#5683058和#17031290描述了我认为与我类似的问题。我已经广泛地搜索了关于如何链接其他目录(尤其是Windows库)中的文件/库的简单而直接的答案,但是没有幸实现这些页面中的知识。也许答案就在眼前,但我“见猫画虎”的勇敢努力是徒劳的。可能是我输入了不正确的路径/名称(lib vs dll?),或者我完全忽略了更基本的内容(缺少标题?)。我尝试使用的一个命令是

g++ -LC:\WINDOWS\System32 -lgdi32 gui.cpp
但这似乎不起作用(注意:名为“gui.cpp”的源文件)

问题1:简单地说,链接到当前目录中的不是的单个头文件/源文件的正确符号/命令是什么

问题2:链接到当前目录中的的正确符号/命令是什么

问题3:链接到当前目录中不是库的正确符号/命令是什么

我意识到这些问题在其他页面上以各种方式得到了回答,但它们经常与有关VisualStudio、Eclipse、Code::Blocks等的说明混在一起,因此对于放弃IDE奢侈的新手来说并不清楚。如果能给你一个直截了当的回答,我将不胜感激。非常感谢您的帮助和指导

我将发布我的代码,但我认为只有前五行中的几行是相关的:

#include <windows.h>
#include <string>

COLORREF desired_color = RGB(200,200,200);
HBRUSH hBrush = CreateSolidBrush(desired_color);

static char str_class_name[]  = "MyClass";
static char str_titlebar[] = "My Window Title";
static int window_width = 300;
static int window_height = 300;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

static HINSTANCE program_global_instance = NULL;

int WINAPI WinMain(HINSTANCE program_current_instance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    program_global_instance = program_current_instance;
    WNDCLASSEX window_class;
    HWND window_handle;
    MSG window_message;

    window_class.cbSize        = sizeof(WNDCLASSEX); // size of struct; always set to size of WndClassEx
    window_class.style         = 0; // window style
    window_class.lpfnWndProc   = WndProc; // window callback procedure
    window_class.cbClsExtra    = 0; // extra memory to reserve for this class
    window_class.cbWndExtra    = 0; // extra memory to reserve per window
    window_class.hInstance     = program_global_instance; // handle for window instance
    window_class.hIcon         = LoadIcon(NULL, IDI_APPLICATION); // icon displayed when user presses ALT+TAB
    window_class.hCursor       = LoadCursor(NULL, IDC_ARROW); // cursor used in the program
    window_class.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // brush used to set background color
    window_class.lpszMenuName  = NULL; // menu resource name
    window_class.lpszClassName = str_class_name; // name with which to identify class
    window_class.hIconSm       = LoadIcon(NULL, IDI_APPLICATION); // program icon shown in taskbar and top-left corner

    if(!RegisterClassEx(&window_class)) {
        MessageBox(0, "Error Registering Class!", "Error!", MB_ICONSTOP | MB_OK);
        return 0;
    }

    window_handle = CreateWindowEx(
        WS_EX_STATICEDGE, // dwExStyle: window style
        str_class_name, // lpClassName: pointer to class name
        str_titlebar, // lpWindowName: window titlebar
        WS_OVERLAPPEDWINDOW, // dwStyle: window style
        CW_USEDEFAULT, // x: horizontal starting position
        CW_USEDEFAULT, // y: vertical starting position
        window_width, // nWidth: window width
        window_height, // nHeight: window height
        NULL, // hWndParent: parent window handle (NULL for no parent)
        NULL, // hMenu: menu handle (Null if not a child)
        program_global_instance, // hInstance : current window instance
        NULL // lpParam -Points to a value passed to the window through the CREATESTRUCT structure.
        );

    if (window_handle == NULL) {
        MessageBox(0, "Error Creating Window!", "Error!", MB_ICONSTOP | MB_OK);
        return 0;
    }

    ShowWindow(window_handle, nCmdShow);
    UpdateWindow(window_handle);

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

    return window_message.wParam;
}

// window_handle: window ID
// uMsg: window message
// wParam: additional message info; depends on uMsg value
// lParam: additional message info; depends on uMsg value
LRESULT CALLBACK WndProc(
    HWND window_handle, 
    UINT Message, 
    WPARAM wParam, 
    LPARAM lParam
    ) {
    switch(Message) {
        case WM_CLOSE:
            DestroyWindow(window_handle);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(window_handle, Message, wParam, lParam);
    }
    return 0;
}
#包括
#包括
COLORREF所需颜色=RGB(200200);
HBRUSH HBRUSH=CreateSolidBrush(所需的颜色);
静态字符str_class_name[]=“MyClass”;
静态字符str_titlebar[]=“我的窗口标题”;
静态int窗口_宽度=300;
静态int窗口_高度=300;
LRESULT回调WndProc(HWND、UINT、WPARAM、LPARAM);
静态HINSTANCE程序\全局\实例=NULL;
int WINAPI WinMain(HINSTANCE程序\当前\实例,HINSTANCE HPPreInstance,LPSTR lpCmdLine,int nCmdShow){
程序\全局\实例=程序\当前\实例;
WNDCLASSEX窗口类;
窗口把手;
消息窗口;
window_class.cbSize=sizeof(WNDCLASSEX);//结构的大小;始终设置为WNDCLASSEX的大小
窗口\u class.style=0;//窗口样式
window_class.lpfnWndProc=WndProc;//窗口回调过程
window_class.cbClsExtra=0;//要为此类保留的额外内存
window\u class.cbWndExtra=0;//每个窗口要保留的额外内存
window\u class.hInstance=program\u global\u instance;//窗口实例的句柄
window_class.hIcon=LoadIcon(NULL,IDI_应用程序);//当用户按下ALT+TAB时显示的图标
window_class.hCursor=LoadCursor(NULL,IDC_箭头);//程序中使用的光标
window_class.hbrBackground=(HBRUSH)(COLOR_window+1);//用于设置背景色的画笔
window_class.lpszMenuName=NULL;//菜单资源名称
window_class.lpszClassName=str_class_name;//用于标识类的名称
window_class.hIconSm=LoadIcon(NULL,IDI_应用程序);//程序图标显示在任务栏和左上角
if(!RegisterClass(&window_类)){
消息框(0,“注册类时出错!”,“出错!”,MB_ICONSTOP|MB_OK);
返回0;
}
窗口\ U句柄=CreateWindowEx(
WS_EX_STATICEDGE,//dwExStyle:windowstyle
str_class_name,//lpClassName:指向类名的指针
str_titlebar,//lpWindowName:window titlebar
WS\u重叠窗口,//dwStyle:windowstyle
CW\U USEFAULT,//x:水平起始位置
CW\u使用默认值,//y:垂直起始位置
窗口宽度,//nWidth:窗口宽度
窗高,//n窗高:窗高
NULL,//hWndParent:父窗口句柄(NULL表示没有父窗口)
NULL,//hMenu:菜单句柄(如果不是子菜单,则为NULL)
程序\全局\实例,//hInstance:当前窗口实例
NULL//lpParam-指向通过CREATESTRUCT结构传递给窗口的值。
);
if(窗口句柄==NULL){
消息框(0,“创建窗口时出错!”,“出错!”,MB|U ICONSTOP|MB|U OK);
返回0;
}
ShowWindow(窗口句柄,nCmdShow);
更新窗口窗口(窗口句柄);
while(GetMessage(&window_message,NULL,0,0)){
TranslateMessage(窗口消息和窗口消息);
DispatchMessage(窗口消息和窗口消息);
}
返回窗口\u message.wParam;
}
//窗口句柄:窗口ID
//窗口消息
//wParam:附加消息信息;取决于uMsg值
//lParam:附加消息信息;取决于uMsg值
LRESULT回调WndProc(
HWND窗把手,
UINT消息,
WPARAM WPARAM,
LPARAM
) {
开关(信息){
案例WM_结束:
车窗(车窗把手);
打破
案例WM_销毁:
PostQuitMessage(0);
打破
违约:
返回DefWindowProc(窗口句柄、消息、wParam、lParam);
}
ret
gcc {other options} -o gui.exe gui.cpp /path/to/source_file_one.cpp /path/to/source_file_n.cpp
gcc {other options} -c -o source_file_one.o /path/to/source_file_one.cpp
gcc {other options} -c -o source_file_n.o /path/to/source_file_n.cpp
gcc {other options} -o gui.exe source_file_n.o source_file_one.o gui.cpp