gpt4 book ai didi

c++ - 在命令行中正确链接库

转载 作者:行者123 更新时间:2023-11-28 06:01:56 26 4
gpt4 key购买 nike

前言:我是 C++ 的新手,才刚刚开始认真编程。

后序:我试图为我在这篇文章中提到的功能/页面发布一个链接,但 Stack Overflow 对我大吼大叫,因为我没有足够的声誉发布两个以上的链接。

我正在尝试使用 MinGW 和命令行在 C++ 中使用 Windows API 制作一些简单的 GUI。我正在尝试更改窗口背景,一个有助于执行此操作的函数是 CreateSolidBrush 函数。这个函数需要 gdi32 库,但每次我尝试编译/链接到这个库时,我都会收到类似“找不到那个库,sucka”的错误。

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

g++ -LC:\WINDOWS\System32 -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;
}

最佳答案

Question 1: To start simple, what is the proper notation/command to link to individual header/source files which are not in the current directory?

那不是链接,那是编译/包含(除非您首先将这些源文件编译为目标文件)。

所以:

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 某些内容时查看的位置。

Question 2: What is the proper notation/command to link to a library which is in the current directory?

见3; -L. 应该有效。

Question 3: What is the proper notation/command to link to a library which is not in the current directory?

到目前为止,您做对了:-L 告诉 gcc 要查找库的路径,-l{libname} 链接到库。

关于c++ - 在命令行中正确链接库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33110118/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com