我正在尝试编译并运行以下程序而没有main()函数C.我使用以下命令编译了我的程序.
gcc -nostartfiles nomain.c
Run Code Online (Sandbox Code Playgroud)
编译器发出警告
/usr/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000400340
Run Code Online (Sandbox Code Playgroud)
好的,没问题.然后,我已经运行了可执行文件(a.out),两个printf语句都成功打印,然后得到分段错误.
那么,我的问题是,为什么在成功执行print语句后出现分段错误?
我的代码:
#include <stdio.h>
void nomain()
{
printf("Hello World...\n");
printf("Successfully run without main...\n");
}
Run Code Online (Sandbox Code Playgroud)
输出:
Hello World...
Successfully run without main...
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)
注意:
这里,-nostartfilesgcc标志阻止编译器在链接时使用标准启动文件
鉴于主程序的标准定义:
int main(int argc, char *argv[]) {
...
}
Run Code Online (Sandbox Code Playgroud)
argcPOSIX系统在哪些情况下可以为零?
在Python中我有一个模块 myModule.py,我在其中定义了一些函数和一个main(),它接受一些命令行参数.
我通常从bash脚本中调用main().现在,我想将所有内容放入一个小包中,所以我想也许我可以将我的简单bash脚本转换为Python脚本并将其放入包中.
那么,我如何从 MyFormerBashScript.py 的main()函数实际调用 myModule.py 的main()函数?我甚至可以这样做吗?我如何传递任何参数?
这是我的代码:
public class Main {
public static void main(String[] args) {
Main p = new Main();
p.start(args);
}
@Autowired
private MyBean myBean;
private void start(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("META-INF/config.xml");
System.out.println("my beans method: " + myBean.getStr());
}
}
@Service
public class MyBean {
public String getStr() {
return "string";
}
}
Run Code Online (Sandbox Code Playgroud)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="mypackage"/>
</beans>
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?我得到NullPointerException.是否可以在独立应用程序中使用自动装配?
java spring program-entry-point dependency-injection autowired
我正在阅读C++常见问题,我注意到了一句话.
main()不能内联.
为什么是这样?
我对此有点新意,请耐心等待.我目前正在学习C#和Java,它们的一个相似之处在于主要功能需要封装在一个类中.例如
public class HelloWorld {
public static void main(String[] args) {
// Some Code
}
}
Run Code Online (Sandbox Code Playgroud)
现在我明白,当你运行程序时,main通常是"入口点".所以基本上,你的程序将在主函数的任何地方开始执行.但我相信在两种语言中,您可以在多个类中拥有多个主要功能.因此,当我编译一个具有多个主要功能的项目时,"入口点"在哪里?编译器如何知道从哪里开始?
main()存在2个有效版本C++:
int main() // version 1
int main(int argc, char **argv) // version 2
Run Code Online (Sandbox Code Playgroud)
但两种重载都不能同时共存.为什么不?(潜在用例:从终端运行程序时,如果没有传递参数,则调用第一个版本,否则第二个版本被调用.)
编译器是否执行特殊检查以允许每个二进制文件只有一个版本?
今天,在使用一个自定义库时,我发现了一种奇怪的行为.静态库代码包含调试main()功能.它不在#define旗帜内.所以它也存在于库中.它被用于链接到包含真实的另一个程序main().
当它们都链接在一起时,链接器不会抛出多个声明错误main().我想知道这是怎么发生的.
为简单起见,我创建了一个模拟相同行为的示例程序:
$ cat prog.c
#include <stdio.h>
int main()
{
printf("Main in prog.c\n");
}
$ cat static.c
#include <stdio.h>
int main()
{
printf("Main in static.c\n");
}
$ gcc -c static.c
$ ar rcs libstatic.a static.o
$ gcc prog.c -L. -lstatic -o 2main
$ gcc -L. -lstatic -o 1main
$ ./2main
Main in prog.c
$ ./1main
Main in static.c
Run Code Online (Sandbox Code Playgroud)
"2main"二进制文件如何找到main要执行的内容?
但是将它们编译在一起会产生多重声明错误:
$ gcc prog.c static.o
static.o: In function `main':
static.c:(.text+0x0): …Run Code Online (Sandbox Code Playgroud) 在阅读有关C ++中的函数的知识时,我被告知函数需要调用声明。例如:
#include <iostream>
int main() {
std::cout << "The result is " << sum(1, 2);
return 0;
}
int sum(int x, int y) {
return x + y;
}
Run Code Online (Sandbox Code Playgroud)
由于该函数没有声明,因此返回错误sum。
main.cpp:4:36: error: use of undeclared identifier 'sum'
std::cout << "The result is " << sum(1, 2);
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
为了解决这个问题,我将添加声明:
#include <iostream>
int sum(int x, int y); // declaration
int main() {
std::cout << "The result is " << sum(1, 2);
return 0;
}
int sum(int …Run Code Online (Sandbox Code Playgroud) c++ program-entry-point forward-declaration function-declaration
在C/C++中,main函数接收类型的参数char*.
int main(int argc, char* argv[]){
return 0;
}
Run Code Online (Sandbox Code Playgroud)
argv是一个数组char*,并指向字符串.这些字符串在哪里?它们是堆,堆栈还是其他地方?