How do you actually use the C library?

I am sure that this question has been asked many times, but I cannot understand it. The bear is with me.

So, when you download the library, you get a bunch of .c and .h files, as well as many other things. Now say that you want to write a program using this library.

I copy all .h files to the project directory. It just doesn't compile.

Great, so I get the library as a bunch of .dlls, and I copy the DLL to my project directory. Still not compiling.

How it works?

What do you do, for example, right after creating a folder for your project? What parts of the library package do you copy / paste into the folder? How do you do it so that it can compile? Go through the steps with me, please.

  • Where to place .h files?
  • Where to place dll files?
  • How to compile?

Thanks.

(The library I'm trying to get is libpng, I'm on Windows with MinGW, and I'm looking for compilation from the command line, as usual.)

(from what I am collecting, you put the .h files in directory A and the DLL files in directory B, and you can use the -l and -L compiler options to tell the compiler where to find them, is that right?)

+3
source share
7 answers

Running it under windows (suppose a Visual Studio user)

  • After unpacking, add directories to the library with the settings of your projects (Project β†’ Properties β†’ C / C ++ β†’ Additional Include Directories)

  • Do the same for the library directory (Project -> Properties -> Linker -> Additional Library Directories)

  • : Project β†’ Properties β†’ Linker β†’ Input β†’ Additional Dependencies

, , .

Visual Studio (Tools β†’ Options β†’ Project and Solutions), , - .

, , Makefile:

  • .
  • include -I g++
  • -L g++
  • , : -llibrary name (: -lxml2 libxml2.so)
  • : library name.a

, :

g++ -I/work/my_library/include -L/work/my_library/lib -lmylib my_static.a -o appname_exe MYFILE.CPP

( )

, makefile - .

+6

, C:

  • - , , . , , , #include , . , . GCC -I .

  • - . . , .. , , , . Linux .a .so, .o. GCC -L.

, :

gcc myProg.c -I/path/to/libpng/include -L/path/to/libpng/lib -lpng -o myProg.exe

( , -l GCC lib , -lpng libpng.a.)

, .

+8

.lib - , ".lib" , . , , Linux... Windows.

".lib" / .dll.

+3

. , (, dll) , . .

, :

(1) (.h) ,

(2) (.lib) .

, (DLL), , , system32.

+2

, , , , , -L . -I . -lpng. -L , , - , , -lpng png.

[] , /include, - /libs. -I . -L.

-I ./include -L ./libs

[Edit2] makefile . ,

+1

: ( ).

, .

(.dll .so), . , , , , runtime (.dll .so).

, () , .

"" , , . . , , .

+1
source

In addition,
you can watch Support for dynamic loading in different languages ​​and on different platforms.
This support is very convenient in cases when you want to use the library additionally, and you do not want your program to fail if the library is not available.

0
source

Source: https://habr.com/ru/post/1723111/


All Articles