Article Directory
foreword
I have used many kinds of editors, such as Visual Studio, VS Code, Pycharm, Dev c++ , etc. In the end, Geany , a compact editor, is the most convenient. Its biggest advantage is that it is lightweight , and its basic functions are quite comprehensive , so I use it for some simple code exercises.
How to compile a single C file
There is a Build icon in the menu bar of Geany, which looks roughly like this after clicking on it.
Among them, the first column C commands is the place to set compilation, assembly, linking and other operations. It can be seen that the underlying implementation is actually completed by calling the GCC compiler . Here is a brief introduction to the common syntax of GCC.
GCC common commands
- Precompile : macro definition expansion, header file expansion, conditional compilation, delete all comments. No grammar checking is done.
- command :
gcc -E xxx.c -o xxx.i
- Generate precompiled files , suffix
.i
-o
Refers to the meaning of output, followed by specifying the file name
- command :
- Compile : Compile the preprocessed file into an assembly file for syntax checking
- command :
gcc -S xxx.i -o xxx.s
- Generate assembly file , suffix
.s
- command :
- Assembly : Convert the assembly file from the previous step into a binary file.
- command :
gcc -c xxx.s -o xxx.o
- Generate object files , suffix
.o
- command :
- Link : Generate executable files from all object files + library functions + startup files.
- command :
gcc xxx.o -o xxx.exe
- Generate executable files , suffix
.exe
- command :
- execute : execute
xxx.exe
the file- Command : xxx
- Enter the file name directly to execute
Notice:Each command means that it can be xxx.c
directly executed from the source file ( ) to the current output format. Therefore, the latter two commands are commonly used. Take a chestnut:
gcc -c test.c -o test.o //执行预编译+编译+汇编,直接输出test.o文件
gcc test.c -o test.exe //执行预编译+编译+汇编+链接,直接输出test.exe文件
-o
Specify the generated file name later, and the suffix can be omitted (the compiler will automatically generate it).
All the above commands can be run in the console ( cmd ). If an exception is thrown, check two things:
- Whether the GCC system environment is configured . Validation can be entered directly
gcc
. - Whether it is in the current c file directory . See the cmd
cd
command for details.
Compile configuration
Therefore, we return to the Geany configuration, the command corresponding to Compilegcc -Wall -c "%f"
is: . -Wall
All warning messages can be generated. -c
Indicates that this is an assembly command that converts an input file into an object file .o
. %f
It is an alternative format in the Geany editor. For details, please refer to: Geany Manual . The common ones are listed as follows:
%e
: The name of the current file without extension or path.%f
: The name of the current file without a path.
So here "%f"
stands for the current file name (with suffix) .
Summary: Click the Compile icon, the assembly operation will be performed, and the target file will be generated in the current directory .o
.
Build configuration
Compared with Compile , Build uses more, the command is: gcc -Wall -o "%e" "%f"
. According to the above, we can see that this is actually inputting the current file ( "%f"
), directly precompiling + compiling + assembling + linking to generate the specified executable file ( "%e"
), and the file name is consistent.
Execute configuration
The command is: "./%e"
, ./
which represents the current directory and %e
is the current file name (without suffix) , so clicking Execute will directly execute the file generated by Build.exe
.
How to compile multiple C files
The above method can only compile a single C file. If we write the header file ourselves, Geany's current configuration cannot find the implementation file corresponding to the header file . Of course, using the GCC command directly in cmd can also compile multiple files at the same time, but this article only discusses the implementation in Geany .
solution
Create a new folder to store all the C files to be compiled (you can also choose to create a project , click Project -> New ). At the same time, change the command corresponding to Buildgcc -Wall ./*.c -o "%e"
to , so that as long as you click Build, all C files in the current directory will be compiled and the corresponding executable files will be generated.
Summarize
It is quite convenient for Geany to write some scripts , but it does not do a good job in project creation . At least in other IDEs , it may automatically compile the files of the current project. This article provides only one solution.