问题
I would like to compile a static library out of all the functions I have written for a C++ project. I am using CodeLite 11.0.0 on Ubuntu 16.04, configured to use GCC as compiler.
I have followed the instructions in CodeLite's tutorial, according to which this should be possible, and changed the project type from Executable to Static Library.
After running the project (CTRL+F5 command), I expected to find a .a
file in the /Debug
folder, either along with, or in place of the executable file. All I could find, though, was the executable and a number of .o
and .o.d
files. The same I was finding when the project was set to Executable.
I tried to close and reopen CodeLite, but it did not help. I can't find any official/unofficial example of how to build a static library with CodeLite.
Does anyone know how to set-up CodeLite to produce a .a
static library file?
回答1:
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 inlibmyprog.a(main.o)
, solibmyprog.a(main.o)
is not linked and might as well not exist.The linkage of
newprog
does need some function,foo
, defined inlibmyprog.a(main.o)
, solibmyprog.a(main.o)
is linked; then as well as the definition offoo
, the program links duplicate definitions ofmain
- its own definition plus the one inlibmyprog.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.
来源:https://stackoverflow.com/questions/46851515/how-to-compile-a-static-library-with-codelite-11-0-0