Search

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
Index
A.2 -- Using libraries with Visual Studio 2005 Express

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

  • Ismail

    Hello,

    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
    Regards
    IB

  • 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!

  • Arash

    Thanks Alex.
    God bless you.

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

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

  • nhatphutho

    thank you very much!
    Your post is very useful!
    🙂

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

  • northyorker

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

  • 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!

  • Programmer2013

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

    https://www.youtube.com/watch?v=PflBixSFyqA or

    https://www.youtube.com/watch?v=PflBixSFyqA&list=PLV5kbONWTSe24-KwLEE3OFpuXl_WTuzyd

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

  • Pavel

    Great tutorial, thanks you!

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

  • 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 http://sourceforge.net/projects/pdcurses/files/pdcurses/3.4/ and download pdc34dllw.zip which is a pre-compiled version of the library.

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

  • 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
    /home/deppa/Desktop/Codeblocks_Projects/codeblocks/CSAL/libcsal_lib.a

    I added the include file location in Search Directories also
    /home/deppa/Desktop/Codeblocks_Projects/codeblocks/CSAL/include

    I also added library location in Search Directories->Linker
    /home/deppa/Desktop/Codeblocks_Projects/codeblocks/CSAL

    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.

  • Ankush

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

  • 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: http://id3lib.sourceforge.net/

    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: http://stackoverflow.com/a/2505376

    • Alex

      Did you download the Windows binaries rather than the source code? https://sourceforge.net/projects/id3lib/files/windows%20binaries/3.8.3/

      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: https://sourceforge.net/projects/id3lib/files/id3lib/3.8.3/ 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.

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter