A.3 — Using libraries with Code::Blocks

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.

See the tutorial A.1 -- static and dynamic libraries for more information step 7.

B.1 -- Introduction to C++11
A.2 -- Using libraries with Visual Studio

56 comments to A.3 — Using libraries with Code::Blocks

  • Justin

    This is a pretty bad tutorial. How does one actually statically link library? These steps are very general. I appreciate the definitions of static and dynamic libraries

  • sv

    I am trying to link MySQL and c in codeblocks but when I compile I get a bunch of "undefined reference" errors
    I added the include directory and lib directory in there correct places but it doesn't work
    Googling I found a similar error post but it was for linux terminal, solution was add a flag '-lmysqlclient'. I don't know how to do this in codeblocks/windows and also the error may be from something else

  • Dan

    Any chance you have a good tutorial on using global compiler variables with CodeBlocks?
    The manual, wiki and internet are outdated and all over the place with very poor explanations of using them, leaving only trial and error to try to figure out what is going on.

  • George Pitchurov

    Why do we need step 5? Why is it not enough to #include the header file in our program and let the IDE link it? The IDE already knows perfectly well where to look for both library headers and code (steps 3 and 4). How is it we don't care about this step when using the standard libraries?

    • Alex

      The standard libraries will automatically link in if you use anything from them. Unfortunately, this doesn't extend to user-defined libraries, so they need to be explicitly specified.

  • masterOfNothing

    I'm completely blank on this.I went to SDL website. Just for testing downloaded a .zip file for Windows. It only has one SDL2.dll file. What do I do with it? For some reason adding packages just didn't click. It is dynamic link library extension.

    • Alex

      Download the development libraries for your IDE, not the runtime binaries.

      • masterOfNothing

        Hello, Alex.

        Sorry for responding late.

        Yes, I downloaded the wrong file, but the main file package doesn't seem to have a .lib file.
        The files I downloaded from:


        So I looked here for instructions how to install just this library:

        SDL2 package had a .dll file in /bin folder, which had to be copied to the project's folder (for .exe file).

        I got the library going, but still confused as hell. It involves a bit different approach, namely, for Code::Blocks you have to copy "-lmingw32 -lSDL2main -lSDL2" in 'other linker options' under 'Linker settings tab'. Which I basically don't understand. I'm sure the same library (SDL) can also be installed in a different way. This worked only on the project created for that tutorial. Not on global scale.

        I'm still unsure how to handle .dll files at this point. Also I'd like to do it the way you wrote it here. Because there are other libraries, and will probably want to know how to install them independently without googling "how to..." for every single one. Can you suggest any library that has a .lib file (import). C++ libraries are a bit more confusing than python's modules :D

  • Hello Alex and Nascardriver, I recently downloaded the SDl library; i tried using your method but each time i try to compile it i get the same error: FATAL ERROR: SDL/SDL.h no such file or directory please HELP...........

    • Alex

      1) Did you set your compiler directory to the SDL root as shown above?
      2) Does <root>/SDL/SDL.h actually exist on your drive?

      • I did exactly as the tutorial but i don't see why it doesn't work; i even checked it more than 10 times
        Is anything wrong with my codeblocks?
        Do i need visual studio to actually use it.
        PS: I have codeblocks 17 but i tried it even on codeblocks 12 and it isn't working aat all.


        • Alex

          No, you don't need visual studio, this article is written specifically for Code::Blocks users.

          Can you answer the following questions for me:
          1) Where is SDL.h on your machine?
          2) What are you setting for your compiler directory?
          3) How are you including SDL.h? (I presume #include "SDL/SDL.h" but just checking)

  • Christian

    Hi Alex,

    First of all, thank you for the amazing tutorial. I have learned so much from it! I have a question about using libraries in CodeBlocks for which nobody has given me a clear answer yet. Below is a piece of code that I use for a function minimization using a package called Minuit2. I have acquired the required libraries and I have followed your instructions on how to use them in CodeBlocks.

    However, when I run it I keep getting the following error

    C:/root_v5.34.36/include/Minuit2/MnApplication.h:48: undefined reference to `vtable for ROOT::Minuit2::MnApplication'

    And many similar ones. Am I missing something, i.e., is there an additional step not listed above that is required for certain libraries?


    • Alex

      Seems like a library specific issue. has a way to fix it for linux, perhaps it could be adapted for Code::Blocks as well.

      • Christian

        Hi Alex,

        Thank you for your reply. That is me on the root forum. :) It did solve the problem in Linux but it still doesn't fix it in CB.

        • Alex

          Did you follow the instructions from lesson A.3 in the appendix?

          Steps 3/4 should be pointed at wherever the .h/.lib files live (wherever /Minuit2/lib is).
          Step 5 should have Minuit2.lib selected.

          • Christian

            Yes, I followed the steps above and CodeBlocks still gives me the error "undefined reference to vtable...".

            • Alex

              Unfortunately, I can't assist further with this one. Sorry :(

              • Christian

                No problem. Thank you for the great tutorial. You have no idea how much I am learning from it!

                • James

                  You defined 'MinFcn' object 'fcn' twice in the same 'if' statement in main() (lines 134 and 142), dunno if that might be giving rise to your issue. Maybe you only meant to use overloaded operator() on line 142?

                  Also, yes I am aware that this is an old thread. Am curious to know if you sorted out your program in the end, and if so what was the issue? Haha.


  • Thanks, this helps lots!

  • Zhijun

    That was awesome!!! It solved the biggest problem I have met these days!
    Thank you

  • Gapi482

    i still have the same problems, it founds the header and comands, but it says there are like 40 undifined references.
    im trying to use SOIL

  • Mirza Safaraz

    hi Alex, how can I use c++ for backend, and where can I learn about it ?

  • trz

    Thank you so much, what a simple and clear tutorial ! helped me to finally link OCCI to c++, after spending HOOURS not being able to find an appropriate tutorial, thank you so much

  • Alexander Bieniek

    I'm having issues with a library I just downloaded. I'm not sure if I'm not setting things up correctly. here's the link:

    First, I can't seem to find the .lib files necessary for step 5 of this tutorial.

    Second, and I have no idea if it's related to the previous issue or not, but when I try to compile, I get an error from a file within the library. Specifically, in id3lib_string.h, "memmove was not declared in this scope", and the same issue with memcpy. From googling around, it looks like that's a member function of <string>, but they do include that in this header file. Unless, maybe <string> is different from <cstring>? Found in this thread:

    • Alex

      Did you download the Windows binaries rather than the source code?

      string is different than cstring -- string contains the std::string class. Header cstring contains a bunch of functions inherited from C to work with C-style strings.

      • Alexander Bieniek

        "Did you download the Windows binaries rather than the source code?"

        I'm pretty sure that the stuff I downloaded was the source code: That's what I'm supposed to have, right? In the thing I just linked, I tried both zips, and neither of them seem to have the .lib file.

        • Alex

          That looks right. I see a .lib file inside the Debug and Release folders.

          • belete

            hi dear, I am using CodeBlocks on windows10,  settings are set to as follows:-
            compiler settings/
                search directories/
                        C:\GDAL\bld\include (contains .h files)
                        C:\\GDAL\bld\lib(contains .lib files)
            project build options/
                linker settings/
                search directories/
                    [same as in compiler settings]
            still I am getting ‘undefined reference to’ errors.. please tell me how to solve it?

  • Ankush

    I am using CodeBlocks on windows7, my settings are set to as follows:-
    compiler settings/
        search directories/
                C:\Python34\include (contains .h files)
                C:\Python34\libs (contains .lib files)
    project build options/
        linker settings/
        search directories/
            [same as in compiler settings]
    still I am getting 'undefined reference to' errors.. please tell me how to solve it?

  • Deepa Kittad

    Hi Alex......

    Please help me in this Issue. It is urgent.....

    I had installed codeblocks in ubuntu 12.04
    I had to add an external library.

    For that i added the library path in linker settings

    I added the include file location in Search Directories also

    I also added library location in Search Directories-->Linker

    when I am using a function xyz from the external library, I am getting undefined reference to xyz

    You can email me the solution to

    • Alex

      Did you #include the appropriate header files in your code? Library files only supply the precompiled code; you still need the declarations to use it.

  • Hi,

    Thanks for you the detailed information, however I would like to know how to download a library?

    I am not able to compile my code in Code Blocks because it says there no such file or directory.

    I know how to link the library with code blocks but I do not know how to download the library to link to with Code Blocks.

    Thanks for your help in Advance.

    • Johny

      Downloading a library will depend on which Operating System you are using.

      On Windows, you will need to download from a website, for example, to download PDCurses, go to and download which is a pre-compiled version of the library.

      On Linux, you can use the package manager to search and download the library.

  • Patrice G

    Hi, thanks a lot for all these useful information !
    I don't understand why we need to say to the linker which library to use for our project (step 5).
    This is already defined with the #include lines in the source files, and in the step 3) and 4) we defined the paths for the compiler and the linker, so they know where to look for h and a (or lib) files.
    It looks like we need to define two times the same thing...

    • Alex

      Header files and libraries may have different names, and there's no direct association between the two. When you #include a header file, the linker can't infer what library file contains the relevant definitions. You have to tell the linker explicitly.

  • Pavel

    Great tutorial, thanks you!

  • I like your C++ course, but some of the ads are a little raunchy.  I don't think I can use this site for teaching high school boys unless you clean up the ads.  Less traffic for you.

  • Programmer2013

    Library NTL for Windows - Codeblocks, perhaps it is easier to install as shown in the video or

  • piciurica

    Hi guys,

    Alex, thank you for the huge amount of work that you've been doing for us! It is very helpful, especially for people like me, the kitchen table learners :d.

    Has anyone worked with PDF Creator Pilot library and Code::Blocks 13.12?

    I ask because I did the following steps:
    - downloaded the library
    - installed it
    - copied the PDFCreatorPilot.dll in C:WindowsSystem32 and C:WindowsSysWOW64
    - added it in the PATH variable
    - restarted the computer
    - copied the PDFCreatorPilot.dll in the directory of the executable of my program
    - followed by the steps depicted in the tutorial

    and for some reason it is not working :(.

    When I include one of the *.h files, the CB gives errors like the following "error: 'DLLEXPORT' does not name a type".

    Is there anything else that I need to do (copy files from/to different locations) or some key lines that are missing from my main?

    Does the order of the steps count?

    Any suggestion is greatly appreciated! Thank you!

  • northyorker

    Thank you very much. I learn to us Codes Block with boost libraries.

  • John Holcomb

    Thank you so much for maintaining this web site. I was absolutely stuck, and this page gave me the knowledge to break
    through the wall that was confronting me. Now I know how to link my compiler to libraries... thanks to you.

  • nhatphutho

    thank you very much!
    Your post is very useful!

  • Mike

    Hi, I need to add libnoise to Code::Blocks in order to work with perlin noise. I added the libraries of libnoise, and made a little test which compiles with no problems. But, whenever I try to build and run the project, it sends me a message which says: undefined reference to 'noise::module::perln::Perlin()', and five similar messages.

    I don't know if I did something wrong. I saved the libraries in C:libnoise. I added in >>Settings>>Compiler and Debugger>>Search directories C:/libnoise/include which contains the headers; and C:/libnoise/bin which contains libnoise.lib

    I did exactly the same in >>Settings>>Compiler and Debugger>>Linker

    As the tutorial shows, In >>Project build options>>Project_Name>>Linker settings I added C:libnoise/bin/libnoise.lib

    In theory, this should be enough to build the project, but it's not.

    Please, somebody who have worked with libnoise or have an idea about what is wrong, please write a reply.

    I had to use / instead of a slash.

  • Jon

    Hey man, I just wanted to give you props for this - you saved me! Code::Blocks is truly fantastic, and I was hoping to get it working.

  • Arash

    Thanks Alex.
    God bless you.

  • Zeus

    This was an amazing tutorial. This was perfect to the problem I had earlier, and it helped me solve it! Your the best Alex thanks!

  • Ismail


    I just wanted to thank whom ever is behind this website, i think it's one of the best
    tutorials i have seen so far on the web, as i have a good background in java, but very newbie
    to C and C++ i think this website is a goldmine for anyone who want to discover these
    fantastic languages, i mean, C C++ Java Perl Pascale etc..

    I have one thing i don't like though, is microsoft all he has done is copying
    like C# it is exactly like java with Capitalized methods.

    I am sorry but as far as microsoft is concerned to me he is just like a parasite.

    Well done guys to everything you're doing

Leave a Comment

Put all code inside code tags: [code]your code here[/code]