IDE support for Blade projects

Blade is a C/C++ build tool. For details, see Blade Build Tool . While powerful, it lacks IDE support. This article introduces how to use the IDE's smart prompt, auto-completion, and auto-jump features in the Blade project.

1.VSCode

VSCode's C/C++ plug-in (ms-vscode.cpptools) provides functions such as intelligent prompting, automatic completion and debugging of C/C++ code. For Blade projects, the plugin will automatically retrieve the project source files and support

  • Code intelligence hints and auto-completion
  • Jump relative to the header file of the project root directory or build directory

Autocompletion and jumping functions are available for both normal code and protobuf-generated code.

For example, in the example project in section 4 of the Blade build tools :

VSCode auto-completion

Advantages: Simple configuration, only one plug-in needs to be installed.

shortcoming:

  • VSCode's automatic jump function is actually based on keyword matching, and does not really analyze the C++ code. If there are functions with the same name in different files, they need to be selected manually.
  • For large projects, the retrieval process will be very slow, resulting in frequent failure of autocompletion and high memory usage.

2.CLion

CLion itself only supports two build tools, Make and CMake. However, for projects that are not based on Make or CMake, you can also use the compilation database to load, so that you can use the IDE features provided by CLion, see the document Compilation database for details .

The compile database is a JSON file describing compile commands, called compile_commands.json (this can be added to .gitignore to avoid committing to git). CLion can extract the necessary compiler information from it, such as include paths, compile options, etc. Fortunately, Ninja, the underlying build tool used by Blade, provides a tool compdb that can generate a compilation database based on the BUILD file.

Still taking the above Blade sample project as an example, by default CLion cannot perform auto-completion and jumping:

Can't autocomplete

cannot jump to symbol

cannot jump to header file

First install CLion. If the code is located on a remote development machine, refer to the document Remote Development to connect to the remote development machine.

Then follow the steps below to generate the compilation database to enable the IDE function of CLion:

Step 1: Use the blade command on the command line to build the required target. The purpose is to generate a Ninja build file build.ninja that includes the target and all its downstream dependencies. You can use options to avoid actually executing the compile command -n:

blade build -n //quick-start:hello_world

After the build is complete, the build.ninja file will be generated in the blade-bin directory.

Step 2: Use Ninja's compdb tool to generate the compilation database, and execute the following command in the project root directory:

ninja -f blade-bin/build.ninja -t compdb cc cxx cxxhdrs > compile_commands.json

This will generate a compile_commands.json file at the project root.

Step 3: Use CLion to open the project. CLion will automatically load compile_commands.json. After the loading is complete, you can see the success information in the Build window (ignore the red letters):

Imported successfully

Then you can use the IDE features provided by CLion:

CLion autocompletion

CLion Smart Tips

The jump to symbol and header file functions also work normally.

Guess you like

Origin blog.csdn.net/zzy979481894/article/details/130352813