Install a library compiled in Codeblocks with MinGW
IT Tricks Random

Install a library compiled in Codeblocks with MinGW


#Install #library #compiled #Codeblocks #MinGW

You have a version of CodeBlocks that runs with the MinGW compiler and has retrieved the binary from an external library that you want to use and which has also been compiled for MinGW.

In the following example, we will use the libcurl library to illustrate the method to be used on the date this tip was created and for the current stable version of libcurl:

http://curl.haxx.se/gknw.net/7.28.1/dist-w32/curl-7.28.1-devel-mingw32.zip

(see the current version at http://curl.haxx.se/download.html)

This method can be used with Codeblocks using MinGW for any library compiled for MinGW.

2. Method

2.1 Unpacking in a directory that centralizes the external libraries used

Unzip the file with the compiled library in a not too complicated directory and with a name without spaces.

Ex: c: libs curl-7.28.1-devel-mingw32

2.2 Integration to the Codeblocks project: 2 steps

In Codeblocks, there are 2 things to do so that a project can use an additional library:

2.2.1 Tell the compiler where to find the .h files

Tell the compiler where to find the .h files, to avoid making mistakes when typing #includes that correspond to the library in the source code.

This is done in: “Project – Build options …” – Click on the project root (not on Debug or Target targets, otherwise the settings will be applied to the target only)

“Search directories” – Add tab – browse the library directory with .h files (for example, in the case of libcurl c: libs curl-7.28.1-devel-mingw32 include) and select this directory.

2.2.2 Provide the editor with links to find the library binaries

Indicate to the editor the links where he should find the library binaries that will allow him to generate the executable, after compilation. For the MinGW environment, the library binaries are files whose extension is .a.

This is done in: “Project – Build options …” – Click on the project root (not on Debug or Target targets, if not, the settings will be applied to the target only, unless you use unlock libraries for a particular library, where it is useful to distinguish, depending on the targets)

“Linker settings” – Add tab – browse to the directory with the library containing the .a files (eg in the case of libcurl cc: libs curl-7.28.1-devel-mingw32 lib) and select the parts of the library that we need as part of the project.

If you don’t know, see the library’s documentation (reflectively, compile the project, see the error messages and add the necessary parts or, radically, add everything).

3. Observations

3.1 .dll files

Depending on how you set up your project (dynamic or static link), you may have to copy .dll to the executable directory (or to the Windows PATH) so that the dynamic download of the library can be done.

In the case of libcurl, the .dlls are not c: libs curl-7.28.1-devel-mingw32 bin

They would be added, for example, in the project’s “Target” directory, containing the executable generated by the compilation (or somewhere in PATH), and this will only be necessary in compilation with dynamic linking and, to launch the executable out of the environment of development.

If you have not already done so, you will see, during execution (by double-clicking the .exe file, ie.): A message will warn you that this or that .dll was not found.

3.2 Relative or absolute paths

When setting up Codeblocks (see steps 2.2.1 and 2.2.2), after selecting a resource, you will be asked to answer this question “Keep this as a relative path?”.

If the answer is yes, it means that CodeBlocks will not maintain the absolute path to the specified resource (ex: “c: libs curl-7.28.1-devel-mingw32 lib libcurl.a)”, but only the relative path in relation to the project directory (eg, something like “.. .. .. libs curl-7.28.1-devel-mingw32 lib libcurl.a).

It is useful to specify a relative path if resources are available in the project directory, as the project will remain compilable on another machine with CodeBlocks, since the resources are in the same place in relation to the project directory and that, whatever the location of the project on the new machine.

On the other hand, if the resources are elsewhere (it is better to put them elsewhere, to bring libraries together in an identified place, where projects can find them, such as c: libs and avoid duplicating them), and if we move the project, the references will be broken. In this case, it is better to specify absolute paths.

3.3 Do not copy the external library in the Codeblocks MinGW directories

This can work and you can find tutorials that will tell you what to do.

However, I do not recommend this, unless you want to:

  • mess up your CodeBlocks installation
  • having to reinstall all your libraries to update CodeBlocks or MinGW and the default CodeBlocks or MinGW directories will be deleted and recreated in the process
  • not knowing where that version of the library is located that only compiles with that source that you use
  • not knowing how to update their libraries

3.4 Terminology

In the lines above, we use the term “library”. The English term “library” is widely used by programmers.

Photo: © Roman Samborskyi – 123RF.com

Leave a Reply

Your email address will not be published. Required fields are marked *