Issue
I have project which i check with cppcheck 1.90, but i want to unset few defines, because it takes a lot of time to check with all defines.
Example: tests.cpp
:
int main() {
int x,y;
#ifdef A
x = 5;
#else
x = 10;
#endif
#ifdef B
y = 5;
#else
y = 10;
#endif
#ifdef C
y = 5*x;
#else
y = 10*x;
#endif
int c = x+y;
}
When i work with file without project - there is no problem:
cppcheck ../tests.cpp --force -UB
Checking ../tests.cpp ...
Checking ../tests.cpp: A...
Checking ../tests.cpp: C...
I unset define B - and cppcheck don't check compile path with B. But how to do it with file generated from cmake project?
cmake ../ -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
cppcheck --project=./compile_commands.json --force -UB
Checking /home/builder/bugs/test_cppcheck/tests.cpp ...
Checking /home/builder/bugs/test_cppcheck/tests.cpp: A...
Checking /home/builder/bugs/test_cppcheck/tests.cpp: B...
Checking /home/builder/bugs/test_cppcheck/tests.cpp: C...
As i can see cppcheck is ignored "-U" key. Is any way to unset define in that case?
Add cmakefile for test example:
CMakeLists.txt
cmake_minimum_required(VERSION 3.6)
project(tests)
set(SRC
tests.cpp
)
set(NAME test)
add_executable(${NAME} ${SRC})
Solution
You have an option to edit the CMakeLists file only once and pass arguments as needed, To achieve this you should
In the CMakeLists file add:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} *hard_coded_flags*")
Command line :
When calling cmake
from command line you add CMAKE_CXX_FLAGS = -UB=1
(or any other cppcheck flag), this will concatenate -UB=1
the to the hard_coded_flags
.
Answered By - joepol