i have just found out that Code Blocks (and MingW compiler) only takes .a library files 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.
-
Recompiling the source, probably. – Bo Persson May 29 '11 at 10:11
3 Answers
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.

- 1,654
- 1
- 14
- 22
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 Setting
s tab and add to the Link Libraries
box.
-
okay...is that the exact code i need or is one suppose to be the name of the library? as i am getting errors saying that the directory from that code dont exist... – I Phantasm I May 29 '11 at 22:20
-
No you need to add *your* libraries! There's a file selector dialog to let you do it! – May 30 '11 at 07:16
-
okay so you are saying i simply need to add the .lib files i need to the linker settings :|... because i have already linked them numerous times and it didnt work but when i got a .a file it worked instantly... – I Phantasm I May 30 '11 at 07:19
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.

- 20,287
- 13
- 71
- 105
-
hey...it appears im working with the second form...i have the source code how would i go about making it into an archive? – I Phantasm I May 30 '11 at 06:10
-
@phan 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 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. If you need more detail just post what issues you're having and I'll update my answer as needed. – greatwolf May 30 '11 at 10:36
-
Victor..if you could add the above comment to your answer that would be great. thanks for your help – I Phantasm I Jun 01 '11 at 10:40