【问题标题】:Antlr4 with C++ and CMakeAntlr4 与 C++ 和 CMake
【发布时间】:2018-04-26 19:49:08
【问题描述】:

我正在尝试将 antlr4 与 C++ 一起使用。我的根目录中有以下 CMakeLists.txt:

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)

我或多或少地复制了以下示例:the "official" cmake/antlr tutorial。为简洁起见,我省略了include (${CMAKE_SOURCE_DIR}/antlr/runtime/ExternalAntlr4Cpp.cmake)的内容

所以这些文件都在相同的结构中,除了 jar。我只是将它作为项目文件包含在内,正如您从 set(ANTLR4CPP_JAR_LOCATION) 命令中看到的那样,它位于我想要的位置。

当我构建这个时,我得到了这个错误:

dyld: Library not loaded: libantlr4-runtime.4.7.1.dylib
Referenced from: /path/to/demo/cmake-build-debug/demo
Reason: image not found

但是,当我查看:/path/to/demo/cmake-build-debug/external/antlr4cpp/lib/ 时,我看到了应该使用 target_link_libraries(demo antlr4-runtime) 命令引用的 libantlr4-runtime.4.7.1.dylib 文件。里面也有静态库。

我错过了什么吗?为什么 CMake 找不到它应该找到的静态或 dylib 文件?

【问题讨论】:

    标签: cmake clion


    【解决方案1】:

    在我开始之前,我没有意识到我运行了brew install antlr4-cpp-runtime。哪个安装了路径的标题。我在我的机器上做了这个,但不是在构建这个项目的任何其他机器上。突出的问题是,如果没有安装运行时,为什么它不是确定性的。但它在安装运行时后立即生效。

    【讨论】:

      猜你喜欢
      • 2020-08-23
      • 2022-08-15
      • 1970-01-01
      • 1970-01-01
      • 2021-06-09
      • 1970-01-01
      • 2020-10-17
      • 1970-01-01
      • 2013-01-06
      相关资源
      最近更新 更多