如何使用main.c以外的文件在IDE中运行C程序? [英] How to run a C program in an IDE using file other than main.c ?

查看:140
本文介绍了如何使用main.c以外的文件在IDE中运行C程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用C编码。

我正在使用像CodeBlocks和Netbeans这样的IDE。当我使用像abc.c这样的自创文件运行程序时,会出错。但是当我在IDE创建的文件main.c中运行该程序时,一切正常。

如何在main.c以外的文件中运行我的程序?



我尝试了什么:



我试图在不同的文件中运行代码。

解决方案

看看这个使用不同的代码文件 C ++教程



主要任务是将它包含在项目配置中并包含标题并使用声明的代码。


你没有运行源文件(即 abc.c )。相反,您运行可执行文件,这是源文件编译的成功产品。

IDE 为您编写编译步骤。因此,如果您不使用 IDE ,则必须手动执行。



例如,在 Linux上使用 gcc 方框:



编译步骤

 gcc -o abc abc.c 



运行可执行文件

 ./ abc 


当你编写一个应用程序时,系统必须知道从哪里开始运行它:控制台类型的应用程序,我在寻找一个名为 main - 这称为 入口点 。如果它不存在,则应用程序将不可执行,因为系统不知道哪一行代码是第一个。 (对于Windows应用程序,它被命名为WinMain或wWinMain。)



您可以愉快地调用您想要的任何文件,只要其中一个包含有效文件入口点一旦编译没有错误就可以执行。这并不意味着它可以工作,或者做你想做的任何事情 - 这是一组称为运行时错误的不同错误 - 要对它进行排序,你需要使用调试器。


I am doing coding in C.
I am using IDEs like CodeBlocks and Netbeans. When I run the program using a self created file like abc.c it gives errors. However when I run the program in the IDE created file main.c everything works fine.
How can I run my program in a file other than main.c ?

What I have tried:

I have tried to run the code in different files.

解决方案

Take a look at this C++ tutorial for using different code files.

The main task is to include it in the project configuration and the include the header and use the declared code.


You don't run a source file (i.e. abc.c). Instead, you run an executable, that is the successful 'product' of a source file compilation.
The IDE makes the compilation step for you. Hence, if you don't use the IDE then you have to perform it manually.

For instance, using gcc on a Linux box:

Compilation step

gcc -o abc abc.c


Run the executable

./abc


When you write an application, the system has to know where to begin running it: of console type apps it does that my looking for a function called main - this is called the entry point. If it isn't there, the app will not be executable because the system has no idea which line of code is meant to be first. (For Windows apps, it is named either WinMain or wWinMain.)

You can happily call your files anything you want, just so long as one of them contains a valid entry point it will be executable once it has compiled without errors. That doesn't mean it will work, or do anything you wanted it to - that is a different set of errors called run-time errors - to sort that out you will need to use a debugger.


这篇关于如何使用main.c以外的文件在IDE中运行C程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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