C++ 是如何实现的?处理多个源文件?

发布于 2024-08-12 04:58:30 字数 322 浏览 2 评论 0原文

我现在正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

霊感 2024-08-19 04:58:30

有两种 include 变体:

#include  "path-spec" 
#include  <path-spec> 

引号表示法:

此形式指示预处理器在包含 #include 语句的文件的同一目录中查找包含文件,然后在包含 (#include) 该文件的任何文件的目录中查找包含文件。

括号符号在某些定义的位置查找头文件。

使用gcc,您可以通过以下方式获取有关这些路径的一些信息:

$ echo | gcc -v -x c++ -E -

编译器接受

-I或/I

选项来添加其他路径。

There are two include variants:

#include  "path-spec" 
#include  <path-spec> 

Quote notation:

This form instructs the preprocessor to look for include files in the same directory of the file that contains the #include statement, and then in the directories of any files that include (#include) that file.

The bracket notation looks for header files in certain defined locations.

With gcc you can get some information about these pathes via:

$ echo | gcc -v -x c++ -E -

Compilers accept

-I or /I

options to add additional pathes.

明媚如初 2024-08-19 04:58:30

如果您使用#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).

通知家属抬走 2024-08-19 04:58:30

它的工作原理类似。 #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.

深居我梦 2024-08-19 04:58:30

如果您的头文件位于同一目录中,您可以像这样包含它们:

#include "bar.h"

如果您想从另一个目录包含此头文件:

#include "../foobar/bar.h"

基本上引号意味着从当前目录和括号中搜索,如 #include中所示 表示在标准头文件目录中搜索。您可以通过在编译命令中添加 -I /path/to/your/custom/headers 将自定义目录添加到标准搜索路径。

If your header files are in the same dir, you can include them like:

#include "bar.h"

If you want to include this header from another dir:

#include "../foobar/bar.h"

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.

楠木可依 2024-08-19 04:58:30

带尖括号的 #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

两相知 2024-08-19 04:58:30

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文