Как добавить библиотеку в codeblocks
Перейти к содержимому

Как добавить библиотеку в codeblocks

  • автор:

A.3 — Using libraries with Code::Blocks

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.

Создание проекта статической библиотеки

1. Выбрать в главном меню (см. рис. 1 ) команду File → New → Project. . .

(Файл → Создать → Проект).

Руководство по работе в среде разработки Code::Blocks – 3 / 60

Выбор шаблона проекта библиотеки

Шаблон Имя проекта Каталоги Новое имя Функция Параметры Пути Файл Шаблон

Рис. 2: Выбор шаблона проекта статической библиотеки

Руководство по работе в среде разработки Code::Blocks – 4 / 60

Шаблон Имя проекта Каталоги Новое имя Функция Параметры Пути Файл Шаблон

Выбор шаблона проекта библиотеки (окончание)

2. В окне выбора шаблона нового проекта (см. рис. 2 ) выбрать шаблон Static library (Статическая библиотека), затем нажать кнопку Go (Вперёд). Запустится мастер создания проекта.

Linking a Library in Code::Blocks

I’ve not installed Code::Blocks on a Linux computer, but for a moment I’ll pretend that such an installation has the same issue I described in last week’s Lesson: You must manually link in the C language math library to create any program that uses a math.h function.

More importantly, to expand the C language’s capabilities, you frequently need to mix in another library. The standard library is linked by default, but pretend that you’re using a library that creates graphics, sound, or provides other specific functions beyond the normal scope of C. You must specify that such a library is linked into the final program.

The library also comes with a header file that provides function definitions, constants, structures, and other whatnot. The header file, however, is not the same as the library. The library is what contains the code that makes the functions work.

On the command line, you use the -lx switch to link in library x. In Code::Blocks, things work differently.

As an example, suppose you need to link in the math library in a Code::Blocks project. You don’t have to on a Mac or PC, but these are the steps you would use. And you can follow the steps to link in any library, not just the math library. Some huge projects require multiple libraries.

After creating the Code::Blocks project, follow these steps to add a library:

1. Choose Project, Build Options.

The Project Build Options dialog box appears.

2. Click the Linker Settings tab.
3. Click the Add button.
4. In Add Library dialog box, click the Ellipsis (…) button to browse to the location of the library file.

Use the Choose Library to Link dialog box to find the library’s location on your computer’s mass storage system:

  • For the MinGW compiler, the location for standard libraries is MinGW\lib , with the full path: C:\Program Files (x86)\CodeBlocks\MingGW\lib\ for a standard Code::Blocks installation. If the library was saved elsewhere, you must browse to that specific directory (“folder”).
  • With XCode on the Mac, the library locations differ depending on the version of XCode. In the current version, C language libraries are kept in /Applications/Xcode.app/Contents/Developer/ . Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib . Ugh.
  • In Linux, and other Unices, the traditional location for C libraries is /usr/lib .

If you were linking in the math library — which isn’t necessary on a PC, but doing so doesn’t screw up anything — you would locate the file named libm.a , which is the way MinGW names its libraries. On the Mac, the math library is named libm.tbd .

5. Select the library file and click the Open button.
6. Click No to avoid using a relative path.

I’m a fan of direct or absolute pathnames.

7. Click OK to add the library to the list.
8. Click OK to close the Project Build Options dialog box.

9. Choose File, Save Project.

The library is linked after the code is compiled. That process allows the library’s functions to access their magic. Otherwise you’d get those ugly linker errors.

In next week’s Lesson, I cover how to locate and install bonus C language library files.

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

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.

3 Answers 3

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

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *