开发者

How to convert multiple source files into one .a file

i have just found out that Code Blocks (and MingW compiler) only takes .a library 开发者_如何学Pythonfiles rather then .lib what is the easiest way to convert .lib to .a files... any tutorials etc would be appreciated. Edit let me modify my question slightly how can you convert numerous source files into one .a archive file.


To answer the specific question, how to convert several source files into a single archive file for static linking; it is a two step operation. You must first compile the source files into object files, followed by turning the object files into an archive.

If you have MSYS with your MinGW installation, I recommend using that. If not, you can still use Windows' command prompt cmd.exe.

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

From the command line, move into the directory that holds the source code. From there, type the command

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

You should either name the [files] specifically, -c a.cpp b.cpp c.cpp or you can identify them all with *.cpp. [header directory] is where the .h files for the source are, relative to you. Generally, source files will be in a directory by themselves called /src, and header files in a sister directory called /include. You would refer to that directory as -I../include. If the header files are in a directory called /include within the /src directory, it would be -Iinclude.

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

ar rcs lib[something].a [files]

Replace [something] with the name of the library. This is the name that will go in the Link Libraries dialog in Code::Blocks. Files is either the name of the object files you created before (a.o, b.o, c.o) or, if there are no unrelated object files in the directory, you can put in *.o.

That should result in the archive file being created within the directory. You can now place it in the proper directory (possibly a sister directory to /include called /lib), and add that directory to your Code::Blocks configuration, under Linker Search Directories. You must then remember to actually add the library for you project.


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

gcc b.c a.lib

In codeblocks, you add the library in the dialog Project|Build Options... and then go to the Linker Settings tab and add to the Link Libraries box.


The .lib (and .a also) works under two different capacities:

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

You didn't really specify in your question which form you're working with. If you're looking to do the second form, you'll have to build the source under gcc as a static library as suggested by Persson since the library format used by msvc tools aren't compatible.

However, there's some good news if you're working with the first form. The MinGW port of gcc should be able to work transparently with a coff-format import library created with msvc development tools. What you can do is simply rename the *.lib import into lib*.a and just pass that to the linker like it's any other *.a file. The linker should be able to sort out its real format.

I've tested this under tdm-gcc-4.5.2 with the latest nightly build of codeblocks and it appears to work ok.

Edit: Merged my comment as part of the answer. If you're working with the second form and need to build a static library from source, the steps are roughly as follows:

If there's an existing msvc project you can use it as a starting point by importing it into codeblocks. It'll be under File->Import Project->Visual Studio etc.

If none is available making a new C::B project from scratch isn't too difficult. Just goto File->New->Project select 'Static Library' as the project type then add whatever source file is needed. If the library is written in a semi-portable matter it should compile without too much trouble.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜