Menu

Dev-C++ (devcpp-4.9.9.2)

pete509
2008-01-12
2012-09-26
  • pete509

    pete509 - 2008-01-12

    How do I create a Library and use it in a C program?

     
    • cpns

      cpns - 2008-01-12

      Most of the work od configuring for a library build is done for you when use the Static Library project template (File->New->Project).

      When you name your project it is worth following GNU convention and prefixing it 'lib'; so library XXX would be called libXXX.

      The project template causes Dev-C++ to invoke the librarian/archive tool (ar.exe) rather than the linker when building the code. This places all the object files into the archive as a library.

      You simply add (or create) source code to the project, any symbol (function, class, or data) with external linkage (i.e. not declared static and with file level scope) will be available to code that links your library.

      Your library should not normally contain a main() function (unless perhaps you are developing an 'application framework' with an alternative entry point).

      Since whole object files are extracted and linked to an application it is recommended that you use separate compilation to provide fine granularity. If you put all your functions or classes etc. into one source file, they will all get linked to your code whenever even just one is used whether they are required or not.

      To use your library in another project, you can do one of two things:

      1) Add a library path to the project's Library directories (this adds a -L<path> option to the linker command line - you can see this in the build log), and then add -lXXX (where the library is called libXXX.a - that's a lowrecase-L BTW, not a number one) to the linker options.

      2) Add the fill path and file name to the linker options (or in Dev-C++ click the "Add object file or library" button.

      The first method is most useful when you have a collection of libraries in one folder because it keeps the command line shorter.

      I hope that helps. If you get any problems building the library or the application using it do post the full "Compile Log" as this will tell use exactly what you have done, (probably more accurately and completely than you can), rather than what you think you did (although you can tell us that too)!

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.