在命令行中正确链接库
Properly Link Libraries in the Command Line
前言:我对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}
指向库的链接。
- 如何从命令行链接C++库
- 链接从命令行转换为Make的库
- Cmake 3.8.0在makefiles中生成错误的链接命令
- 为什么我在我的C 代码中获得链接命令失败错误
- Xcode C :链接命令失败,出口代码1未找到-lboost_regex
- 使用 cmake 将 boost 链接到 c++:命令行编译
- Qt命令行编译在链接器阶段失败,出现垃圾NXCOMPAT数据
- VS 2008:链接命令包括我没有要求且不存在的库
- 通过命令行编译和链接 Swift/Objective-C++ 应用程序
- 当我仅使用链接器选项 /LTCG时,为什么Visual Studio在Linker命令行中显示选项 /PGD
- 为什么我必须在链接器行的末尾传递库
- 如何使用命令行在C++中编译和链接分隔的 .h 和 .cpp 文件
- Turbo C++中通过命令行出现链接器错误
- 如何通过命令行设置VC++动态链接
- 如何从命令行编译和链接多个文件
- CMake:运行链接命令时出错:%1不是有效的Win32应用程序
- 是否可以在命令行上使用 visual-c++ 静态链接特定库
- 如何在VS 2012中查看c++链接命令
- CMake(Android工具链)-运行链接命令时出错:没有这样的文件或目录
- 如何使用CMake在链接命令行结束时添加标志?