0

I'm trying to use google's RE2 regex library, but I can't find any documentation about how to link to it. The documentation just explains how to install re2, but there isn't any CMAKE example. Currently when I'm trying to compile I get

main.cpp:(.text.ZN3re23RE29FullMatchIJEEEbRKNS_11StringPieceERKS0_DpOT[ZN3re23RE29FullMatchIJEEEbRKNS_11StringPieceERKS0_DpOT]+0x32): undefined reference to `re2::RE2::FullMatchN(re2::StringPiece const&, re2::RE2 const&, re2::RE2::Arg const* const*, int)' clang: error: linker command failed with exit code 1 (use -v to see invocation)

davdsb
  • 101
  • 1
  • 10
  • My advice is to take a look at the re2Config.cmake that is produced in the INSTALL process: [https://github.com/google/re2/blob/main/CMakeLists.txt#L165](https://github.com/google/re2/blob/main/CMakeLists.txt#L165) to figure out what targets to put in your `target_link_libraries()` I think its just `re2::re2` looking at the github briefly but I did not install so I can't test. – drescherjm Jul 27 '22 at 15:23
  • @Tsyvarev I see... Maybe I was a bit fast on the undefined reference question. When I see such error (without any CMake code) I usually just assume OP is not sure where this error comes from. I can't seem to reopen the question by myself though. – Guillaume Racicot Jul 27 '22 at 15:25
  • @davdsb I was wrong marking it as duplicate. I provided answer for that case. If you have a CMake problem, usually you have to provide a minimal CMake script that produce your error. – Guillaume Racicot Jul 27 '22 at 15:33

1 Answers1

2

They haven't documented the process to link to the library, but looking at their CMake the way to do that is quite straightforward.

Look carefully at those lines near the end of re2/CMakeLists.txt:

install(TARGETS re2 EXPORT re2Targets
        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
        INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(EXPORT re2Targets
        DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/re2 NAMESPACE re2::)

This exports the target re2 in the re2 namespaces.

This means your CMake script can be as such:

cmake_minimum_required(VERSION 3.21)
project(myproject)
add_executable(myexec myexec.main.cpp)

# Find the installed re2 library
find_package(re2 REQUIRED)

# Link to the library and header files.
target_link_libraries(myexec PRIVATE re2::re2)

If you installed the library at a custom location, remember to add that custom location in your CMake prefixes:

# inside myproject/build
cmake .. -DCMAKE_PREFIX_PATH=/path/to/custom/location/of/re2
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
  • I'm getting `Could not find a package configuration file provided by "re2"`. I guess I didn't installed it in the right directory. Where can I find the directory it's installed on? – davdsb Jul 27 '22 at 16:57
  • 1
    @davdsb: You could run `make install` again. It will print files which are installed. Among these files you could try to find the one, which path ends with `lib/cmake/re2/re2Config.cmake`. Beginning of that path is the installation directory. – Tsyvarev Jul 27 '22 at 17:17