Can't compile ANTLR with cmake - c++

I am trying to include ANTLR in my cmake project, I closely followed the tutorial:
# minimum required CMAKE version
CMAKE_MINIMUM_REQUIRED(VERSION 3.7 FATAL_ERROR)
project(ANTLR_TEST)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
# compiler must be 17
set(CMAKE_CXX_STANDARD 17)
# 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)
# Specify the version of the antlr4 library needed for this project.
# By default the latest version of antlr4 will be used. You can specify a
# specific, stable version by setting a repository tag value or a link
# to a zip file containing the libary source.
# set(ANTLR4_TAG 4.11.1)
# set(ANTLR4_ZIP_REPOSITORY https://github.com/antlr/antlr4/archive/refs/tags/4.11.1.zip)
# 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_CURRENT_SOURCE_DIR}/antlr_jar/antlr-4.11.1-complete.jar)
# add macros to generate ANTLR Cpp code from grammar
find_package(ANTLR REQUIRED)
# Call macro to add lexer and grammar to your build dependencies.
antlr_target(SampleGrammarLexer TLexer.g4 LEXER
PACKAGE antlrcpptest)
antlr_target(SampleGrammarParser TParser.g4 PARSER
PACKAGE antlrcpptest
DEPENDS_ANTLR SampleGrammarLexer
COMPILE_FLAGS -lib ${ANTLR_SampleGrammarLexer_OUTPUT_DIR})
# include generated files in project environment
include_directories(${ANTLR_SampleGrammarLexer_OUTPUT_DIR})
include_directories(${ANTLR_SampleGrammarParser_OUTPUT_DIR})
# add generated grammar to demo binary target
add_executable(demo main.cpp
${ANTLR_SampleGrammarLexer_CXX_OUTPUTS}
${ANTLR_SampleGrammarParser_CXX_OUTPUTS})
target_link_libraries(demo antlr4_static)
While building works on WSL it does not with Visual Studio:
3>Finished building the project antlr4_runtime-build_static.vcxproj.
4>------ Build started: Project: demo, Configuration: Debug x64 ------
4>Building Sample Grammar Lexer with ANTLR 4.11.1
4>Building Custom Rule D:/Q/OribosScript/OribosLang/src/CMakeLists.txt
4>Building SampleGrammarParser with ANTLR 4.11.1
4>main.cpp
4>TLexer.cpp
4>TParser.cpp
4>Generating code...
4>LINK : fatal error LNK1104: File "antlr4_runtime\src\antlr4_runtime\runtime\Cpp\dist\Debug\antlr4-runtime-static.lib" cannot be opened.
4>The creation of the project "demo.vcxproj" is complete -- ERROR.
However, src\build\antlr4_runtime\src\antlr4_runtime\runtime\Cpp\dist\antlr4-runtime-static.lib exists, so VS is apparently looking for a Debug folder that does not exist.
What do I need to change in my CMakeLists.txt to make it work on Windows, too?

Related

why i have some missing files when cmake generate build folder ? (Maya 2020 - CMake 3.16.4 - VS 2017)

I exactly followed the instruction in API help to create visual studio project:
The CMakeLists.txt File guide
but I got this error:
CMake Warning (dev) in CMakeLists.txt:
No project() command is present. The top-level CMakeLists.txt file must
contain a literal, direct call to the project() command. Add a line of
code such as
project(ProjectName)
near the top of the file, but after cmake_minimum_required().
CMake is pretending there is a "project(Project)" command on the first
line.
This warning is for project developers. Use -Wno-dev to suppress it.
by the way , this error didn't stop the process and CMake generate the build folder for me
but as you can see it didn't create some files i think , there are no helloworld.vcxproj & helloworld.vcxproj.filters
FYI : i use Cmake 3.16.4 and visual studio 2017
The tutorial is incomplete, as it is missing the project() command. Your CMake project should always have at least one project() command, as it is used to initialize some pretty essential variables, and the language used in the CMake file, among other things. From the CMake documentation:
The top-level CMakeLists.txt file for a project must contain a literal, direct call to the project() command; loading one through the include() command is not sufficient. If no such call exists, CMake will issue a warning and pretend there is a project(Project) at the top to enable the default languages (C and CXX).
Using the set() command to initialize PROJECT_NAME is bad practice, as the project() call also does this for you. I would suggest modifying the CMake file to include the project() command instead:
cmake_minimum_required(VERSION 2.8)
include($ENV{DEVKIT_LOCATION}/cmake/pluginEntry.cmake)
# Set the project here.
project(exampleNode)
set(RESOURCES_FILES myResource.xpm)
set(MEL_FILES
exampleNode.mel)
set(SOURCE_FILES
exampleNode.cpp
${MEL_FILES}
)
set(LIBRARIES
OpenMaya Foundation
)
find_package(MtoA)
find_alembic()
build_plugin()
this is the correct CMakeLists.txt :
cmake_minimum_required(VERSION 2.8)
project(test)
set(PROJECT_NAME test)
include($ENV{DEVKIT_LOCATION}/cmake/pluginEntry.cmake)
set(RESOURCES_FILES myResource.xpm)
set(MEL_FILES
test.mel)
set(SOURCE_FILES
test.cpp
${MEL_FILES}
)
set(LIBRARIES
OpenMaya Foundation
)
build_plugin()

How do I build OpenCV with TesseractOCR?

I'm trying to build OpenCV with the Tesseract OCR module to use on a raspberry pi.
There is very little information online on how to build this, and what is available is very incomplete.
I'm building using Visual Studio 2017 on windows 10 using the VisualGDB plugin for cross compilation.
I can load the OpenCV CMake project and build it without issues.
I downloaded the OpenCV-contrib modules, and pointed the OPENCV_EXTRA_MODULES_PATH to C:/opencv/opencv_contrib-master/modules
I can see there is a text module that contains some sources for tesseract-ocr.
There are also entries for Tesseract_INCLUDE_DIR and Tesseract_LIBRARY
Does this mean I need to download Tesseract separately? Or can I just use the files that came with OpenCV contrib?
I cloned https://github.com/tesseract-ocr/tesseract
But there is no include folder anywhere. There is an src folder that contains headers and source files, but no include folder anywhere.
Do I need to build tesseract first? And when its built, it creates lib and include files?
I tried to build tesseract, but it required leptonica.
I was able to build leptonica. but I cannot manage to include it in Tesseract cmake.
I added the lines
add_subdirectory(${CMAKE_SOURCE_DIR}/../leptonica)
add_subdirectory(${CMAKE_SOURCE_DIR}/../leptonica/build)
find_package(Leptonica ${MINIMUM_LEPTONICA_VERSION} REQUIRED CONFIG)
But I'm getting errors:
add_subdirectory not given a binary directory but the given source C:\opencv\tesseract\CMakeLists.txt
Could not find a package configuration file provided by "Leptonica" C:\opencv\tesseract\CMakeLists.txt 124
Thanks,
I got tesseract to build, here is what I added:
To tesseract/CMakeLists.txt on line 105 comment out and add
#if(NOT EXISTS ${PROJECT_SOURCE_DIR}/.cppan)
# if (NOT Leptonica_DIR AND NOT MSVC)
# find_package(PkgConfig REQUIRED)
# pkg_check_modules(Leptonica REQUIRED lept>=${MINIMUM_LEPTONICA_VERSION})
# link_directories(${Leptonica_LIBRARY_DIRS})
# else()
# find_package(Leptonica ${MINIMUM_LEPTONICA_VERSION} REQUIRED CONFIG)
# endif()
#else()
# if (STATIC)
# set(CPPAN_BUILD_SHARED_LIBS 0)
# else()
# set(CPPAN_BUILD_SHARED_LIBS 1)
# endif()
# add_subdirectory(.cppan)
#endif()
set(Leptonica_DIR "C:/opencv/leptonica/build")
find_package(Leptonica ${MINIMUM_LEPTONICA_VERSION} REQUIRED CONFIG)
find_package(OpenCL QUIET)
Then some build options need to be changed. Same file, line 260.
Set all the PROPERTIES COMPILE_FLAGS "-marm" otherwise it wont compile
set_source_files_properties(
${CMAKE_CURRENT_SOURCE_DIR}/src/arch/dotproductsse.cpp
PROPERTIES COMPILE_FLAGS "-msse4.1")
set_source_files_properties(
${CMAKE_CURRENT_SOURCE_DIR}/src/arch/intsimdmatrixsse.cpp
PROPERTIES COMPILE_FLAGS "-msse4.1")
set_source_files_properties(
${CMAKE_CURRENT_SOURCE_DIR}/src/arch/dotproductavx.cpp
PROPERTIES COMPILE_FLAGS "-mavx")
set_source_files_properties(
${CMAKE_CURRENT_SOURCE_DIR}/src/arch/intsimdmatrixavx2.cpp
PROPERTIES COMPILE_FLAGS "-mavx2")
Then, copy the endianness.h and config_auto.h from C:\opencv\leptonica\build\src to C:\opencv\leptonica\src , again otherwise it wont build
Then it will complain about undefined PACKAGE_VERSION in tessdatamanager.cpp, so define it in the CMakeLists or just directly in the source file.
After that it should build, if your directories match.
My directory structure is as follows:
Tesseract git repository located in C:\opencv\tesseract
Tesseract build directory C:\opencv\tesseract\build
Leptonica repository located in C:\opencv\leptonica
Leptonica build directory in C:\opencv\leptonica\build (contains LeptonicaConfig.cmake,
Leptonica library files, (libleptonica.so, libleptonica.so.1.77.0, libleptonica.so.5.3.0) are in C:\opencv\leptonica\build\src

How can i fix tbb on CMake

I have this problem.
CMake Error at CMakeLists.txt:14 (find_package): By not providing
"FindTBB.cmake" in CMAKE_MODULE_PATH this project has asked CMake to
find a package configuration file provided by "TBB", but CMake did
not find one.
I could not find a package configuration file provided by "TBB" with any of the following names:
TBBConfig.cmake
tbb-config.cmake
Add the installation prefix of "TBB" to CMAKE_PREFIX_PATH or set
"TBB_DIR" to a directory containing one of the above files. If
"TBB" provides a separate development package or SDK, be sure it has
been installed.
how can i fix this?
Here is my CMakeLists.txt
cmake_minimum_required(VERSION 3.9)
project(deneme)
set(CMAKE_CXX_STANDARD 11)
include("C:/yunus/Git/inteltbb/tbb/cmake/TBBBuild.cmake")
tbb_build(TBB_ROOT "C:/yunus/Git/inteltbb/tbb" CONFIG_DIR TBB_DIR)
find_package(TBB REQUIRED tbb)
add_executable(deneme main.cpp ToDo.cpp ToDo.h)
TBB does not come by default with a FindTBB.cmake module so the guidelines in the error message are a bit misleading.
If your project provides the corresponding FindTBB.cmake module you need to add the path to it and the path to the TBB installation to your CMake call, i.e.
cmake . -G "<your generator here>" -DTBB_DIR=<path to TBB installation> -DCMAKE_PREFIX_PATH=<path to FindTBB.cmake>
Otherwise you need to download a suitable FindTBB.cmake module e.g. https://github.com/schuhschuh/cmake-basis-modules/blob/develop/FindTBB.cmake
This one uses TBB_ROOT instead of TBB_DIR.
Edit:
Try the binary package integration of TBB first.
Comment the include(...) and the tbb_build(...) command and add
target_link_libraries(deneme ${TBB_IMPORTED_TARGETS})
to your CMakeLists.txt after the add_executable call. Then call
cmake . -G "<your generator here>" -DCMAKE_PREFIX_PATH=<path to your TBB installation>

CMake can not determine linker language for target: azurestorage error

I'm very new to C++ programming and having some trouble using CMake to add the azure-storage-cpp repository to my VS solution.
Here is the build error I am getting in VS, when I attempt to build the azure storage project.
CMake can not determine linker language for target: azurestorage
Here is my CMake entry:
ExternalProject_Add( azurestorage
PREFIX azurestorage
GIT_REPOSITORY https://github.com/Azure/azure-storage-cpp.git
GIT_TAG master
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../azurestorage
SOURCE_SUBDIR Microsoft.WindowsAzure.Storage)
I tried adding SET_TARGET_PROPERTIES(azurestorage PROPERTIES LINKER_LANGUAGE CXX) to my CMakeList.txt file but it doesn't help. I've also read on other forums that the repo needs to have a .cpp and .h file in the root directory for CMake to know which language. However since the azure-storage-cpp repo isn't mine, I don't have the ability to add such files.
I'm using VS2015 on Windows10
What am I doing wrong? Any and all help is appreciated.
I've given your example a try and the relevant error message is more to the top of CMake's output:
-- Unsupported Build Platform.
So if you want to add it, don't use ExternalProject_Add(). The library's included CMakeLists.txt is for Unix/Linux/OSX.
But it comes with an existing .vcproj for VS2015 which you can include into your project with include_external_msproject():
find_package(Git REQUIRED)
execute_process(
COMMAND "${GIT_EXECUTABLE}" clone https://github.com/Azure/azure-storage-cpp.git
)
set(NUGET_EXECUTABLE "${CMAKE_CURRENT_BINARY_DIR}/azure-storage-cpp/tools/NuGet.exe")
execute_process(
COMMAND "${NUGET_EXECUTABLE}" restore "azure-storage-cpp/Microsoft.WindowsAzure.Storage.v140.sln"
)
include_external_msproject(
azurestorage
"azure-storage-cpp/Microsoft.WindowsAzure.Storage/Microsoft.WindowsAzure.Storage.v140.vcxproj"
)

How to find the Qt5 CMake module on Windows

I'm trying to make a very basic Qt5 application using CMake on Windows.
I used the documentation of Qt5 to use CMake, and my main.cpp file just contains a main function.
My CMakeLists.txt is exactly:
cmake_minimum_required(VERSION 2.8.9)
project(testproject)
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# Find the QtWidgets library
find_package(Qt5Widgets)
# Tell CMake to create the helloworld executable
add_executable(helloworld hello.cpp)
# Use the Widgets module from Qt 5.
qt5_use_modules(helloworld Widgets)
When in MSysGit bash I enter
$ cmake -G"Visual Studio 11"
I get this output:
$ cmake -G"Visual Studio 11"
-- The C compiler identification is MSVC 17.0.60204.1
-- The CXX compiler identification is MSVC 17.0.60204.1
-- Check for working C compiler using: Visual Studio 11
-- Check for working C compiler using: Visual Studio 11 -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler using: Visual Studio 11
-- Check for working CXX compiler using: Visual Studio 11 -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
CMake Warning at CMakeLists.txt:11 (find_package):
By not providing "FindQt5Widgets.cmake" in CMAKE_MODULE_PATH this project
has asked CMake to find a package configuration file provided by
"Qt5Widgets", but CMake did not find one.
Could not find a package configuration file provided by "Qt5Widgets" with
any of the following names:
Qt5WidgetsConfig.cmake
qt5widgets-config.cmake
Add the installation prefix of "Qt5Widgets" to CMAKE_PREFIX_PATH or set
"Qt5Widgets_DIR" to a directory containing one of the above files. If
"Qt5Widgets" provides a separate development package or SDK, be sure it has
been installed.
CMake Error at CMakeLists.txt:17 (qt5_use_modules):
Unknown CMake command "qt5_use_modules".
-- Configuring incomplete, errors occurred!
Do you have any ideas?
After the lines
cmake_minimum_required(VERSION 2.8.9)
project(testproject)
add
set (CMAKE_PREFIX_PATH "C:\\Qt\\Qt5.0.1\\5.0.1\\msvc2010\\")
This solves the problem.
You should set the CMAKE_PREFIX_PATH environment variable instead or use the cmake-gui to set the path to the Qt 5 packages.
You need just add Qt path to Windows %PATH% variable. As suggested in official documentation: http://doc.qt.io/qt-4.8/install-win.html#step-3-set-the-environment-variables
Here's a technique that takes advantage of cmake's ability to read the registry to coerce a registry value into locating the matching msvc's Qt5Config.cmake.
It attempts to use the highest available Qt5 version by doing a reverse sort on the various "5.x" folder names inside (e.g. C:\Qt\).
This could be placed inside a module as well, e.g. QtLocator.cmake.
SET(QT_MISSING True)
# msvc only; mingw will need different logic
IF(MSVC)
# look for user-registry pointing to qtcreator
GET_FILENAME_COMPONENT(QT_BIN [HKEY_CURRENT_USER\\Software\\Classes\\Applications\\QtProject.QtCreator.cpp\\shell\\Open\\Command] PATH)
# get root path so we can search for 5.3, 5.4, 5.5, etc
STRING(REPLACE "/Tools" ";" QT_BIN "${QT_BIN}")
LIST(GET QT_BIN 0 QT_BIN)
FILE(GLOB QT_VERSIONS "${QT_BIN}/5.*")
LIST(SORT QT_VERSIONS)
# assume the latest version will be last alphabetically
LIST(REVERSE QT_VERSIONS)
LIST(GET QT_VERSIONS 0 QT_VERSION)
# fix any double slashes which seem to be common
STRING(REPLACE "//" "/" QT_VERSION "${QT_VERSION}")
# do some math trickery to guess folder
# - qt uses (e.g.) "msvc2012"
# - cmake uses (e.g.) "1800"
# - see also https://cmake.org/cmake/help/v3.0/variable/MSVC_VERSION.html
MATH(EXPR QT_MSVC "2000 + (${MSVC_VERSION} - 600) / 100")
# check for 64-bit os
# may need to be removed for older compilers as it wasn't always offered
IF(CMAKE_SYSTEM_PROCESSOR MATCHES 64)
SET(QT_MSVC "${QT_MSVC}_64")
ENDIF()
SET(QT_PATH "${QT_VERSION}/msvc${QT_MSVC}")
SET(QT_MISSING False)
ENDIF()
# use Qt_DIR approach so you can find Qt after cmake has been invoked
IF(NOT QT_MISSING)
MESSAGE("-- Qt found: ${QT_PATH}")
SET(Qt5_DIR "${QT_PATH}/lib/cmake/Qt5/")
SET(Qt5Test_DIR "${QT_PATH}/lib/cmake/Qt5Test")
ENDIF()
And then..
# finally, use Qt5 + COMPONENTS technique, compatible with Qt_DIR
FIND_PACKAGE(Qt5 COMPONENTS Core Gui Widgets Xml REQUIRED)
The #tresf's solution perfectly covers the whole idea. It's only one thing to add: Microsoft's versioning seems to be turning into geometric progression. The series is too short yet to confirm, so as of 2019' the following formula may be used:
# do some math trickery to guess folder
# - qt uses (e.g.) "msvc2012"
# - cmake uses (e.g.) "1800"
# - see also https://cmake.org/cmake/help/v3.0/variable/MSVC_VERSION.html
# - see also https://dev.to/yumetodo/list-of-mscver-and-mscfullver-8nd
if ((MSVC_VERSION GREATER_EQUAL "1920") AND (IS_DIRECTORY "${QT_VERSION}/msvc2019"))
set(QT_MSVC "2019")
elseif ((MSVC_VERSION GREATER_EQUAL "1910") AND (IS_DIRECTORY "${QT_VERSION}/msvc2017"))
set(QT_MSVC "2017")
elseif (MSVC_VERSION GREATER_EQUAL "1900")
set(QT_MSVC "2015")
else ()
MATH(EXPR QT_MSVC "2000 + (${MSVC_VERSION} - 500) / 100")
endif ()
One way is to open the CMakeLists.txt in Qt Creator. Qt Creator supports CMake natively and it always knows where Qt is.