skypjack / uvw

Header-only, event based, tiny and easy to use libuv wrapper in modern C++ - now available as also shared/static library!
MIT License
1.84k stars 209 forks source link

[CLang] error: constexpr variable must be initialized by a constant expression #226

Closed vladfaust closed 3 years ago

vladfaust commented 3 years ago

Hello,

The code does not compile with CLang 10 neither on native Windows (LLVM prebuilt) nor MinGW64:

# Include paths and targets are omitted for clarity
[build] C:\Dev\LLVM\10\Prebuilt\bin\clang++.exe -DUSING_UV_SHARED=1 -g -Xclang -gcodeview -O0 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd -fcoroutines-ts -std=c++2a -MD -MT 

[build] In file included from ../test/cpp/uvw.cpp:4:
[build] In file included from ../lib/uvw/vendor/src\uvw.hpp:1:
[build] In file included from ../lib/uvw/vendor/src/uvw/async.h:6:
[build] In file included from ../lib/uvw/vendor/src/uvw/handle.hpp:9:
[build] In file included from ../lib/uvw/vendor/src/uvw/resource.hpp:8:
[build] In file included from ../lib/uvw/vendor/src/uvw/underlying_type.hpp:8:
[build] In file included from ../lib/uvw/vendor/src/uvw/loop.h:16:
[build] ../lib/uvw/vendor/src/uvw/util.h:373:35: error: constexpr variable 'addrFunc' must be initialized by a constant expression
[build]     static constexpr AddrFuncType addrFunc = &uv_ip4_addr;
[build]                                   ^          ~~~~~~~~~~~~

It does compile on MinGW64 GCC, though.

I guess that the problem is that taking an address of a variable is not a constant expression.

skypjack commented 3 years ago

Actually, uv_ip4_addr is a function and taking its address is a constant expression. Though, it's not when the library is dynamically loaded. Can I ask you to repeat the test with a statically linked version of libuv if possible? Thank you very much.

vladfaust commented 3 years ago

@skypjack I've managed to successfully link with static libuv with both Windows and MinGW64 CLang(s). It is enough for my case -- writing a compiler --, but I still think it is a mistake to rely on PIC in the wrapper.

skypjack commented 3 years ago

Yup, I agree with you. I wasn't suggesting to do that to solve the problem. Your feedback helped to confirm my guess though. 😉