TadaEXE
TadaEXE

Reputation: 11

Unable to get antlr4.13 for c++ to link in my projct

I'm currently in the process of building a toy language with c++ as a side project and chose to use antlr4 for lexing and parsing but I just can't get it to link. I'm generally following this guide with this repo as a base, but I've also looked at other materials but nothing brought me as close to actually compiling as I'm now.

It's noteworthy that I can get this repo to build no problem, if I change line 38/39 of ExternalAntlr4Cpp.cmake from

38  set(ANTLR4_STATIC_LIBRARIES
39      ${ANTLR4_OUTPUT_DIR}/libantlr4-runtime.a)

to

38  set(ANTLR4_STATIC_LIBRARIES
39      ${ANTLR4_OUTPUT_DIR}/libantlr4-runtime-static.a)

As building that repo worked for me I chose to copy stuff over to my project. This is my current file tree:

.
├── antlr4/
│   ├── generated files (Lexer, Paser, Visitor etc.)
│   └── ...
├── cmake/
│   ├── antlr4-generator.cmake.in
│   ├── antlr4-runtime.cmake.in
│   ├── ExternalAntlr4Cpp.cmake
│   └── FindANTLR.cmake
├── src/
│   ├── main.cpp
│   ├── CMakeLists.txt
│   └── Sugar.g4
├── tools/
│   └── antlr-4.13.1-complete.jar
├── .gitingore
└── CMakeLists.txt

The content of my top-level CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)
project(Sugar)

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

message("Starting Sugar build.")

set(CMAKE_CXX_STANDARD 17)

add_subdirectory(src)

The content of src/CMakeLists.txt:

# ANTLR------------------------------------------------
# required if linking to static library
add_definitions(-DANTLR4CPP_STATIC)

# using /MD flag for antlr4_runtime (for Visual C++ compilers only)
set(ANTLR4_WITH_STATIC_CRT OFF)
# add external build for antlrcpp
include(ExternalAntlr4Cpp)
# add antrl4cpp artifacts to project environment
include_directories(${ANTLR4_INCLUDE_DIRS})

# set variable pointing to the antlr tool that supports C++
# this is not required if the jar file can be found under PATH environment
set(ANTLR_EXECUTABLE ${CMAKE_SOURCE_DIR}/tools/antlr-4.13.1-complete.jar)
# add macros to generate ANTLR Cpp code from grammar
find_package(ANTLR REQUIRED)

message("Building antlr at ${CMAKE_SOURCE_DIR}/antlr4")

# antlr_target is just not building the files....
execute_process(COMMAND antlr4 -visitor -Dlanguage=Cpp -o ${CMAKE_SOURCE_DIR}/antlr4 ${CMAKE_SOURCE_DIR}/src/Sugar.g4)

antlr_target(Sugar Sugar.g4 VISITOR OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/antlr4/)
# add our project source directories
include_directories(${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/antlr4/)
# -----------------------------------------------------

add_executable(${PROJECT_NAME} main.cpp ${ANTLR_Sugar_CXX_OUTPUTS})

target_include_directories(${PROJECT_NAME} PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}
)


target_link_libraries(${PROJECT_NAME} antlr4_static)

The content of src/main.cpp:

#include "antlr4/SugarLexer.h"
#include "antlr4/SugarParser.h"
#include "antlr4/SugarVisitor.h"
#include "antlr4-runtime.h"
#include <fstream>

int test_antlr_usage()
{
    std::ifstream stream;
    stream.open("input.scene");
    
    antlr4::ANTLRInputStream input(stream);
    SugarLexer lexer(&input);
    antlr4::CommonTokenStream tokens(&lexer);
    SugarParser parser(&tokens);    

    return 0;
}

int main(int argc, char *argv[])
{
    return test_antlr_usage();
}

I left out the unimportant parts of the projct to not make this question even bigger, but if any more details are needed I'm linking the branch that I'm currently suffering on.

The cmake configure and general build run smoothly. Even syntax highlighting within vscode works perfectly fine. The only problem is the linking. Here is an example:

[build] lld-link: error: undefined symbol: public: __cdecl antlr4::ANTLRInputStream::ANTLRInputStream(class std::basic_istream<char, struct std::char_traits<char>> &)
[build] >>> referenced by E:\Sugar\src\main.cpp:35
[build] >>>               src/CMakeFiles/Sugar.dir/main.cpp.obj:(int __cdecl test_antlr_usage

[build] lld-link: error: undefined symbol: public: virtual __cdecl antlr4::CharStream::~CharStream(void)
[build] >>> referenced by E:\Sugar\build-win-clang-debug\src\antlr4_runtime\src\antlr4_runtime\runtime\Cpp\runtime\src\ANTLRInputStream.h:17
[build] >>>               src/CMakeFiles/Sugar.dir/main.cpp.obj:(public: virtual __cdecl antlr4::ANTLRInputStream::~ANTLRInputStream(void))

When looking into the build dir (build-win-clang-debug\src\antlr4_runtime\src\antlr4_runtime\runtime\Cpp\runtime\src) I can see that all .h and .cpp files that are needed are there, but the linker/compiler seems to not care.

I'm pretty sure that I'm doing an obvious mistake I just don't find what I'm doing wrong.

I'm currently building with Clang 17.0.3 from the current VSBuildTools.

Upvotes: 1

Views: 49

Answers (0)

Related Questions