如何识别“ c++”中每个编译单元的位置-e&quot输出

发布于 2025-02-12 18:12:09 字数 1971 浏览 2 评论 0原文

为了更多地了解汇编,我试图逐步进入汇编过程?
我创建了文件foo.hppfoo.cppbar.cpp
foo.hpp:包含一个简单的类接口。
foo.cpp:包含其实现(包括foo.hpp
bar.cpp:包含输入点main(),它只需构建foo class(include foo.hpp

我用c ++ -wall -wextra -werror -e foo.cpp bar.cpp
并期望为foo.cppbar.cpp提供一个单独的汇编单元,但我只有一个输出到stdout,好吧,我在某个地方阅读了编译器实际上为每个.cpp文件接收单独的编译单元,因此我认为输出只是一个简化吗?
问题1.0:这是正确的假设吗?
这是预处理器的输出:

# 1 "Bar.cpp"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 414 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "Bar.cpp" 2


# 1 "./Foo.hpp" 1


class Foo {
 private:
  int _value;
 public:
  Foo( int value);
  int getValue( void ) const;
};
# 4 "Bar.cpp" 2

int main( void ) {
 Foo foo(2);
 foo.getValue();

}
# 1 "Foo.cpp"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 414 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "Foo.cpp" 2
# 1 "./Foo.hpp" 1


class Foo {
 private:
  int _value;
 public:
  Foo( int value);
  int getValue( void ) const;
};
# 2 "Foo.cpp" 2


Foo::Foo( int value) : _value(value) {};
int Foo::getValue( void ) const { return this->_value ; };

问题1.1:假设这只是一个简化的版本,并且编译器实际上接收了单独的编译单元,我该如何区分特定的CU?是将它们分开的###“ number”“ fileName”行?

问题1.2:如果是,请注意#4“ ./bar.cpp” 2 e节(bar.cpp包括foo.hpp )标题的内容实际上在bar.cpp部分中复制,而未复制在foo.cpp e节(foo.cpp)中。包括foo.hpp)为什么?
问题1.3:上次偏置问题:这些生物是什么&lt; nocen-in-gt; &lt;命令行&gt;

<强>编辑:

正如评论中提到的@holyblackcat,我实际上需要单独编译每个人以获取每个编译单元,因此,如果您遇到相同的混乱,只需编译器您的文件单独。

In order to understand more about compilation i tried to go into the compilation process step by step ?
I created the files Foo.hpp , Foo.cpp and Bar.cpp
Foo.hpp : contains a simple class interface.
Foo.cpp : contains it's implementation (includes Foo.hpp)
Bar.cpp : contains entry point main() which simply construct Foo class (includes Foo.hpp)

I compiled with C++ -Wall -Wextra -Werror -E Foo.cpp Bar.cpp
and was expecting a separate compilation unit for Foo.cpp and Bar.cpp but i got only one single output to stdout, well i read somewhere that the compiler actually receives separate compilation unit for each .cpp file so i assumed the output is just a simplification ?
QUESTION 1.0 : is this a correct assumption ??
This is the output of preprocessor :

# 1 "Bar.cpp"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 414 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "Bar.cpp" 2


# 1 "./Foo.hpp" 1


class Foo {
 private:
  int _value;
 public:
  Foo( int value);
  int getValue( void ) const;
};
# 4 "Bar.cpp" 2

int main( void ) {
 Foo foo(2);
 foo.getValue();

}
# 1 "Foo.cpp"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 414 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "Foo.cpp" 2
# 1 "./Foo.hpp" 1


class Foo {
 private:
  int _value;
 public:
  Foo( int value);
  int getValue( void ) const;
};
# 2 "Foo.cpp" 2


Foo::Foo( int value) : _value(value) {};
int Foo::getValue( void ) const { return this->_value ; };

QUESTION 1.1: Assuming that this is just a simplified version and the compiler actually receives separate compilation units, How can i differentiate where a specific CU is? is it the # "number" "filename" lines that separates them ??

QUESTION 1.2: if yes then note that in # 4 "./Bar.cpp" 2 section (Bar.cpp includes Foo.hpp) the content of the header are actually copied in Bar.cpp section while not copied in Foo.cpp section (Foo.cpp also include Foo.hpp) why is that ?
QUESTION 1.3: last off-context question : what are these creatures <built-in> <command line> .

EDIT:

as @HolyBlackCat mentioned in the comments, I need actually to compile each separately to get a separate compilation unit for each , so if you encountered the same confusion, Just compiler your files separately.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

雪化雨蝶 2025-02-19 18:12:09

如果要单独的compialtion单元,则不要在命令行上指定所有源文件。一一汇编它们:

C++ -Wall -Wextra -Werror -E -o Foo.e Foo.cpp
C++ -Wall -Wextra -Werror -E -o Bar.e Bar.cpp

If you want separate compialtion units then don't specify all source files on the command line. Compile them one by one:

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