简体   繁体   English

如何维护自制库?

[英]How to maintain self-made libraries?

I'm using Qt Creator 2.7.0 on ubuntu 13.04. 我正在Ubuntu 13.04上使用Qt Creator 2.7.0。

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: 我刚发现我需要在应用程序的.pro文件中包含以下内容,才能使用自己的库:

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 . 如您所见,我的所有项目都放在一个名为Programming的文件夹中(在本例中为.. ),每个项目的根目录都有.pro.pro.user文件,子目录/src有源文件,而发行版在另一个子文件夹/release

So, this is what my Programming folder looks like: 因此,这是我的Programming文件夹的外观:

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. 但是,我认为可以创建一个文件夹Programming/libs/ ,并在该libs文件夹中添加libMyLib.amyclass.h文件。 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. 问题是,我将获得存储在计算机上的每个库的include路径,并且libs文件夹会变得一团糟,尤其是如果在不同的库中有两个具有相同名称的标头时。

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? 我真的是使用库的新手,是否有关于如何在您的计算机上找到它们以及如何将它们包含到您的项目中的通用解决方案?

You could mimic libraries like Boost and have a directory tree like this: 您可以模仿诸如Boost之类的库,并具有如下目录树:

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. 这样,您将拥有一个源外构建树(在build/ ),这样二进制文件就不会与源文件混淆了。

The lib/ and include/ directories are handy because you can then define an install build target. lib/include/目录非常方便,因为您随后可以定义install构建目标。 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). 它将所有内容复制到usr/local/libuser/local/include以便您的App只需执行#include <MyLib/some_header.h> ,您就可以直接针对库二进制文件进行链接(因为您将所有内容复制到了您的系统范围路径)。 There is also no danger of name clashes because you wrapped it inside your own MyLib subdirectory. 也没有名称冲突的危险,因为您将其包装在自己的MyLib子目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM