I'm trying to use antlr4 with C++. I have the following CMakeLists.txt in my root directory:
cmake_minimum_required(VERSION 3.10)
project(demo VERSION 0.1 DESCRIPTION "demo")
include_directories(${CMAKE_CURRENT_BINARY_DIR})
set(CMAKE_CXX_STANDARD 17)
#############################################################################
# ANTLR SPECIFIC CMAKE COMMANDS
# This is derived from: https://github.com/blorente/antlr-4.7-cpp-cmake-base
#############################################################################
# Set the ocation of the JAR.
set(ANTLR4CPP_JAR_LOCATION ${CMAKE_SOURCE_DIR}/antlr/jar/antlr.jar)
# Add external build for antlrcpp.
include (${CMAKE_SOURCE_DIR}/antlr/runtime/ExternalAntlr4Cpp.cmake)
# add antrl4cpp artifacts to project environment.
include_directories( ${ANTLR4CPP_INCLUDE_DIRS} )
link_directories( ${ANTLR4CPP_LIBS} )
# message(WARNING "Found antlr4cpp libs: ${ANTLR4CPP_LIBS} and includes: ${ANTLR4CPP_INCLUDE_DIRS} ")
# Build the lexer/parser .h/.cpp files off the g4 grammar files.
antlr4cpp_process_grammar(demo demoparser
${CMAKE_SOURCE_DIR}/grammar/DemoLexer.g4
${CMAKE_SOURCE_DIR}/grammar/DemoParser.g4)
# include the generated files from the grammar/lexer.
include_directories(${antlr4cpp_include_dirs_demoparser})
#############################################################################
# Build demo
add_executable(demo main.cpp ${antlr4cpp_src_files_demoparser})
# Add dependencies for antlr
add_dependencies(demo antlr4cpp antlr4cpp_generation_demoparser)
target_link_libraries(demo antlr4-runtime)
I have, more or less, copied the example from: the "official" cmake/antlr tutorial. For brevity I have omitted the contents of include (${CMAKE_SOURCE_DIR}/antlr/runtime/ExternalAntlr4Cpp.cmake)
So the files are all in the same structure, except the jar. I just include it as a project file and, as you can see from the set(ANTLR4CPP_JAR_LOCATION)
command, it resides where I want it.
When I build this, I get this error:
dyld: Library not loaded: libantlr4-runtime.4.7.1.dylib
Referenced from: /path/to/demo/cmake-build-debug/demo
Reason: image not found
However, when I look in: /path/to/demo/cmake-build-debug/external/antlr4cpp/lib/
I see the libantlr4-runtime.4.7.1.dylib
file that should be referenced by using the target_link_libraries(demo antlr4-runtime)
command. There are static libraries in there as well.
Am I missing something? Why is CMake not finding the static or dylib files it's supposed to?
I didn't realize, right before I started this, that I ran brew install antlr4-cpp-runtime
. Which installed the headers to the path. I did this on my machine, but not on any of the other machines that build this project. The outstanding problem is why is it not deterministic is if the runtime isn't installed. But it works immediately upon installing the runtime.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With