简体   繁体   English

具有Main()的C ++库分发

[英]C++ Library distribution with a Main()

Question

Is it ok for a C++ Library to have the auto-generated main() function, since the main() is not in the Header file that is distributed with it? C ++库具有自动生成的main()函数可以吗,因为main()不在与它一起分发的Header文件中?

If it is not ok, then would it be better to wrap an #ifdef _DEBUG around main()? 如果还不行,那么最好在main()周围包裹#ifdef _DEBUG?

Scenario 脚本

I have a C++ Visual Studio project that wraps all our message queue functionality to abstract whatever AMQP we are currently using, and it is compiled as a Library in the Release configuration. 我有一个C ++ Visual Studio项目,该项目包装了我们所有的消息队列功能,以抽象化当前使用的AMQP,并且在Release配置中将其编译为Library。

In the Debug Configuration, it is compiled as an executable so that some tests can be ran on it to confirm that it is working. 在“调试配置”中,将其编译为可执行文件,以便可以对其进行一些测试以确认其正常运行。

I have been looking around all morning and can't find any references to what is considered best-practice in this regard. 我整天都在四处张望,找不到关于这方面最佳做法的任何参考。

The idea is that the only part of the library that is used is the put() and get() from the MessageQueue class that is in the library. 这个想法是,库中唯一使用的部分是库中MessageQueue类中的put()和get()。

I would just refactor the test piece and your problem goes away: Always build your code as a library with no main function at all and have a totally separate project that links to the library and does the tests. 我只是重构测试件,而您的问题就消失了:始终将代码构建为一个没有任何main功能的库,并拥有一个完全独立的项目 ,该项目链接到该库并进行测试。 That way it's built in a way completely similar to actual release use rather than as a self-contained binary executable that's not the same set of circumstances as a normal release usage. 这样,它就以与实际发行版使用完全相似的方式构建,而不是作为独立的二进制可执行文件构建而成,该二进制可执行文件与普通发行版使用情况不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM