Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cpp runtime under MSYS #4141

Open
ggabbiani opened this issue Feb 24, 2023 · 0 comments
Open

Cpp runtime under MSYS #4141

ggabbiani opened this issue Feb 24, 2023 · 0 comments

Comments

@ggabbiani
Copy link

ggabbiani commented Feb 24, 2023

Hi all,

I'm writing a C++ based project using ANTLR4 Cpp runtime, following the latest documentation.

The project successfully builds and executes under Linux and macOS but fails under Windows when trying to link the executables with the antlr4_static library receiving the following message:

[build] [13/14  92% :: 4.005] Linking CXX executable bin\orthodocs.exe
[build] FAILED: bin/orthodocs.exe 
[build] cmd.exe /C "cd . && C:\msys64\mingw64\bin\g++.exe -O3 -DNDEBUG  CMakeFiles/orthodocs.dir/src/analyzer.cpp.obj CMakeFiles/orthodocs.dir/src/main.cpp.obj -o bin\orthodocs.exe -Wl,--out-implib,lib\liborthodocs.dll.a -Wl,--major-image-version,0,--minor-image-version,0  lib/libfactories.a  lib/libCppSCAD.a  lib/libMarkdown.a  lib/libcommons.a  lib/libspdlog.a  lib/libCppSPDX.a  antlr4_runtime/src/antlr4_runtime/runtime/Cpp/dist/libantlr4-runtime.a  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find antlr4_runtime/src/antlr4_runtime/runtime/Cpp/dist/libantlr4-runtime.a: No such file or directory
[build] collect2.exe: error: ld returned 1 exit status
[build] ninja: build stopped: subcommand failed.

Environment:

Windows: Windows 10 Pro
MSYS2: MINGW64_NT-10.0-19045 version 3.4.3.x86_64 (runneradmin@fv-az269-587) (gcc version 11.3.0 (GCC) ) 2023-01-17 21:17 UTC
CMake: v3.24.1
ANTLR4 Cpp: v4.12.0

What I found so far:

It seems like a mismatch between the Cpp runtime build runtime/CMakeLists.txt and the client code build cmake/ExternalAntlr4Cpp.cmake.

CMake recognizes MINGW compiler and set the library prefix/suffix accordingly

WIN32="1"
MINGW="1"
MVSC=""
CMAKE_FIND_LIBRARY_PREFIXES="lib;"
CMAKE_FIND_LIBRARY_SUFFIXES=".dll.a;.a;.lib"

Cpp runtime build (runtime/CMakeLists.txt) tests WIN32 to set the static_lib_suffix to '-static' hence producing libantlr4-runtime-static.a.

set(static_lib_suffix "")

if (WIN32)
  set(static_lib_suffix "-static")
  ...
endif()
...
if (TARGET antlr4_static)
  set_target_properties(antlr4_static
                        PROPERTIES VERSION   ${ANTLR_VERSION}
                                   SOVERSION ${ANTLR_VERSION}
                                   OUTPUT_NAME "antlr4-runtime${static_lib_suffix}"
                                   COMPILE_PDB_NAME "antlr4-runtime${static_lib_suffix}"
                                   COMPILE_FLAGS "${disabled_compile_warnings} ${extra_static_compile_flags}")
endif()

Client code (cmake/ExternalAntlr4Cpp.cmake) evaluates MVSC for managing the static library name so searches for libantlr4-runtime.a.:

if(MSVC)
  ...
else()
  set(ANTLR4_STATIC_LIBRARIES
      ${ANTLR4_OUTPUT_DIR}/libantlr4-runtime.a)
  ...
endif()

this explains the mismatch.

What I do not understand is ... which of the two behaviors is the wrong one?

Personally I solved modifying cmake/ExternalAntlr4Cpp.cmake in this manner:

diff --git a/antlr4/cmake/ExternalAntlr4Cpp.cmake b/antlr4/cmake/ExternalAntlr4Cpp.cmake
index 54f874b..648b84a 100644
--- a/antlr4/cmake/ExternalAntlr4Cpp.cmake
+++ b/antlr4/cmake/ExternalAntlr4Cpp.cmake
@@ -27,16 +27,21 @@ else()
   set(ANTLR4_OUTPUT_DIR ${ANTLR4_ROOT}/runtime/Cpp/dist)
 endif()

+set(static_lib_suffix "")
+if (WIN32)
+  set(static_lib_suffix "-static")
+endif()
+
 if(MSVC)
   set(ANTLR4_STATIC_LIBRARIES
-      ${ANTLR4_OUTPUT_DIR}/antlr4-runtime-static.lib)
+      ${ANTLR4_OUTPUT_DIR}/antlr4-runtime${static_lib_suffix}.lib)
   set(ANTLR4_SHARED_LIBRARIES
       ${ANTLR4_OUTPUT_DIR}/antlr4-runtime.lib)
   set(ANTLR4_RUNTIME_LIBRARIES
       ${ANTLR4_OUTPUT_DIR}/antlr4-runtime.dll)
 else()
   set(ANTLR4_STATIC_LIBRARIES
-      ${ANTLR4_OUTPUT_DIR}/libantlr4-runtime.a)
+      ${ANTLR4_OUTPUT_DIR}/libantlr4-runtime${static_lib_suffix}.a)
   if(MINGW)
     set(ANTLR4_SHARED_LIBRARIES
         ${ANTLR4_OUTPUT_DIR}/libantlr4-runtime.dll.a)

In this manner it builds and executes correctly but is this the correct way to solve or am I missing something?

Many thanks in advance

Giampiero

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant