How to convert multiple source files to a single .a file

I just found out that Code Blocks (and the MingW compiler) only accepts .a files, not .lib files, which is the easiest way to convert .lib files to .a files ... any tutorials, etc. will be appreciated. Edit let me change my question a bit, how can you convert multiple source files into one. archive file.

+1
source share
3 answers

.lib (and .a also) works with two different features:

  • Like an import library for a common Dll that your program depends on.
  • Like an archive library created from one or more object files compiled from a source.

You did not indicate in your question what form you are working with. If you want to make the second form, you will need to create the source in gcc as a static library, as suggested by Persson, since the library format used by msvc is not compatible.

However, there is good news if you are working with the first form. The MinGW gcc port should work transparently with a coff-format import library created using msvc development tools. You can simply rename *.lib import to lib*.a and just pass it to the linker like any other *.a file. The linker should be able to sort its real format.

I tested this under tdm-gcc-4.5.2 with the last overnight build of code blocks and it seems to work fine.

Edit: Combined my comment as part of the answer. If you are working with the second form and you need to create a static library from the source code, the steps are approximately as follows:

If an existing msvc project exists, you can use it as a starting point by importing it into code blocks. It will be located under File-> Import Project-> Visual Studio, etc.

If none of them are available, creating a new C :: B project from scratch is not too difficult. Just go to File-> New-> Project, select "Static Library" as the type of project, then add any source file. If the library is written in semi-portable material, it should compile without any problems.

+1
source

To answer a specific question, how to convert several source files into one archive file for static linking; This is a two-step operation. You must first compile the source files into object files, and then turn the object files into an archive.

If you have MSYS with MinGW installed, I recommend using this. If not, you can still use the cmd.exe Windows command prompt.

Make sure your MinGW / bin directory is part of the PATH environment variable, so you can call the compiler from anywhere.

At the command prompt, change to the directory containing the source code. From there enter the command

 mingw32-gcc -O2 -c [files] -I[header directory] 

You must either specify [files] specifically, -c a.cpp b.cpp c.cpp , or you can identify them all with *.cpp . [header directory] where the .h files for the source are relative to you. Typically, the source files will be in the directory themselves / src and the header files in a sister directory called / include. You reference this directory as -I../include . If the header files are in the / include directory in the / src directory, this will be -Iinclude .

Once you have generated the .o files, enter the command

 ar rcs lib[something].a [files] 

Replace [something] with the name of the library. This is the name that will appear in the Link Libraries dialog box in Code :: Blocks. Files is either the name of previously created object files (ao, bo, co), or if there are no unrelated object files in the directory, you can enter *.o .

This should lead to the creation of an archive file in the directory. Now you can put it in the appropriate directory (possibly the sisters directory for / include called / lib) and add this directory to the Code :: Blocks configuration in the link lookup directories. Then you should remember how to add a library project for you.

+2
source

MinGW can use .LIB files. The following links to .LIB created using the MS compiler:

 gcc bc a.lib 

In the code blocks, you will add the library in the Project|Build Options... dialog box, and then go to the Linker Setting tab and add to the Link Libraries field.

0
source

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


All Articles