包括目录与lib目录概念问题 [英] include directories vs. lib directory concept question

查看:117
本文介绍了包括目录与lib目录概念问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

链接到包含文件和链接到lib文件之间有什么区别?

What is the difference between linking to include files versus linking to lib files?

我对C / C ++相当新,我很难理解使用include文件和静态lib文件来调用函数之间的区别。在我看来,include文件具有一个可以像.lib文件一样调用的函数。

I am fairly new to C/C++ and I'm having a hard time figuring out the difference between using include files and a static lib file to call functions. In my mind, the include files have functions that one can call just like .lib files.

推荐答案

其他类似语言)一个函数被称为具有一个声明定义

In C++ (and C and other similar languages) a function is said to have both a declaration and a definition.

一个简短的声明,声明该函数存在,它的界面看起来像。考虑一个将两个整数加在一起的基本函数 add 。它的声明可能如下所示:

The declaration is simply a short statement that declares that the function exists, and what it's interface looks like. Consider a basic function add that adds two integers together. It's declaration might look like the following:

int add(int, int);

这意味着存在一个函数 add 取两个整数并返回一个整数。它不指定函数实际上做什么,尽管事实上我们可以根据它的名字做出一个好的猜测。

This means "there exists a function add that takes two integers and returns an integer". It does not specify what the function actually does, despite the fact that we can make a good guess based on its name.

函数的定义是我们定义的 的功能。这可能是你认为是实际的功能代码。使用 add 函数作为示例:

The definition of the function is where we define exactly what the function does. This might be what you consider to be the actual function code. Using the add function as an example:

int add (int a, int b)
{
    return a + b;
}

那么这与你的问题是否相符呢?嗯,假设我们在 math.cpp 中有一些数学函数:

So how does this fit with your question? Well, suppose we have a number of math functions in math.cpp:

// math.cpp

int add (int a, int b)
{
    return a + b;
}

int sub(int a, int b)
{
    return a - b;
}

还假设我们决定使用其中的一些在我们的main函数中 main.cpp

And also suppose we decide to use some of these in our main function in main.cpp:

// main.cpp

int main (int argc, char* argv[])
{
    printf("1 + 2 = %d\n", add(1, 2));
    printf("8 - 3 = %d\n", sub(8, 3));
}



如果您尝试编译 main.cpp ,它会抱怨它不知道 add sub 是什么。这是因为你试图使用它们而不声明它们存在 - 这正是一个声明。所以你可以这样做:

If you try to compile main.cpp as it is, it will complain that it doesn't know what add and sub are. This is because you are trying to use them without declaring that they exist - which is exactly what a declaration is for. So you might do the following:

// main.cpp

int add(int, int);
int sub(int, int);

int main (int argc, char* argv[])
{
    printf("1 + 2 = %d\n", add(1, 2));
    printf("8 - 3 = %d\n", sub(8, 3));
}

这将工作,但不是很灵活。如果我们添加一个新的函数 mul ,我们需要把它的声明添加到 main.cpp code> .cpp 文件(如果你有很多 .cpp 文件,这是很多工作)。所以我们做的是,而是我们把所有的声明放在一个单一的文件(比如说, math.h ),所以我们只需要保持声明的列表在一个地方。然后,我们简单地将 math.h 包含到使用数学函数的任何文件中。这是头文件(a.k.a.包含文件)的目的。

This would work, but is not very flexible. If we add a new function mul, we need to go and add its declaration to main.cpp and every other .cpp file that uses it (which is a lot of work if you have a lot of .cpp files). So what we do, is instead we put all the declarations into a single file (say, math.h) so we only have to maintain the list of declarations in a single spot. Then, we simply include math.h into any file that uses the math functions. This is the purpose of header files (a.k.a. include files).

这很好,但可以更好。就像这样,我们有一个 main.cpp 文件和一个 math.cpp 文件每次你编译程序*。如果你的数学函数根本没有改变,肯定最好是一次编译它们,只要在重新编译 main.cpp 时将预编译的定义插入你的可执行文件即可。这正是 .lib 文件的目的。它们包含相关函数的定义的预编译代码。你仍然需要include文件来让你知道lib中存在什么函数。

This works great, but could be even better. As it is, we have a main.cpp file, and a math.cpp file, both of which are compiled every time you compile the program*. If your math functions don't change at all, surely it's better to compile them once and just insert the pre-compiled definitions into your executable whenever you recompile main.cpp? That is exactly the purpose of .lib files. They contain the pre-compiled code for definitions of the relevant functions. You still need the include file to let you know what functions exist in the lib.

编译的链接阶段的目的是采取这些预编译的函数和

The purpose of the linking stage of compilation is to take these pre-compiled functions and the functions you have just compiled, and roll them together into a single executable file.

基本上,你可以看一个静态的lib作为一些预编译的代码。预定义的函数,以及它的匹配包括文件作为一个工具,让任何代码使用这些函数知道哪些是可用的和它们的描述是什么。

Essentially, you can look at a static lib as the pre-compiled code for a number of predefined functions, and its matching include file as a tool to let any code wanting to use those functions know which are available and what their description is.

相当长一点比我打算。 :)希望它有助于澄清事情!

Wow, that ended up quite a bit longer than I intended. :) Hope it helps clarify things!

* 这不是严格的真实,但足以满足我们的目的。

* This is not strictly true, but is sufficient for our purposes here.

这篇关于包括目录与lib目录概念问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆