是否可以从其他cpp文件使用#define? [英] Is it possible to use #define from other cpp file?

查看:208
本文介绍了是否可以从其他cpp文件使用#define?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为预处理器一个接一个地处理文件,我不知道该如何处理包含,所以我认为这是不可能的,但是听到别人的想法将是很棒的。

I think the preprocessor handles files one by one and I can't figure out how to do it with includes, so I think it's impossible, but it would be great to hear other's thoughts.

我在 a.cpp 中:

#define A 1

,我想从 2.cpp使用它

编辑:
我无法修改第一个文件。所以现在我只复制了定义。但是问题仍然悬而未决。

I cant modify first file. So for now i just have copied defines. But question still opened.

推荐答案

其他翻译部门看不到源文件中的定义。实施文件是单独编译的。

Defines inside a source file aren't seen by other translation units. Implementation files are compiled separately.

您可以


  • 将它们放入标头并包含它

  • 使用编译器的选项

  • 以合理的方式进行操作- extern const int A = 1; 在实现文件中,并在要使用它时声明它 extern const int A;

  • put them in a header and include it
  • use your compiler's options
  • do it the sane way - extern const int A = 1; in an implementation file and declare it when you want to use it extern const int A;.

其中,我会说第一个选择可能是最差的

这篇关于是否可以从其他cpp文件使用#define?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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