是否有一种自动方法可以删除发布版本的调试方法?

发布于 2024-10-09 04:06:28 字数 403 浏览 0 评论 0原文

注意:这是我之前提出的问题的扩展:额外的函数/方法定义会增加程序的内存占用吗?

当我编写一个类时,我通常最终会编写几个测试/调试方法,用于确保该类按其应有的方式工作,或用于打印数据来帮助调试或单元测试等。是否有一种简单/自动的方法可以在没有这些方法的情况下进行发布,或者我是否需要在任何时候想要编译发布版本时手动删除额外的代码?

我从 C++ 和 Java 的角度提出这个问题。我正在使用 Code::Blocks 和 Eclipse 作为 IDE,如果这能以某种方式解决问题的话。

Note: This is an extension of an earlier question I asked here: Do additional function/method definitions increase a program's memory footprint?

When I write a class, I usually end up writing several testing/debugging methods, used to make sure the class works as it should, or for printing data to help with debugging, or for unit testing, etc. Is there an easy/automatic way to make a release without these methods, or do I need to manually delete the extra code any time I want to compile a release version?

I ask this question both from a C++ and a Java perspective. I'm using Code::Blocks and Eclipse as IDEs, if that plays into the answer somehow.

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

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

发布评论

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

评论(5

飘过的浮云 2024-10-16 04:06:28

对于 C++,您可以使用预处理器宏:

#ifnef NDEBUG
void dbgFunction() { ... }
#endif

NDEBUGassert 使用的标准宏:您的 assertions won'如果设置了NDEBUG,则不会对其进行评估。


另一个想法可能是从您不使用的符号(或您明确想要删除的符号)中剥离二进制文件(因此不触及源代码)。

For C++ you can use preprocessor macros:

#ifnef NDEBUG
void dbgFunction() { ... }
#endif

NDEBUG is a standard macro used by assert: your assertions won't be evaluated if NDEBUG is set.


Another idea could be that of stripping the binary (thus without touching the source code) from symbols you're not using (or from the symbols you explicitly want to remove).

抱猫软卧 2024-10-16 04:06:28

从 Java 的角度来看,有多种策略:

  • 如果是生成日志消息的代码片段,请使用 log4j 或其他日志记录框架之一。

  • 如果它是生产代码中未使用的完整方法,请将其保留在那里。在运行时,JIT 编译器会注意到它没有被使用,并且不会费心去编译它。代码在运行时可能仍然存在(作为未编译的字节码),但对内存占用的影响将是微不足道的......考虑到 JVM 需要几兆字节的堆空间才能启动。

  • 如果它是一个类或类的集合(例如 JUnit 测试类),请将其放入并行包中,包名称中的某处带有“test”,并在创建 JAR 文件时将其过滤掉。

  • 如果您使用 Maven,请将测试类放在“src/test/java”目录而不是“src/main/java”目录中,maven 命令会自动排除它们。

一些开发人员建议在 Java 中使用源代码预处理器,但大多数人会说这是一个坏主意;即它引入的问题多于它解决的问题。首先,主流 Java IDE 和调试器不理解处理器指令,因此您将遇到各种与工具相关的问题。此外,这不是“Java方式”。

From a Java perspective, there are a variety of strategies:

  • If it is snippets of code that produce log messages, use log4j or one of the other logging frameworks.

  • If it is a complete method that is unused in the production code, leave it there. At runtime, the JIT compiler will notice that it isn't used and not bother to compile it. The code may still be there at runtime (as uncompiled bytecodes), but the impact on the memory footprint will be insignificant ... given that the JVM needs a few megabytes of heap space just to get started.

  • If it is a class or collection of classes (e.g. JUnit test classes), put it/them in a parallel package with "test" somewhere in the package name, and filter it/them out when you create the JAR file.

  • If you are using Maven, put test classes in the "src/test/java" directory rather than "src/main/java", and the maven command will automatically exclude them.

Some developers recommend using a sourcecode preprocessor with Java, but most would say that this is a bad idea; i.e. it introduces more problems than it solves. For a start, the mainstream Java IDEs and debuggers don't understand proprocessor directives, so you are going to run into various tool-related issues. Besides, it is not the "Java way".

琉璃繁缕 2024-10-16 04:06:28

要添加到 peoro 在 C++ 中的答案,您的发布项目应始终定义 NDEBUG,并且您的调试项目应始终定义 _DEBUG,以便您可以测试其中任何一个或创建自己的预处理器指令来排除代码。

我个人发现时不时地拥有一些我自己的预处理器指令非常有益,如果您使用预编译的头文件,那么放置它们的好地方是 stdafx.h 。您可以启用或禁用整个功能或部分代码,这些功能可能正在进行中,或者对于某些构建来说是不需要的。例如,在一个应用程序中,通过修改单个定义,我可以从 OpenGl 切换到 DirectX,或者从项目中完全排除 3D 渲染。

此外,您可以为项目创建新的配置,而不仅仅是调试和发布,这些项目具有某些定义集以及包含或排除等。您可能需要两个调试版本,例如一个包含调试跟踪,另一个具有打印的详细日志记录执行期间几乎整个堆栈。

To add to peoro's answer in C++ your release projects should always have NDEBUG defined and your debug projects should always have _DEBUG defined so you can test for either of those or create your own pre-processor directives to exclude code.

I personally find it very beneficial to have a few of my own preprocessor directives from time to time and great place to put them is in stdafx.h if you are using pre-compiled header files. You can enable of disable whole features or portions of code which may be works in progress or simple undesirable for certain builds. For example in one application by modifying a single define I can switch from OpenGl to DirectX or completely exclude 3d rendering from the project.

In addition you can create new configurations for projects beyond just debug and release which have certain sets of defines and for inclusions or exclusions etc. You may want two debug versions for example one that includes the debug traces and one that has verbose logging that prints out nearly the entire stack during execution.

树深时见影 2024-10-16 04:06:28

使用预处理器开关

#ifdef MY_DEBUG_SWITCH
// debug code goes here
#endif

Use preprocessor switches

#ifdef MY_DEBUG_SWITCH
// debug code goes here
#endif
安稳善良 2024-10-16 04:06:28

在 Java 世界中,您可以使用 log4j 来实现所有日志记录目的,将调试日志记录在您的代码为log.debug()。在生产代码中,您可以在高于 DEBUG 的任何地方使用 log4j 属性级别运行代码,例如 INFO。您可以在此处了解更多详细信息。

In the Java world you can use log4j for all your logging purposes, having your debug logging in your code as log.debug(). In your Production code you could run the code with the log4j property level anywhere higher than DEBUG for instance INFO. You can have more details here.

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