I'm trying to create a simple application that uses antlr4 c++ runtime to parse Java source files. However, I could not compile it due to problems with conan package manager.
I'm following antlr recommendation to use conan package manager.
My CMakeLists.txt
file content is:
cmake_minimum_required(VERSION 3.15)
project(JavaParserCppRuntime)
find_package(antlr4-runtime REQUIRED)
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} antlr4-runtime)
My conanfile.txt
content is:
[requires]
antlr4-cppruntime/4.13.0
[generators]
CMakeDeps
CMakeToolchain
The root of my project is the folder conan-pkg-based, and the directory structure is on the following image. Project directory structure
I executed the following commands in terminal with success (as recommended in conan documentation):
conan profile detect
conan install . --output-folder=build --build=missing
cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
However, when I try to execute the last command cmake --build .
, I get the following error:
<...>/conan-pkg-based/src/main.cpp:17:10: fatal error: antlr4-runtime.h: No such file or directory
17 | #include "antlr4-runtime.h"
| ^~~~~~~~~~~~~~~~~~
compilation terminated.
gmake[2]: *** [CMakeFiles/JavaParserCppRuntime.dir/build.make:76: CMakeFiles/JavaParserCppRuntime.dir/src/main.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/JavaParserCppRuntime.dir/all] Error 2
gmake: *** [Makefile:91: all] Error 2
The main.cpp
file content is:
/* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
* Use of this file is governed by the BSD 3-clause license that
* can be found in the LICENSE.txt file in the project root.
*/
//
// main.cpp
// antlr4-cpp-demo
//
// Created by Mike Lischke on 13.03.16.
//
#include <iostream>
#include <fstream>
#include <string>
#include "antlr4-runtime.h"
#include "JavaLexer.h"
#include "JavaParser.h"
// using namespace antlrcpptest;
using namespace antlr4;
void parse_file(char* file_path)
{
std::ifstream file(file_path); // Open the file for reading
if (file.is_open()) { // Check if the file was successfully opened
std::string line;
while (std::getline(file, line)) { // Read the file line by line
std::cout << line << std::endl; // Print each line
}
ANTLRInputStream input(line);
JavaLexer lexer(&input);
CommonTokenStream tokens(&lexer);
tokens.fill();
for (auto token : tokens.getTokens()) {
std::cout << token->toString() << std::endl;
}
JavaParser parser(&tokens);
tree::ParseTree* tree = parser.compilationUnit();
std::cout << tree->toStringTree() << std::endl << std::endl;
file.close(); // Close the file when done reading
} else {
std::cout << "Failed to open the file." << std::endl;
}
}
int main(int argc, char* argv[])
{
parse_file(argv[0]);
parse_file(argv[1]);
return 0;
}
The error clearly says that when the cmake tries to compile the main.cpp it cannot find the "antlr4-runtime.h"
header. But that header should be provided by conan.