How do I link to a library with Code::Blocks?

2018-12-31 15:55发布

问题:

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.

回答1:

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.)

To link with gdi32:

\"enter

This will reliably work with MinGW-gcc for all system libraries (it should work if 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 not work for some reason, you may have to provide a different name (for example the library is named gdi32.lib for 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).



回答2:

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.



回答3:

To recap the process needed to use a library: Once per library:

1) Acquire the library. Download it from the website or via a package manager.

2) Install the library. Unzip it to a directory or install it via a package manager.

3) Tell the compiler where to look for the header file(s) for the library.

4) Tell the linker where to look for the library file(s) for the library.

Once per project:

5) Tell the linker which static or import library files to link. 6) #include the library’s header file(s) in your program. 7) Make sure the program know where to find any dynamic libraries being used.

Steps 1 and 2 — Acquire and install library Download and install the library to your hard disk. See the tutorial on static and dynamic libraries for more information about this step. Steps 3 and 4 — Tell the compiler where to find headers and library files We are going to do this on a global basis so the library will be available to all of our projects. Consequently, the following steps only need to be done once per library. A) Go to the “Settings menu” and pick “Compiler”. B) Click the “Directories” tab. The compiler tab will already be selected for you. C) Press the “Add” button, and add the path to the .h files for the library. If you are running Linux and installed the library via a package manager, make sure / usr/include is listed here. D) Click the “Linker” tab. Press the “Add” button, and add the path to the .lib files for the library. If you are running Linux and installed the library via a package manager, make sure /usr/lib is listed here. E) Press the “OK” button. Step 5 — Tell the linker which libraries your program is using For step 5, we need to add the library files from the library to our project. We do this on an individual project basis. A) Right click on the bolded project name under the default workspace (probably “Console application”, unless you changed it). Choose “Build options” from the menu. B) Click the linker tab. Under the “Link libraries” window, press the “Add” button and add the library you wish your project to use. C) Press the “OK” button

Steps 6 and 7 — #include header files and make sure project can find DLLs Simply #include the header file(s) from the library in your project.

Source: http://www.learncpp.com/cpp-tutorial/a3-using-libraries-with-codeblocks/