antlr4 icon indicating copy to clipboard operation
antlr4 copied to clipboard

ANTLRInputStream constructor with string_view

Open rea-yseop opened this issue 5 years ago • 9 comments

I updated antlr to 4.9 and I am using the cpp version. My problem seems to be related to https://github.com/antlr/antlr4/pull/2847 where the constructor has been changed.

I have the following code

void foo(const std::string& input) {
  antlr4::ANTLRInputStream input_stream(input);
}

Which returns

undefined reference to `antlr4::ANTLRInputStream::ANTLRInputStream(std::basic_string_view<char, std::char_traits<char> >)'

I went to check to symbols of the lib and i only had image

I am using c++20 for my project but from https://github.com/antlr/antlr4/blob/4.9/runtime/Cpp/CMakeLists.txt

# Initialize CXXFLAGS.
if("${CMAKE_VERSION}" VERSION_GREATER 3.1.0)
  set(CMAKE_CXX_STANDARD 11)
  set(CMAKE_CXX_STANDARD_REQUIRED ON)
else()
  set(CMAKE_CXX_FLAGS                "${CMAKE_CXX_FLAGS} -std=c++11")
  set(CMAKE_CXX_FLAGS_DEBUG          "${CMAKE_CXX_FLAGS_DEBUG} -std=c++11")
  set(CMAKE_CXX_FLAGS_MINSIZEREL     "${CMAKE_CXX_FLAGS_MINSIZEREL} -std=c++11")
  set(CMAKE_CXX_FLAGS_RELEASE        "${CMAKE_CXX_FLAGS_RELEASE} -std=c++11")
  set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -std=c++11")
endif()

It looks like we are using c++11. Not sure if this is because of that since we need the string_view from c++17.

rea-yseop avatar Dec 04 '20 16:12 rea-yseop

I have the same problem.

antlr4::ANTLRInputStream inputStream("........");

Using string literal directly also have link error.

undefined reference to `antlr4::ANTLRInputStream::ANTLRInputStream(std::basic_string_view<char, std::char_traits<char> >)'

GCC version: 9.3 Compile flag: --std=c++2a

g199209 avatar Dec 07 '20 03:12 g199209

I simplly changed CMakeLists.txt in cpp runtime.

# Initialize CXXFLAGS.
if("${CMAKE_VERSION}" VERSION_GREATER 3.1.0)
  set(CMAKE_CXX_STANDARD 20)
  set(CMAKE_CXX_STANDARD_REQUIRED ON)
else()
  set(CMAKE_CXX_FLAGS                "${CMAKE_CXX_FLAGS} -std=c++2a")
  set(CMAKE_CXX_FLAGS_DEBUG          "${CMAKE_CXX_FLAGS_DEBUG} -std=c++2a")
  set(CMAKE_CXX_FLAGS_MINSIZEREL     "${CMAKE_CXX_FLAGS_MINSIZEREL} -std=c++2a")
  set(CMAKE_CXX_FLAGS_RELEASE        "${CMAKE_CXX_FLAGS_RELEASE} -std=c++2a")
  set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} -std=c++2a")
endif()

The problem solved. But I'm not sure shall we change it to C++ 20 directly.

@nburles @mike-lischke

g199209 avatar Dec 07 '20 04:12 g199209

C++20 is not an option currently. The runtime compiles with C++17 enabled (I use it like that in one of my projects). But we can target C++20 for the next ANTLR4 release.

mike-lischke avatar Dec 07 '20 07:12 mike-lischke

Thanks for the answers. But i also had to change the CMakeLists.txt directly for C++17. Did i miss an option to enable C++17 easily ?

rea-yseop avatar Dec 07 '20 08:12 rea-yseop

No, the current cmake file is probably still that from the beginning with C++11. I just wanted to say that I already tested the runtime with C++17 and it compiled without problems from what I remember. So it would require a change for C++17 in the cmake files.

mike-lischke avatar Dec 07 '20 11:12 mike-lischke

Ok thanks. I also tried with C++17 so i can confirm it works. Unfortunately i am using Conan so i can't really edit the file manually or change the flags so it would be nice to enable C++17 in the next version.

rea-yseop avatar Dec 07 '20 11:12 rea-yseop

Actually i ran into another error after using C++17. Don't know if I should create another issue for that though

image

For now, I just removed the const for the members and it works.

rea-yseop avatar Dec 07 '20 16:12 rea-yseop

A patch has been merged recently for this exact problem.

mike-lischke avatar Dec 07 '20 17:12 mike-lischke

I think that MSVC needs: if (MSVC) add_compile_options(/Zc:__cplusplus) endif()

To report the correct c++ version

https://docs.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=msvc-170

sdoiel61 avatar Jun 20 '22 15:06 sdoiel61