davdsb
davdsb

Reputation: 131

Link RE2 in cmake

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)

Upvotes: 2

Views: 1627

Answers (1)

Guillaume Racicot
Guillaume Racicot

Reputation: 41780

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

Upvotes: 5

Related Questions