c语言中的多个主要功能 [英] multiple main functions in c language

查看:107
本文介绍了c语言中的多个主要功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在任何条件下都可以在c语言中使用多个主函数。

如果是那么我们怎么做呢....

解决方案

主要功能是程序执行的地方开始。因此,任何可执行文件中只能存在一个Main函数。

但是在源代码中,您可能有多个Main函数。并且使用预处理程序指令,在构建可执行文件时,除了正好一个Main函数之外的所有指令都将被删除。


除了其他注释之外,还可以在链接时< a href =http://msdn.microsoft.com/en-us/library/f9t8842e(v=vs.90).aspx>指定程序的入口点 [ ^ ],但你需要了解潜力对于问题。


我使用多个主要功能的唯一语言是Java。在那里你可以将一个 public static void main(String [] args)放入一个类中,你可以从IDE启动任何类的main()函数(例如Eclipse) )。这可以用于将小测试用例放入类中。尽管如此,当你最终打包产品时,你必须在清单文件中将其中一个类标记为主类(如com.mycompany.MainClass),当有人启动你的程序时(例如从jar文件中),那么只有一个主类是有效的。



在C中,你甚至没有名称空间(比如java中的包和类),所以你根本无法实现这样的功能。如果你想在几个地方测试函数(比如在java中)那么你可以这样做:

Something.c:

 ... blah blah正常发布代码Something.c ... 


#if BUILD_DEBUG
void MyTestCode_2()
{
... blah blah blah ...
}
< span class =code-preprocessor>#endif



main.c:

< pre lang =c ++> int main( int argc, char * argv [])
{
#如果BUILD_DEBUG
// 这个愚蠢如果有助于避免在大多数编译器中出现无法访问的代码警告
// 并帮助您打开/关闭调试测试代码
if 1
{
// 您可以在此声明并调用任何测试函数
void MyTestCode_2();
MyTestCode_2();
return 0 ;
}
#endif

...原始主码...
}


is it possible to have more then one main functions in c language in any condition.
if it is then how we can do that....

解决方案

the Main function is the point of the program where its execution starts. Consequently, only one Main function can exist in any executable.
But in your source code, you may have more than one Main function. And with preprocessor directives, all of them with the exception of exactly one Main function are removed when the executable is built.


In addition to the other comments, it is possible at link time to specify the program's entry point[^], but you need to understand the potential for problems.


The only language in which I've used "multiple main" functions is Java. There you can put a public static void main(String[] args) into an class and you can start the main() function of any class from the IDE (for example Eclipse). This can be useful to put in small test cases into the classes. Still, when you finally package your product you have to mark one of these classes as the main class (like com.mycompany.MainClass) in your manifest file and when someone starts your program (for example from a jar file) then only one main is valid.

In C you don't even have namespaces (like packages and classes in java) so you simply can't implement features like this. If you want test functions in several places (like in java) then you can do that this way:
Something.c:

... blah blah normal release code of Something.c ...


#if BUILD_DEBUG
void MyTestCode_2()
{
    ... blah blah blah ...
}
#endif


main.c:

int main(int argc, char* argv[])
{
#if BUILD_DEBUG
    // this stupid if helps avoiding "unreachable code" warnings in most compilers
    // and helps you to turn on/off debug test code
    if (1)
    {
        // you can declare and call any of your test functions here
        void MyTestCode_2();
        MyTestCode_2();
        return 0;
    }
#endif

    ... original main code ...
}


这篇关于c语言中的多个主要功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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