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

3

23

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.

Frivolous answered 2/5, 2011 at 21:36 Comment(0)
B
51

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 image description here

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

Barrie answered 4/5, 2011 at 10:9 Comment(3)
BTW, OP should change linker settings for entire project instead of single target. This can be done by clicking on the project name inside of Project build options window (My Project on the screenshot).Dedication
Hi, I was having a problem linking SDL and SDL_image, thank you this example helped me a lot.. btw in some examples I've seen the say to link it like "-lSDL" and "-lSDL_image" but that is ok for the command line. CodeBlocks adds the "-l" for you so only add the library name, as you did GDI32. They should explain this out as there are a lot of noobies around and get confused. (I was one) thank youAnecdotage
Thanks, Damon. using this solution I linked gdi32 library in my project. My developement OS is window7 and IDE is codeblock. It resolve my following error: "undefined reference to getstockobject 4@"Amboise
H
0

You can create new with project win32 gui api it has default add library -lgdi32 -luser32 -lkernel32 -lcomctl32 -mwindows or add library -mwindows in your project, it worked for me.

Honeyman answered 20/11, 2022 at 15:8 Comment(0)
S
-1

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.

Slacker answered 4/5, 2011 at 11:45 Comment(1)
I don't believe this is true. im using opengl(win32 window creation) with a console application and it still works.Curable

© 2022 - 2024 — McMap. All rights reserved.