简体   繁体   English

C ++导入库按来源

[英]C++ import library by source

I'm new to C++ and wonder if it is good practice to include a library by source code. 我是C ++的新手,想知道按源代码包含库是否是一种好习惯。 If it is, what would be the best way to achieve this? 如果是这样,那么实现这一目标的最佳方法是什么? Just copying in a subfolder and using include? 只是在子文件夹中复制并使用包含?

In my special case, I have written a small library and I'm going to use it on two different microprocessors. 在我的特殊情况下,我编写了一个小库,将在两个不同的微处理器上使用它。 Compiling the library separately, copying all headers and using this "package" seems to be overkill for me. 对我来说,分别编译该库,复制所有标头并使用此“包”似乎是过大的。

Normally libraries are used as libraries because it is much easier and comfortable that way. 通常,将库用作库,因为这样更容易使用。 If you are using dynamic libraries (.dll or .so) things get even better because you can replace libraries on the fly and things should continue to work smoothly. 如果您使用的是动态库(.dll或.so),则情况会变得更好,因为您可以随时替换库,并且应继续平稳运行。

You decided to use code repositories instead of libraries which means probably more work for you. 您决定使用代码存储库而不是库,这可能意味着您需要做更多的工作。 If you are happy this way that's OK, but just make sure you do not break any license, some lgpl packages (like Qt) clearly require their libraries to be linked dynamically. 如果您对这种方式感到满意,那么可以,但是只需确保您不破坏任何许可证,某些lgpl软件包(例如Qt)显然需要动态链接其库。

The best way to do this: hard to say but in your place I would probably use git and include the libraries as submodules. 最好的方法是:很难说,但在您的位置,我可能会使用git并将库作为子模块包含在内。

Compiling the library separately is what should be done. 应该分别编译该库。

It's not that overkill either : you're just compiling the .o files for your library, then wrapping them in an archive and handling that archive around. 不是矫枉过正或者:你刚刚编译.o文件库中,然后包裹在他们的档案和周围处理该存档。

Just #include ing source code is a bad idea since it means just to copy the code into your own, things can go wrong that way. #include源代码是一个坏主意,因为这意味着仅将代码复制到您自己的代码中,那样可能会出错。 For example if there is a static variable somewhere in the library code and the same named static variable in your code you will have a conflict. 例如,如果库代码中某处有一个静态变量,而代码中有相同名称的静态变量,则将发生冲突。

Instead you should probably compile the library separately and link it, possibly the same way as you would do anyway (ie you build the library and then you link with that library). 取而代之的是,您可能应该分别编译该库并链接它,这可能与您无论如何都要进行编译(即,建立该库然后与该库链接)相同。 But the light weight alternative would be just to compile the additional C++ files and then link the object files together to an executable. 但是,轻量级的选择只是编译其他C ++文件,然后将目标文件链接到可执行文件。 Details on how you do that is compiler specific. 有关如何执行此操作的详细信息是特定于编译器的。

There's valid reasons for including the library source in this way, for example if your project needs to modify the library during development it would be easier to do so if the rebuilding of the library is done as a part of the build process of the project. 有充分的理由以这种方式包括库源代码,例如,如果您的项目在开发过程中需要修改库,那么如果将库的重建作为项目构建过程的一部分来完成,则这样做会更加容易。 With a well designed build process the library shouldn't have to be rebuilt unless there are actual changes to it. 通过精心设计的构建过程,除非对该库进行了实际更改,否则不必重建该库。

The value of a library is in part that you link it more often than you compile it, leading to a net saving. 库的价值部分在于,与编译相比,链接的频率更高,从而节省了资金。

If you control all the source, then whatever build process works best for you is fine. 如果您控制所有源代码,那么最适合您的构建过程都可以。

I agree with πάντα ῥεῖ but I'll also add that the reason it is bad practice is because the compiled library can be stored in your computer in a common location and used by tons of different programs, thereby reducing the amount of data your computer has to store, in memory as well as RAM(if more than one running program uses the same library). 我同意πάνταῥεῖ,但我还要补充一点,这是不好的做法,原因是编译后的库可以存储在您计算机中的同一位置,并且可以被大量不同的程序使用,从而减少了计算机拥有的数据量存储在内存以及RAM中(如果有多个正在运行的程序使用同一库)。 An example is openGL which is a library that many games use and is probably already in your system somewhere. 一个示例是openGL,它是许多游戏使用的库,可能已经在系统中的某个地方。 If you use windows, software installers link up these libraries to their programs and add them if you don't have them. 如果使用Windows,则软件安装程序会将这些库链接到其程序,如果没有,则将其添加。 If you use linux, you will be notified if libraries are missing and prompted to install them. 如果您使用linux,则会在缺少库的情况下收到通知,并提示您安装它们。 All of that aside, you can, technically use un-compiled libraries but that introduces a number of potential licensing problems as well as additional problems with THEIR dependencies. 除了所有这些,您可以从技术上使用未编译的库,但这会带来许多潜在的许可问题以及THEIR依赖项的其他问题。

By copying source code to other projects and "mixing" it with other source code will stop this library from being a "library". 通过将源代码复制到其他项目并将其与其他源代码“混合”,将阻止该库成为“库”。 Later on you will be tempted to make a small change in one copy (for CPU) or fix a bug and forget to do the same in the other copy. 稍后,您将倾向于在一个副本(对于CPU)中进行少量更改或修复错误,而忘记在另一副本中进行相同的更改。

There might be additional consideration but you should try to keep the code in one place. 可能还有其他考虑,但是您应该尝试将代码放在一个地方。 Do not Repeat Yourself (DRY) is a very strong and fundamental principal of software engineering with many benefits. 不要重复自己(DRY)是软件工程中非常重要的基础,它有很多好处。

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

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