C++ 如何使用 Code::Blocks 链接到库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5862757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I link to a library with Code::Blocks?
提问by optimusfrenk
C++ GUI Tutorial: undefined reference to TextOut
I have the same problem, but I'm new to programming and Code::Blocks, and I want to use the GDI32 library. How can I install it? I'm very confused because I can use the windows.h header, but some functions like TextOut
aren't available.
我有同样的问题,但我是编程和 Code::Blocks 的新手,我想使用 GDI32 库。我该如何安装?我很困惑,因为我可以使用 windows.h 标头,但某些功能TextOut
不可用。
回答by Damon
The gdi32 library is already installed on your computer, few programs will run without it. Your compiler will (if installed properly) normally come with an import library, which is what the linker uses to make a binding between your program and the file in the system. (In the unlikely case that your compiler does not come with import libraries for the system libs, you will need to download the Microsoft Windows Platform SDK.)
gdi32 库已经安装在您的计算机上,没有它,很少有程序可以运行。您的编译器(如果安装正确)通常会附带一个导入库,链接器使用它来在您的程序和系统中的文件之间进行绑定。(在不太可能的情况下,您的编译器没有为系统库提供导入库,您需要下载 Microsoft Windows 平台 SDK。)
To link with gdi32:
与 gdi32 链接:
This will reliably work with MinGW-gcc for all system libraries (it should workif you use any other compiler too, but I can't talk about things I've not tried). You can also write the library's full name, but writing libgdi32.a
has no advantage over gdi32
other than being more type work.
If it does notwork for some reason, you may have to provide a different name (for example the library is named gdi32.lib
for MSVC).
这将可靠地与所有系统库的 MinGW-gcc 一起使用(如果您也使用任何其他编译器,它应该可以工作,但我不能谈论我没有尝试过的事情)。你也可以写图书馆的全名,但写作除了是更多类型的工作之外libgdi32.a
没有任何优势gdi32
。
如果它不是出于某种原因,你可能必须提供一个不同的名称(例如图书馆被命名gdi32.lib
为MSVC)。
For libraries in some odd locations or project subfolders, you will need to provide a proper pathname (click on the "..." button for a file select dialog).
对于一些奇怪位置或项目子文件夹中的库,您需要提供正确的路径名(单击“...”按钮以打开文件选择对话框)。
回答by optimusfrenk
At a guess, you used Code::Blocks to create a Console Application project. Such a project does not link in the GDI stuff, because console applications are generally not intended to do graphics, and TextOut
is a graphics function. If you want to use the features of the GDI, you should create a Win32 Gui Project, which will be set up to link in the GDI for you.
猜想,您使用 Code::Blocks 创建了一个控制台应用程序项目。这样的项目在GDI的东西里是没有链接的,因为控制台应用程序一般不打算做图形的,TextOut
是一个图形功能。如果您想使用 GDI 的功能,您应该创建一个Win32 Gui 项目,它将被设置为您在 GDI 中链接。