Hello. I have a library, which is a set of header files and source files and written in C ++. It describes many different classes that are used in my other projects. Can I compile my library into a static library, and then in my other projects connect it and use classes from it. I myself write in C ++ for a long time and I don’t know such subtleties.

    2 answers 2

    Can. You will ship .lib (or .a - depends on the platform) and headers

      It is possible, but carefully. The library and the program using it must be compiled with the same compiler. Some compilation keys must match.

      Alternatively, instead of lib, you can use obj-files that are created automatically. They work in the same way as lib, but, unlike lib, there is one obj for each compilation unit (cpp-file).

      • one
        1. Well, the requirement of "one compiler" is too strict. Correct will be "compatible" (although the answer to someone who is compatible, I am not ready to answer). 2. .lib are created from .obj (in linux - .a from .o). And, library tools, the object file can be extracted from the library. - alexlz
      • > It would be more correct to "compatible" (although the answer to who is compatible with whom I am not ready to answer). I read that it should be compiled by just one compiler. It came across many times. I myself do not use lib and use only one compiler, so I did not check it. But I can assume that compilers call C ++ functions in different ways and do different alignment of structures and classes. > .lib are created from .obj (in linux - .a from .o). I know that. I meant that it is not necessary to create lib. You can use just obj, and in the same way. - gammaker
      • 2
        @GLmonster but obj instead of lib is awesome. Constantly edit the Makefile or include the necessary obj in the project (and there are a lot of them) ... Or include everything and hope that the clever linker will throw out the extra ones? - alexlz
      • 2
        MinGW produces .o files in COFF format, MSVC in theory also in COFF (or, alternatively, MS-COFF. COFF is a subset of MS-COFF) - they are compatible with each other. But the Borland compilers (now Embarcadero) produce object files in OMF format. To convert from COFF to OMF and back there are corresponding utilities. This is all under Windows. Under Linux, object files are in ELF format. - insolor