包含的C ++/C头文件的实现在哪里? [英] Where is the implementation of included C++/C header files?

查看:213
本文介绍了包含的C ++/C头文件的实现在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎有点愚蠢:)但是已经困扰了一段时间.当我在C ++/C程序中包含一些其他人编写的头文件时,编译器如何知道在头文件中声明的类成员函数的实现在哪里?

This may seem a little stupid:) But it's been bothering a while. When I include some header files which are written by others in my C++/C program, how does the compiler know where is the implementation of the class member function declared in the header files?

说我想写一些利用OpenCV库的程序.通常我会使用:

Say I want to write some program which takes advantage of the OpenCV library. Normally I would want to use:

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

但是,据我所知,这些只是头文件,仅声明函数而没有实现.然后,编译器如何知道在哪里可以找到实现?尤其是当我想构建一个.so文件时.

However, these are just header files which, as far as I can tell, only declares functions but without implementation. Then how does the compiler know where to find the implementation? Especially when I want to build a .so file.

有类似的帖子.基本上,它说第三方库,尤其是商业产品不会发布源代码,因此它们附带了带有头文件的lib文件.但是,它并没有弄清楚编译器如何知道在哪里可以找到lib文件.另外,该帖子中的答案提到如果我要编译自己的代码,则需要这些头文件的实现源代码.这是否意味着没有实现源就无法构建.so文件?

There is a similar post. Basically it said thrid-party library, esp. commercial product don't release source code, so they ship the lib file with the header. However, it didn't make clear how does the compiler know where to find the lib file. In addition, The answer in that post mentioned if I want to compile the code of my own, I would need the source code of the implementation of those header files. Does that mean I cannot build a .so file without the source of the implementation?

推荐答案

通常,实现预编译库的形式分发.您需要告诉编译器它们的位置.

In general, the implementation is distributed as form of pre-compiled libraries. You need to tell the compiler where they are located.

例如,对于gcc,引用在线手册

-llibrary
-l library

链接时搜索名为 library 的库. [...]

Search the library named library when linking. [...]

-Ldir

将目录 dir 添加到要搜索-l的目录列表中.

Add directory dir to the list of directories to be searched for -l.


注意:您不需要显式指定标准库,它们会自动链接.相反,如果您不希望将它们与您的二进制文件链接,则需要通过传递-nostdlib选项通知编译器.


Note: you don't need to explicitly specify the standard libraries, they are automatically linked. Rather, if you don't want them to be linked with you binary, you need to inform the compiler by passing the -nostdlib option.

这篇关于包含的C ++/C头文件的实现在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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