antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
17.04k stars 3.27k forks source link

ANTLRInputStream constructor with string_view #2992

Open rea-yseop opened 3 years ago

rea-yseop commented 3 years ago

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.

g199209 commented 3 years ago

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 commented 3 years ago

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

mike-lischke commented 3 years ago

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.

rea-yseop commented 3 years ago

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 ?

mike-lischke commented 3 years ago

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.

rea-yseop commented 3 years ago

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 commented 3 years ago

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.

mike-lischke commented 3 years ago

A patch has been merged recently for this exact problem.

sdoiel61 commented 2 years ago

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