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

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

问题描述

包含和链接真正起到什么作用?有什么区别?为何我需要同时指定两者? 当我编写#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文件,将看不到方法代码,因为该代码在库中. 原因之一是商业化的,因为您需要公开代码并在公司中拥有源代码.库已编译,因此您可以发布它. 头文件仅说明编译器,以及库中可以找到哪些类和方法.

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

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

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