那天ViVi问了两个问题:
1)在C++中,怎么定义 多个main
2)程序怎么不从main进入
解决(在DevCPP4.9.9.2中编译通过):
1)
#include
<
iostream
>
using namespace std;
namespace NS
... {
int main()
...{
}
}
int main()
... {
}
using namespace std;
namespace NS
... {
int main()
...{
}
}
int main()
... {
}
2)
#include
<
iostream
>
using namespace std;
void foo() __attribute__ ((constructor));
int main()
... {
}
void foo()
... {
printf("Hello from %s ", __FUNCTION__);
system("pause");
}
using namespace std;
void foo() __attribute__ ((constructor));
int main()
... {
}
void foo()
... {
printf("Hello from %s ", __FUNCTION__);
system("pause");
}
注:查了一些GCC的手册,找到以下内容:
The __attribute__ keyword can be used to assign an attribute to a function or data
declaration. The primary purpose of assigning an attribute to a function is to make it
possible for the compiler to perform optimization.
constructor : A function with this attribute is called automatically before the call
is made to main(). Also see the destructor attribute.