bash 如何编译给定目录中的所有c文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32029445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compile all the c files in given directory?
提问by Shrey Kumar
I want to compile all the files in a given directory.
我想编译给定目录中的所有文件。
I don't want to use
我不想使用
gcc -o main file1.cpp file2.cpp file3.cpp ...
because there are many files.
因为有很多文件。
So how do I compile all the files whose extension is .cpp in a given directory using terminal/bash?
那么如何使用终端/bash 编译给定目录中所有扩展名为 .cpp 的文件?
回答by dev.null
You are asking for files with extension .cpp? So you can just type:
您要求扩展名为 .cpp 的文件?所以你可以输入:
gcc -o main *.cpp
gcc -o main *.cpp
The shell expand the asterisk to file1.cpp, file2.cpp and so on of the current directory.
shell将星号展开为当前目录的file1.cpp、file2.cpp等。
回答by Mingle Li
I'm assuming you're using Mac / Linux:
我假设您使用的是 Mac/Linux:
- CD into the directory of your .c file:
cd /path/tofile/
. - Congrats! You are in the directory of your .cpp file. Just type (or copy and paste) this-
gcc -o main file.cpp
- to compile one file, or this -gcc -o main *.cpp
for all files - If that was successful, just run the program with
./main
, or whatever you named your executable.
- CD 进入您的 .c 文件的目录:
cd /path/tofile/
. - 恭喜!您位于 .cpp 文件的目录中。只需键入(或复制并粘贴)这个
gcc -o main file.cpp
- - 编译一个文件,或者这个 -gcc -o main *.cpp
所有文件 - 如果成功,只需使用
./main
或任何您命名的可执行文件运行程序。
Hope this works for you!
希望这对你有用!