修改程序的启动函数
# 修改程序的启动函数
C/C++编程的时候,为什么一定要有main函数?
Q:main函数和普通函数有区别吗?
A:没有任何区别。我们写一个例子:
// test.cpp
int main()
{
return 0;
}
int func()
{
return 0;
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
接着我们使用Compiler Explorer (opens new window)查看一下汇编结果:
你会发现没有任何区别。我们可以通过修改编译选项,通过-e
指定一个程序的起点。
// test.cpp
int func()
{
return 0;
}
1
2
3
4
5
2
3
4
5
g++ -e func test.cpp -o a.out
1
上次更新: 2025/07/13, 18:55:16