C++ 是如何实现的?处理多个源文件?
我现在正在学习 C++,有 Python 背景,但在理解 C++ 如何处理多个源文件方面遇到了一些困难。在 Python 中,导入语句首先检查您尝试导入的模块的当前工作目录,然后检查 sys.path 中的目录。在 C++ 中,我应该在哪里放置自定义的 .h 文件?编译器会在哪里查找?
例如,我有一个程序 foo.exe,它是从单个源文件 foo.cpp 编译而来的,两者都位于同一目录中。我决定要更好地组织事情,所以我创建了一个新的 .h 文件 bar.h 并将内容转储到其中。我是否只需要 #include 即可获取我放在那里的东西?如果我想将 bar.h 与另一个程序(在完全不同的目录中)一起使用怎么办?
I'm studying C++ right now, coming from a background in Python, and I'm having some trouble understanding how C++ handles multiple source files. In Python, the import statement first checks the current working directory for the module you're trying to import and then it checks the directories in sys.path. In C++, where would I place a custom made .h file? Where would the compiler even look?
For example, I've got a program, foo.exe compiled from a single source file, foo.cpp, both in the same directory. I decide that I want to organize things a little better, so I create a new .h file, bar.h and dump stuff in there. Would I just need to #include to get to the stuff I put there? What if I want to use bar.h with another program (in a completely different directory)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
有两种
include
变体:引号表示法:
括号符号在某些定义的位置查找头文件。
使用
gcc
,您可以通过以下方式获取有关这些路径的一些信息:编译器接受
-I或/I
选项来添加其他路径。
There are two
include
variants:Quote notation:
The bracket notation looks for header files in certain defined locations.
With
gcc
you can get some information about these pathes via:Compilers accept
-I or /I
options to add additional pathes.
如果您使用
#include
,它(通常)会在包含路径中查找,否则如果您使用#include "../../foo/bar. h”
。在大多数编译器上,您可以使用
-I
或/I
设置包含路径。详细信息请参阅其手册。不过,不要在标头中定义任何对象 - 如果这样做(并将标头包含在多个源文件中),您将在链接时遇到多个定义错误。
It (generally) looks in the include path if you use
#include <foo>
, else it uses relative paths if you use#include "../../foo/bar.h"
.You set the include path with
-I
or/I
on most compilers. Consult its manual for details.Don't define any objects in headers though -- you will have multiple definition errors at link time if you do (and include the header in multiple source files).
它的工作原理类似。 #include 仅由编译器使用。在执行时,文件 bar.h 不会被使用。但在编译时却是这样。
在编译时,该文件可能位于两个位置:
1.- 当前目录(如 python 中)
2.- 在包含路径中配置的目录。在哪里配置该目录取决于您使用的编译器。它们中的大多数允许您在编译命令行中定义包含目录。大多数 IDE 允许您在某些选项菜单中对其进行配置。
希望有帮助。
It works in a similar way. The #include only is used by the compiler. In execution time the file bar.h doesn't gets used. But in compile time it is.
In compile time, the file could be in two places:
1.- The current directory (as in python)
2.- The directories configured in your include path. Where to configure that directories depends of the compiler you are using. Most of them let you define the include directories in the compile command line. And most IDEs let you configure it in some Options menu.
Hope it helps.
如果您的头文件位于同一目录中,您可以像这样包含它们:
如果您想从另一个目录包含此头文件:
基本上引号意味着从当前目录和括号中搜索,如
#include中所示
表示在标准头文件目录中搜索。您可以通过在编译命令中添加-I /path/to/your/custom/headers
将自定义目录添加到标准搜索路径。If your header files are in the same dir, you can include them like:
If you want to include this header from another dir:
Basically quotations mean to search from current directory and brackets like in
#include <abc.h>
mean to search in standard header file directories. You can add custom directories to the standard search path by adding a-I /path/to/your/custom/headers
in the compile command.带尖括号的 #include 在系统包含目录中查找。像这样:
#include
使用双引号,它会在当前目录以及提供给编译器进行搜索的其他目录中查找。
#include“foo.h”
g++ -I../include foo.cpp
#include with angle brackets looks in the system include directories. Like this:
#include <iostream>
With double quotes it looks in the current directory and other directories given to the compiler to search.
#include "foo.h"
g++ -I../include foo.cpp
python 模型中缺少的是链接器。
在Python中,你导入代码并立即对其进行解释。在 c/c++ 中,您将每个源文件编译为目标文件。然后,您告诉链接器将一堆目标文件收集到可执行文件中。
通常,c/c++ 源文件中的包含内容仅包含其他 C 文件中内容的描述(函数名称等),而不包含函数内容。这足以让编译器编译给定的文件。然后链接器会将您的目标文件与“众所周知”的函数库组合起来并生成可执行文件
the thing you are missing in the python model is the linker.
in python you import code and its interpreted right there and then. in c/c++ you compile each source file into an object file. You then tell the linker to collect a bunch of object files into an executable
Typically the includes in c/c++ source files only contain descriptions of whast in the other C files (the names of functions, etc) not the function contents. This is enough for the compiler to compile a given file. Then the linker will combine your object files with libraries of 'well known' functions and make an executable