在命令行中正确链接库

Properly Link Libraries in the Command Line

本文关键字:链接 命令行      更新时间:2023-10-16

前言:我对c++相当陌生,刚刚开始认真编程。

post -前言:我试图为我在这篇文章中提到的函数/页面发布一个链接,但是Stack Overflow对我大喊大叫,因为我没有足够的声誉来发布两个以上的链接。

我试图使一些简单的gui在c++与Windows API使用MinGW和命令行。我试图改变窗口背景,其中一个功能是CreateSolidBrush功能。这个函数需要gdi32库,但是每次我尝试编译/链接到这个库时,我都会收到一个"找不到那个库,笨蛋"的错误。

Page1和page2提供了有关MinGW库功能的有用信息。Stack Overflow post # 5683058和# 17031290描述了我认为与我相似的问题/问题。关于如何从其他目录(尤其是Windows库)链接文件/库,我已经广泛地搜索了一个简单而直接的答案,但在实现这些页面的知识方面没有运气。也许答案就在我眼前,但我"见猫画虎"的勇敢努力是徒劳的。这是可能的,我正在进入不正确的路径/名称(lib vs dll?),或者也许我完全忽略了一些更基本的东西(缺少一个头?)。我尝试使用的一个命令是

g++ -LC:WINDOWSSystem32 -lgdi32 gui.cpp

但这似乎不起作用(注意:源文件名为"gui.cpp")。

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

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

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

我意识到这些问题在其他页面上有各种各样的答案,但它们经常与关于Visual Studio、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;
}

问题1:从简单的开始,什么是正确的符号/命令来链接到当前目录下的单个头文件/源文件?

这不是链接,这是编译/包含(除非你先把那些源文件编译成目标文件)。

:

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

-c告诉gcc不要尝试将事物链接在一起,因为这是在最后一步中完成的。

{other options}可以包含-I{include dirs}来通知gcc当你#include的东西在哪里看。

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

看到3;-L.应该可以。

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

到目前为止,你做的是对的:-L告诉gcc查找库的路径,-l{libname}指向库的链接。