链接到库时包含和链接有什么区别? [英] What is the difference between include and link when linking to a library?

查看:24
本文介绍了链接到库时包含和链接有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

包含和链接到底有什么作用?有什么区别?为什么我需要同时指定它们?当我写#include math.h,然后写-lm编译,#include math.h-lm 分别做什么?

What does include and link REALLY do? What are the differences? And why do I need to specify both of them? When I write #include math.h and then write -lm to compile it, what does #include math.h and -lm do respectively?

据我了解,在链接库时,您需要它的 .h 文件和它的 .o 文件.这是否表明 #include math.h 意味着接受 .h 文件,而 -lm 接受 .o 文件?

In my understanding, when linking a library, you need its .h file and its .o file. Does this suggest #include math.h means take in the .h file while -lm take in the .o file?

推荐答案

这是因为头文件只包含声明,而 .o 文件(或 .obj、.dll 或 .lib)包含方法的定义.如果你打开一个 .h 文件,你将看不到方法的代码,因为它在库中.一个原因是商业性的,因为您需要发布您的代码并在您的公司拥有源代码.库已编译,因此您可以发布它.头文件只告诉编译器,它可以在库中找到哪些类和方法.

It's because headers files contain only declaration and .o files (or .obj, .dll or .lib) contain definitions of methods. If you open an .h file, you will not see the code of methods, because that is in the libraries. One reason is commercial, because you need to publish your code and have the source code in your company. Libraries are compiled, so you could publish it. Header files only tell compiler, what classes and methods it can find in the library.

这篇关于链接到库时包含和链接有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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