admin管理员组文章数量:1122846
Sadly I'm very stuck at the moment. 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 note worthy 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 but I've already wasted too many hours and I just don't find what I'm doing worng... So thanks in andvance, please end my suffering.
Oh and I'm currently building with Clang 17.0.3 from the current VSBuildTools.
本文标签: Unable to get antlr413 for c to link in my projctStack Overflow
版权声明:本文标题:Unable to get antlr4.13 for c++ to link in my projct - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736306989a1933157.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论