VScode does not recognize macros defined in CMakeList

1. Problem description

In the ubuntu system, it is usually customary to use VScode as the IDE to read or modify the code. After opening the project with VScode, it is usually necessary to configure c_cpp_properties.json, settings.json, tasks.json, etc. After setting the path correctly, It can basically solve most of the errors in the program. When reading the Fast-Lio code, I found that there is still an error: undefined identifier "ROOT_DIR". As shown below

Second, analyze the reasons

After viewing the program, it is considered that "ROOT_DIR" is a macro, but its definition is not found in each header file. Finally found that "ROOT_DIR" is a macro defined in the CMakeList.txt file, as shown below:

add_definitions(-DROOT_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/\")

 For this kind of macro defined during compilation, it cannot be solved by including a certain *.c or *.h, so VScode will report an error that the macro is not defined.

Three, the solution

Add the macro definition in c_cpp_properties.json, as shown in the figure below, and then save it, and the error will disappear.

{
    “configurations”:[
        {
            "defines":[
                "ROOT_DIR=***"
            ],
        }
    ]
}

Note: Defining "ROOT_DIR" may cause other errors. For example, if you use this macro to assign a value to string in the program, it is best to define it as "ROOT_DIR=*** ", and assign it any value. It is fine to hide vscode without reporting an error. Anyway, compile When using the assignment in CMakeList.

Reference blog: 

Add custom preprocessor definitions? · Issue #304 · microsoft/vscode-cpptools (github.com)

VSCODE reads C code--some macro definitions display abnormal problem solving_Macro definition cannot be recognized_Unlimited Life Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/weixin_44884315/article/details/130143940