bkryza / clang-uml

Customizable automatic UML diagram generator for C++ based on Clang.
Apache License 2.0
610 stars 44 forks source link

no member named 'variant' in namespace 'std' #271

Closed allbluelai closed 6 months ago

allbluelai commented 6 months ago

Hello, I added the following in t20001.cc:

#include <variant>
// in tmain()
std::variant<double, char, bool> aaaa;

my CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
project(testMain)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
add_executable(main main.cpp t20001.cc)

main.cpp just std::cout "hello world" code.

When I ran clang-uml, it outputed:

$ clang-uml
fatal: not a git repository (or any of the parent directories): .git
[info] [tid 13832] [cli_handler.cc:305] Loaded clang-uml config from .clang-uml
[info] [tid 13832] [cli_handler.cc:332] Loading compilation database from /home/john/Documents/test/build/ directory
[info] [tid 13835] [generators.h:368] Generating diagram t20001_sequence
/home/john/Documents/test/t20001.cc:73:18: error: no member named 'variant' in namespace 'std'
            std::variant<double, char, bool> aaaa;
            ~~~~~^
/home/john/Documents/test/t20001.cc:73:32: error: expected '(' for function-style cast or type construction
            std::variant<double, char, bool> aaaa;
                         ~~~~~~^
2 errors generated.
Error while processing /home/john/Documents/test/t20001.cc.
[error] [tid 13835] [generators.cc:274] ERROR: Failed to generate diagram t20001_sequence: Diagram t20001_sequence generation failed

I used CMakeLists.txt to set CPP compiler as C++ 17/20. (see set(CMAKE_CXX_STANDARD 17) in CMakeLists.txt) The results in t20001 PlantUML only worked for C++ 20, and failed for C++ 17.

bkryza commented 6 months ago

@allbluelai Strange, it works for me, can you paste the output of the following command:

cat debug/compile_commands.json | grep t20001.cc

and also:

clang-uml --version
allbluelai commented 6 months ago

@bkryza Hello,

cat debug/compile_commands.json | grep t20001.cc
```sh
$ cat build/compile_commands.json | grep t20001.cc
"command": "/usr/bin/c++  -isystem /usr/include/c++/11 -isystem /usr/include/x86_64-linux-gnu/c++/11 -isystem /usr/include/c++/11/backward -isystem /usr/lib/gcc/x86_64-linux-gnu/11/include -isystem /usr/local/include -isystem /usr/include/x86_64-linux-gnu -isystem /usr/include  -o CMakeFiles/main.dir/t20001.cc.o -c /home/john/Documents/test/t20001.cc",
"file": "/home/john/Documents/test/t20001.cc"

clang-uml --version

$ clang-uml --version
clang-uml 0.5.2
Copyright (C) 2021-2024 Bartek Kryza <bkryza@gmail.com>
Linux x86_64 6.5.0-25-generic
Built against LLVM/Clang libraries version: 15.0.7
Using LLVM/Clang libraries version: Ubuntu clang version 15.0.7
bkryza commented 6 months ago

@allbluelai Ok, so it seems that for some reason your CMake does not add the -std=c++17 flag to the compilation command - and std::variant requires at least C++ 17.

Can you try to add -std=c++17 manually to the compile_commands.json like that:

"command": "/usr/bin/c++  -std=c++17  -isystem /usr/include/c++/11 ...

and see if this helps. If yes, you'll have to figure out how to force CMake to add this flag to your compile_commands.json during build.

One idea is to add the following line at the end of your CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
project(testMain)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
add_executable(main main.cpp t20001.cc)

target_compile_features(main PRIVATE cxx_std_17)
allbluelai commented 6 months ago

Can you try to add -std=c++17 manually to the compile_commands.json like that:

"command": "/usr/bin/c++  -std=c++17  -isystem /usr/include/c++/11 ...

and see if this helps.

Yes, it helps.

target_compile_features(main PRIVATE cxx_std_17)

This does not work for me. But I use the new `CMakeLists.txt':

cmake_minimum_required(VERSION 3.0)
project(testMain)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17") # new line
# set(CMAKE_CXX_STANDARD 17) # delete it, no need
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
add_executable(main main.cpp t20001.cc)

It works. And compile_commands.json for t20001.cc:

{
"directory": "/home/john/Documents/test/build",
"command": "/usr/bin/c++  -isystem /usr/include/c++/11 -isystem /usr/include/x86_64-linux-gnu/c++/11 -isystem /usr/include/c++/11/backward -isystem /usr/lib/gcc/x86_64-linux-gnu/11/include -isystem /usr/local/include -isystem /usr/include/x86_64-linux-gnu -isystem /usr/include  -std=c++17 -o CMakeFiles/main.dir/t20001.cc.o -c /home/john/Documents/test/t20001.cc",
"file": "/home/john/Documents/test/t20001.cc"
}

@bkryza Finally, thank you again. I'll close it.

leha-bot commented 5 months ago

Actually c++17 support has been added in CMake 3.8, I suggest to upgrade the cmake minimum version to 3.8 https://cmake.org/cmake/help/latest/release/3.8.html#id6

allbluelai commented 5 months ago

@leha-bot Hi,

Actually c++17 support has been added in CMake 3.8, I suggest to upgrade the cmake minimum version to 3.8 https://cmake.org/cmake/help/latest/release/3.8.html#id6

Sorry, using CMake 3.8 didn't help for me. Even if I use CMake 3.22.1 (the latest version in my OS) it doesn't help. My CMakeLists.txt:

cmake_minimum_required(VERSION 3.22.1)
project(testMain)
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17") # comment out
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
add_executable(main main.cpp t20001.cc)

target_compile_features(main PUBLIC cxx_std_17)

And no -std=c++17 in compile_commands.json:

{
  "directory": "/home/john/Documents/test/build",
  "command": "/usr/bin/c++  -isystem /usr/include/c++/11 -isystem /usr/include/x86_64-linux-gnu/c++/11 -isystem /usr/include/c++/11/backward -isystem /usr/lib/gcc/x86_64-linux-gnu/11/include -isystem /usr/local/include -isystem /usr/include/x86_64-linux-gnu -isystem /usr/include  -o CMakeFiles/main.dir/t20001.cc.o -c /home/john/Documents/test/t20001.cc",
  "file": "/home/john/Documents/test/t20001.cc"
}

OS:


$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.4 LTS
Release:        22.04
Codename:       jammy