cmake crashed when generating compilation database
set(CMAKE_C_COMPILE_OBJECT "<CMAKE_C_COMPILER> -DABC=1 -DXYZ=1 <FLAGS> <INCLUDES> <SOURCE> -o <OBJECT>")
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
crash info:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::replace: __pos (which is 18446744073709551615) > this->size() (which is 537)
Aborted (core dumped)
callstack:
Thread 1 (Thread 0x7f51d3d91740 (LWP 251948)):
#0 0x00007f51d207cfd7 in raise () from /lib64/libc.so.6
#1 0x00007f51d207e3aa in abort () from /lib64/libc.so.6
#2 0x00007f51d2995ea3 in ?? () from /usr/lib64/libstdc++.so.6
#3 0x00007f51d299be98 in ?? () from /usr/lib64/libstdc++.so.6
#4 0x00007f51d299bee3 in std::terminate() () from /usr/lib64/libstdc++.so.6
#5 0x00007f51d299c113 in __cxa_throw () from /usr/lib64/libstdc++.so.6
#6 0x00007f51d2997e01 in ?? () from /usr/lib64/libstdc++.so.6
#7 0x00007f51d29dc511 in std::string::replace(unsigned long, unsigned long, char const*, unsigned long) () from /usr/lib64/libstdc++.so.6
#8 0x00000000007bf3a9 in cmMakefileTargetGenerator::WriteObjectRuleFiles(cmSourceFile const&) ()
#9 0x00000000007c17f4 in cmMakefileTargetGenerator::WriteTargetBuildRules() ()
#10 0x00000000007d9f95 in cmMakefileLibraryTargetGenerator::WriteRuleFiles() ()
#11 0x00000000007b3238 in cmLocalUnixMakefileGenerator3::Generate() ()
#12 0x0000000000769f18 in cmGlobalGenerator::Generate() ()
#13 0x000000000077b29e in cmGlobalUnixMakefileGenerator3::Generate() ()
#14 0x000000000052c3f9 in cmake::Generate() ()
#15 0x000000000052c8f8 in cmake::Run(std::vector<std::string, std::allocator<std::string> > const&, bool) ()
#16 0x0000000000426108 in (anonymous namespace)::do_cmake(int, char const* const*) ()
#17 0x000000000041ad83 in main ()
look at the code in function cmMakefileTargetGenerator::WriteObjectRuleFiles: https://gitlab.kitware.com/cmake/cmake/-/blob/v3.19.0/Source/cmMakefileTargetGenerator.cxx#L841
std::string langDefines = std::string("$(") + lang + "_DEFINES)";
compileCommand.replace(compileCommand.find(langDefines),
langDefines.size(),
this->GetDefines(lang, this->GetConfigName()));
if compileCommand
does not contain a C_DEFINES, string.find return value of -1, which cause the crash in later process.