正确地 Link 命令行中的库
Properly Link Libraries in the Command Line
前言:我是 C++ 的新手,才刚刚开始认真编程。
Post-Preface:我试图 post 一个 link 我在这个 post 中提到的 functions/pages,但 Stack Overflow 对我大喊大叫,因为我没有足够的声誉 post 超过两个 link。
我正在尝试使用 MinGW 和命令行在 C++ 中使用 Windows API 制作一些简单的 GUI。我正在尝试更改 window 背景,CreateSolidBrush 函数可帮助完成此操作。此函数需要 gdi32 库,但每次我尝试 compile/link 到此库时,我都会收到类似 "can't find that library, sucka".
的错误
Page1 and page2 提供有关 MinGW 库功能的有用信息。 Stack Overflow post#5683058 和#17031290 描述了我认为与我相似的 questions/problems。我已经广泛搜索了关于如何从其他目录(尤其是 Windows 库)link files/libraries 的简单而直接的答案,但没有运气从这些页面中实现知识。也许答案就在我眼前,但我为 "see the cat, draw the tiger" 所做的勇敢努力是徒劳的。有可能我输入了错误的 path/name(lib vs dll?),或者我可能完全忽略了一些更基本的东西(缺少 header?)。我尝试使用的一个命令是
g++ -LC:\WINDOWS\System32 -lgdi32 gui.cpp
但这似乎不起作用(注意:源文件名为 "gui.cpp")。
问题 1:简单来说,什么是 notation/command 到 link 到 header/source 个文件 的 [=34] =]不在当前目录?
问题 2:什么是 notation/command 到 link 到 库 而 是 当前目录?
问题 3:什么是 notation/command 到 link 到 库 是 不是 当前目录?
我知道这些问题在其他页面上以多种方式 sorta-kinda 得到了回答,但它们经常与有关 Visual Studio、Eclipse、Code::Blocks 等的说明混在一起。因此对于放弃 IDE 奢侈品的新手来说不清楚。对于典型的 run-of-the-mill 新手,我将不胜感激。非常感谢您的帮助或指导。
我会 post 我的代码,但我认为前五行中只有几行是相关的:
#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?
那不是 linking,那是 compiling/including(除非您先将这些源文件编译为目标文件)。
所以:
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 不要尝试和 link 在一起,因为这是在最后一步中完成的。
{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}
link 告诉 gcc 针对库的路径。
前言:我是 C++ 的新手,才刚刚开始认真编程。
Post-Preface:我试图 post 一个 link 我在这个 post 中提到的 functions/pages,但 Stack Overflow 对我大喊大叫,因为我没有足够的声誉 post 超过两个 link。
我正在尝试使用 MinGW 和命令行在 C++ 中使用 Windows API 制作一些简单的 GUI。我正在尝试更改 window 背景,CreateSolidBrush 函数可帮助完成此操作。此函数需要 gdi32 库,但每次我尝试 compile/link 到此库时,我都会收到类似 "can't find that library, sucka".
的错误Page1 and page2 提供有关 MinGW 库功能的有用信息。 Stack Overflow post#5683058 和#17031290 描述了我认为与我相似的 questions/problems。我已经广泛搜索了关于如何从其他目录(尤其是 Windows 库)link files/libraries 的简单而直接的答案,但没有运气从这些页面中实现知识。也许答案就在我眼前,但我为 "see the cat, draw the tiger" 所做的勇敢努力是徒劳的。有可能我输入了错误的 path/name(lib vs dll?),或者我可能完全忽略了一些更基本的东西(缺少 header?)。我尝试使用的一个命令是
g++ -LC:\WINDOWS\System32 -lgdi32 gui.cpp
但这似乎不起作用(注意:源文件名为 "gui.cpp")。
问题 1:简单来说,什么是 notation/command 到 link 到 header/source 个文件 的 [=34] =]不在当前目录?
问题 2:什么是 notation/command 到 link 到 库 而 是 当前目录?
问题 3:什么是 notation/command 到 link 到 库 是 不是 当前目录?
我知道这些问题在其他页面上以多种方式 sorta-kinda 得到了回答,但它们经常与有关 Visual Studio、Eclipse、Code::Blocks 等的说明混在一起。因此对于放弃 IDE 奢侈品的新手来说不清楚。对于典型的 run-of-the-mill 新手,我将不胜感激。非常感谢您的帮助或指导。
我会 post 我的代码,但我认为前五行中只有几行是相关的:
#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?
那不是 linking,那是 compiling/including(除非您先将这些源文件编译为目标文件)。
所以:
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 不要尝试和 link 在一起,因为这是在最后一步中完成的。
{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}
link 告诉 gcc 针对库的路径。