As you've probably discovered, CodeLite allows you to change the type of a
project in the drop-down menu from Settings -> General -> Project type.
Doing so, however, does not change the name of the project target. So, if
you started off your project as an executable myprog
- from which, say, the
Debug build generated ./Debug/myprog
under the project folder - then
you change the project type to static library and rebuild it, the Debug
build will still generate ./Debug/myprog
, but that file will now in fact
be a static library, lacking the customary lib
-prefix and .a
extension.
To give the output file a conventional static library name -
libmyprog.a
- you need to go back into Settings -> General and
change Output File from:
$(IntermediateDirectory)/$(ProjectName)
to:
$(IntermediateDirectory)/lib$(ProjectName).a
Then rebuild the project and it will output a target that is a static
library and looks like one.
Of course, you must make the same changes to the project settings in both
the Debug and Release configurations if you want them both to produce
targets with the same file type and file name.
However...
If this way of converting a program project to a static library project does not
seem very slick, that could be because it is a conversion of very little use.
The static library produced after the conversion will contain exactly the same object
files that the program was built from, including the object file that defines
the main
function of the original program. Let's suppose that object file
is main.o
, and that it defines 0 or more other functions that the linker can see.
Any other program, newprog
, that is linked with the static library must provide
its own main
function in a different object file, so in any such linkage one of
two things must happen:-
The linkage of newprog
does not need any function defined in libmyprog.a(main.o)
,
so libmyprog.a(main.o)
is not linked and might as well not exist.
The linkage of newprog
does need some function, foo
, defined in libmyprog.a(main.o)
,
so libmyprog.a(main.o)
is linked; then as well as the definition of foo
, the
program links duplicate definitions of main
- its own definition plus the
one in libmyprog.a(main.o)
. Duplicate definitions are an error, so the linkage fails.
Putting a definition of some program's main
function into a member of a static
library is pointless, because if that member is ever needed in the linkage of another
program then its linkage will fail.
So converting your program project to a static library project calls for some
refactoring prior to the conversion:-
If any function that you want in the static library is defined in the same source
file as main
, then you need to take it out of that source file and define it
is a different one.
After that, remove the source file that defines main
from the project.
Lastly, convert and rebuild the project.
You have to do that refactoring to extract from your original program source code
a bunch of source files that are suitable for building into a static library.
Assuming you've done that, the straightforward way to create a static library with
CodeLite is to create a project for that purpose and in the New Project Wizard
choose Library -> Static Library as the project type instead of some kind
of executable.
Then just add either new or existing source files to the static library project
until it contains definitions of all the functions you want the library to
provide. Build, test, debug, edit... until done.