Skamah One
Skamah One

Reputation: 2456

How to maintain self-made libraries?

I'm using Qt Creator 2.7.0 on ubuntu 13.04.

I've just recently ran into the idea of using libraries, and they're a whole new thing for me. I've just figured that I need to have the following in my application's .pro file to use a library of my own:

LIBS += -L<lib's dir> -l<lib's name>
INCLUDEPATH += <headers' dir>

// for example:
LIBS += -L$$PWD/../MyLib/release/ -lMyLib
INCLUDEPATH += $$PWD/../MyLib/src/

As you see, I have all my projects in a folder called Programming (the .. in this case) Each of my project have .pro and .pro.user files in the root, source files in a sub folder /src and the release in an other sub folder /release.

So, this is what my Programming folder looks like:

Programming
   MyLib
      MyLib.pro
      MyLib.pro.user
      src
         myclass.h
         myclass.cpp
      release
         libMyLib.a
         Makefile
         myclass.o
   MyApp
      MyApp.pro
      MyApp.pro.user
      src
         main.cpp
      release
         main.o
         Makefile
         MyApp

However, I figured that I could create a folder Programming/libs/, and add libMyLib.a and myclass.h files inside that libs folder. I would do the same for all of my libraries, and then I could always include them like this:

LIBS += -L$$PWD/../lib/ -lMyLib
INCLUDEPATH += $$PWD/../lib/

The problem is, I'd get include path for every library stored on my computer and the libs folder would become a mess, especially if there are two headers with same name on different libraries.

I'm really new to using libraries, is there a general solution on how they should be located on your computer, and how to include them into your projects?

Upvotes: 1

Views: 260

Answers (1)

TemplateRex
TemplateRex

Reputation: 70556

You could mimic libraries like Boost and have a directory tree like this:

MyLib
    build
        Makefile, .pro or .sln file here
    lib
        MyLib
           // your .so / .a here
    include
        MyLib
           // your .h here
    src
           // your .cpp here
    CMake or qmake file here

This way you have an out-of-source build tree (in build/) so that your binary files are not mixed up with your source files.

The lib/ and include/ directories are handy because you can then define an install build target. If you then type

 make install

it will copy everything to usr/local/lib and user/local/include so that your App can simply do #include <MyLib/some_header.h> and you can link directly against your library binaries (because you copied everything to a location in your system wide path). There is also no danger of name clashes because you wrapped it inside your own MyLib subdirectory.

Upvotes: 2

Related Questions