Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
一步从命令行运行c程序?_C - Fatal编程技术网

一步从命令行运行c程序?

一步从命令行运行c程序?,c,C,我刚开始写一些C程序 首先,我只是通过VS代码运行它们。又好又简单,我只需按下一个按钮,砰的一声,它就出现了 但是现在我需要将文件作为参数传递给我的程序,这就需要从命令行运行它 我现在的做法是使用这两个步骤(我认为这只是基本的做法): 然后运行该文件: ask@Garsy:~/Notes/ethHack/crpytifiles$ ./test 从长远来看,这有点乏味。我有没有办法一步完成这个过程 也许也没有创建可执行文件 如果我可以像您通常使用python或java文件一样运行它,一个命令,

我刚开始写一些C程序

首先,我只是通过VS代码运行它们。又好又简单,我只需按下一个按钮,砰的一声,它就出现了

但是现在我需要将文件作为参数传递给我的程序,这就需要从命令行运行它

我现在的做法是使用这两个步骤(我认为这只是基本的做法):

然后运行该文件:

ask@Garsy:~/Notes/ethHack/crpytifiles$ ./test 
从长远来看,这有点乏味。我有没有办法一步完成这个过程

也许也没有创建可执行文件


如果我可以像您通常使用python或java文件一样运行它,一个命令,然后运行它,那将非常酷。

您可以使用makefile来运行它。有关GNU Make的更多信息

该文件应称为MakefileMakefile(它可以有不同的名称,只是保持简单),您可以通过执行以下命令来运行它:

make

假设您安装了
gnumake
,并且
test.c
makefile
位于同一目录中

从长远来看,这有点乏味。我有没有办法一步完成这个过程

对。您可以创建一个shell函数(如果shell支持别名参数,则可以创建别名,而bash不支持别名参数),例如:

它将编译脚本,如果编译成功,则运行它,然后删除已编译的二进制文件

也许也没有创建可执行的

不(嗯,不容易)。正在执行的二进制文件被卸载到
exec*()
函数系列中,执行的操作很复杂,我怀疑与stdin操作不兼容。因此,您不能将可执行文件发送到管道并从管道执行它


你所能做的就是使用一个,尽管它不是完全相同的东西。

正如@alex01011正确指出的那样,你需要的是一个
生成文件,他的解决方案应该可以工作。这里我想建议的是一个更好的
Makefile

首先
make
已经知道如何在简单情况下使用
test.c
中的build
test
。它将从
Makefile
变量向预处理器、编译和链接器步骤添加参数,因此最好使用内置命令以获得更好的灵活性

# Tell make that `all` and `run` is technically not files that will be built
.PHONY : all run

# These flags are passed to the compiler, we always want to compile with 
# warnings when developing
CFLAGS= -Wall

# `all` is the first rule, so that is the one that will be build not    
# specifying anything on the command line
# `all` also depends on `test` so that will be built from `test.c` calling 
# `make` or `make all` 
all: test

# `make run` will run your command. `run` depends on `all` to make sure the 
# program exist before calling `./test`
# Note the the indent must be made with a tab and not spaces
run: all
       ./test
如果您的程序由更多的文件组成,事情会变得更加复杂,但仍然易于管理:

# Example of a Makefile for a project that is composed of the files 
# test.c foo.c, bar.c, foo.h and bar.h
# The main-function is in test.c, and the generated program will be
# called `test`
#  
.PHONY: all run
CFLAGS= -Wall


all: test

# foo.c includes foo.h therefore foo.o depends on foo.h in addition to foo.c
foo.o: foo.h 

# bar.c includes bar.h therefore foo.o depends on bar.h in addition to bar.c
bar.o: bar.h 

# test.c includes both foo.h and bar.h 
test.o: foo.h bar.h 

# test should be linked with foo.o and bar.o in addition to test.o
test: foo.o bar.o

run: all
       ./test
现在,如果需要,键入
makerun
将自动构建并链接
test
,如果没有错误,则运行
/test

除了
CFLAGS
之外,您可以设置的其他变量有
CC
cppfagas
LDFLAGS
LOADLIBES
LDLIBS


通常,您还希望在
Makefile
中有一个
clean
目标,用于键入
makeclean
以删除生成的文件。有关更多详细信息,请参见
info make

我想知道没有人对IDE和shell进行一般性比较。因此,是的,IDE可能会给你一些安慰。但是,如果你从零开始学习了linking&Co的基本原理,你会很高兴的,否则的话,你会发现 IDE可能会变得非常具有挑战性,当您开始一些无法开箱即用的东西时

其余的都是一些有用的技巧,可以提高壳状make或or的效率 其他自动化建设者。Shell编辑器提供了额外的工具和插件来增加您的工作流程-例如,使用vim作为Shell编辑器(以及一些插件),您可以非常接近IDE。这包括语法突出显示,
代码检查、程序编译和运行等。。。就我的2美分

我宁愿把编译、运行和链接作为单独的目标。将随着项目的发展而更好地扩展。@bereal,这是正确的做法。因为他是初学者,所以我只是保持简单,没有变量或其他方法。可以避免使用
Makefile
,例如,通过使用替代的构建自动化工具(如…)或简单的
compile\u and\u run
shell脚本
make test&&./test
。如果您的项目很简单,只有一个.c文件,没有头execept系统头,没有预处理器/编译器/链接器标志,那么您甚至不需要
Makefile
。您使用什么编译器和什么操作系统?看起来您正在使用一些Linux…如何
gcc test.c-o test/测试
?您确实需要创建一个可执行文件,但是可以避免使用
Makefile
,例如,通过使用替代的构建自动化工具(如…)或简单的
编译和运行
shellscript@BasileStarynkevitch,是的,同样地,通过使用makefile可以避免使用
忍者
脚本或shell脚本。为作业选择正确的工具。Shell脚本没有内置关于如何编译/链接文件的知识,也没有依赖项,也没有自动重建比依赖项旧的文件。Ninja有依赖项和自动重建,但没有用于编译或链接的内置命令。Ninja通常应与前端一起使用,以创建Ninja文件。对于小项目来说,
make
通常更简单。我想我很清楚它们的区别。问这个问题更多的是出于我运行这些命令的紧迫性,并且对此感到有点厌倦
ccr() { gcc "$1" -o x.$$ && ./x.$$; rm -f x.$$ }

$ ccr hello.c
Hello, world!
$
# Tell make that `all` and `run` is technically not files that will be built
.PHONY : all run

# These flags are passed to the compiler, we always want to compile with 
# warnings when developing
CFLAGS= -Wall

# `all` is the first rule, so that is the one that will be build not    
# specifying anything on the command line
# `all` also depends on `test` so that will be built from `test.c` calling 
# `make` or `make all` 
all: test

# `make run` will run your command. `run` depends on `all` to make sure the 
# program exist before calling `./test`
# Note the the indent must be made with a tab and not spaces
run: all
       ./test
# Example of a Makefile for a project that is composed of the files 
# test.c foo.c, bar.c, foo.h and bar.h
# The main-function is in test.c, and the generated program will be
# called `test`
#  
.PHONY: all run
CFLAGS= -Wall


all: test

# foo.c includes foo.h therefore foo.o depends on foo.h in addition to foo.c
foo.o: foo.h 

# bar.c includes bar.h therefore foo.o depends on bar.h in addition to bar.c
bar.o: bar.h 

# test.c includes both foo.h and bar.h 
test.o: foo.h bar.h 

# test should be linked with foo.o and bar.o in addition to test.o
test: foo.o bar.o

run: all
       ./test