Steps and methods to run C program

Mondo Technology Updated on 2024-02-01

If it is a compiled C program, then as long as the program file can be recognized as an executable file by the current system, you can directly double-click to run, and the interface will be rendered, and the console will be opened if it is a pure console program, and if it is a background program, it will run silently in the background. When I think, this situation is definitely not what you want to ask, what you want to ask should beHow to compile and run the C program source code

The steps to compile and run the source code of a C program are generally as followsBuild a compilation environment, debug source code, compile and packagewithRun the program

Just like us humans, if you want to understand articles written in English, you must first have the ability to understand English, and the same is true for operating systems, if you want to be able to parse source code files written in C language, you must have corresponding support, and this support isDevelopment environment。When it comes to compiled languages such as C, the so-called development environment isCompile environment

The specific way to build the compilation environment is also very simple, that is, on the current system** or install tools with the function of compiling C language**, such as clang and gcc.

Of course, in order to make it easier to operate at compile time, you can also install CMAKE, IDE and other tools at the same time. I just adopted itClang+Cmake+VS Code to build a C language compilation environment.

VS Code is a visual application that can be installed through a DMG file, and to get the DMG file of VS Code, of course, you can go to the official website**. Clang and cmake, on the other hand, are program tools that do not require visual operations and can be installed using the brew tool. Of course, before installation, you can check whether these two tools are already available in the current system environment, because macOS has many built-in development tools compared to Windows systems.

What I do know is that macOS has a built-in Clang tool, while the Cmake tool requires an extra**.

In addition, considering the space, I don't introduce how VS code is related to clang and cmake here, please forgive me.

In order to avoid duplication of work, we get a copy of the source code of the C program, which should be debugged first, rather than compiled and packaged into an executable file, because there may be some in the source codeThe compilation does not report an error, and the execution errordefects**. This is where the cmake tool comes inDebuggingfeatures, we can make the most of them.

When debugging, you need to put a breakpoint in the right place to let the program stay. Of course, in order to better observe, VS code also needs to be properly configured, which is invscode directoryjson file:

"setupcommands": [

Add it to the end of the file, and then start the debug mode, in the debugging window, we can view the memory situation such as the variables of the program, and execute the program step by step.

Combined with the display displayed at the top of the screenDebug control buttons, you can debug it on a process-by-process basis. If the debugging process is running incorrectly, then modify it; If there are no errors after the whole debugging, then it is time to compile and package.

Compiling, packing, and running the program are both relatively simple, so they're put together.

By installing a cmake tool extension in VS Code, we can implement this on the right side of the VS Code editorMain sidebarCmake operations in a visual way, such as compiling a project, executing a project.

The sidebar view provided by the cmake tool extension is as follows.

aboveTo build is to compilePackingmeaning, whileTo start is to runProcedureIn addition, there are trigger options for debugging and other functions in the sidebar.

Once the source code is compiled, an executable file is generated in the build directory.

To run the program, either by using the startup options provided by the Cmake Tool, or by double-clicking on the target program by opening it to the build directory with the Finder. Obviously, the first way is easier and faster.

Related Pages