Clangd error on placement new operator

已完成

Hello All,

I'm struggling with a strange error on my C++ code. I'm using placement new operator as follows:

class Partition
{
......
}

part_ptr = malloc(sizeof(Partition));
if (part_ptr == NULL) {
// ... error handling here
}
part = new (part_ptr) Partition(partition);

In CMakeLists, I set CMAKE_CXX_STANDARD 17. Also tried with 14 and 11...

My problem is that the built-in clangd syntax checker in CLion indicates and error on the "new (part_ptr)..." statement. The error reads "No matching function for call to 'operator new'". It's not shown as a warning, it's shown as a full-blown error.

The code compiles without errors using gnu C++ compiler, using gnu++11 language standard.

Does anyone have a hint what I am missing? Either in CMake, or a CLion setting maybe?

Thanks,
Norbert

评论操作 固定链接

Hello!

Please provide a short self-contained code sample which can be compiled and with which the issue can be reproduced.

0
评论操作 固定链接

Hello,

Thanks for your reply. I did as requested, and while doing it, I realized a simple C++ code sample using the MINGW environment I have works perfectly. I managed to debug further what's going on here. It boils down to the issue of size_t type differences in the actual compiler used to build the project and the interpretation of size_t of the built-in CLANG compiler.

I'm using a custom toolchain for my project (embedded development), with its own compiler. However, since CLion does not recognize this environment I need to set MINGW for Windows to have a compile environment for CLion (settings-->toolchains-->environment). Now, my guess is that CLANG uses the MINGW environment to deduct target architecture, which in my case will be 64 bits. As a consequence, CLANG and CLANGD expects size_t to be unsigned long long. My embedded environment, being 32 bits, uses unsigned long. Hence the CLion syntax checker complains that there's no matching function call for the overload new operator.

Currently I could overcome the issue by using a compiler-targeted conditional define and override __SIZE_TYPE__, which is used in header new to deduct size_t size.

I'm now investigating if I can somehow adjust the MINGW environment to match the 32-bit target I'm building for, which would be the ultimate solution.

1
评论操作 固定链接

Syrel How were you able to define size_t?

I'm writing code for the ESP32 and running into this

0
评论操作 固定链接

Syrel
open File | Settings | Languages & Frameworks | C/C++ | Clangd and append ,--target=riscv32 to the end of clangd diagnostics options

4
评论操作 固定链接

Thank you Ilia Motornyi, you saved my day. I am using platformIO and CLion and now everything works fine.

0
评论操作 固定链接

Hi! I wrote a detailed getting started guide on how to set up CLion and PlatformIO in case anyone had these issues like me.

https://johetajava.hu/en/post/platform-io-clion

0
评论操作 固定链接

Ambrus, awesome, cool! One small note - I think you may skip CMake and MinGW installation, because latest CLion(2021.3 will be released soon) comes with bundled MinGW and CMake

1
评论操作 固定链接

Thank you! I will update my CLion and the post when it comes out, it would simplify a lot of this :D 

0
评论操作 固定链接

Just in case, the CLion EAP build with the bundled MinGW is already available - https://www.jetbrains.com/clion/nextversion/.

0

请先登录再写评论。