1

What I am trying to do is to set up a minimal working example of using the regex library TRE from within a C++ project. I don't find it hard to understand how to use the library in itself (I have worked with other software that expose TRE's API) and in fact, there is a previous question here that does give an example of using TRE within C++: Fuzzy regex match using TRE.

So, I did download all files from the TRE repository, created the prep tree and installed the library, as per the instructions. Then I set up a modernized version of the code in the above link (since that question was asked, one thing that changed according to TRE's documentation is that the way to include it is now trough tre/tre.h instead of regex.h):

#include <string>
#include "tre/tre.h"

int main()
{
    regex_t rx;
    tre_regcomp(&rx, "(January|February)", REG_EXTENDED);

    regaparams_t params = { 0 };
    params.cost_ins = 1;
    params.cost_del = 1;
    params.cost_subst = 1;
    params.max_cost = 2;
    params.max_del = 2;
    params.max_ins = 2;
    params.max_subst = 2;
    params.max_err = 2;

    regamatch_t match;
    match.nmatch = 0;
    match.pmatch = 0;

    if (!tre_regaexec(&rx, "Janvary", &match, params, 0)) {
        std::printf("Levenshtein distance: %d\n", match.cost);
    } else {
        std::printf("Failed to match\n");
    }

    return 0;
}

I put the code in a file called test-tre-01.cpp, stored a in a folder called mytretest. Inside such a folder resides the tre subfolder, as the root of TRE. Therefore, I try compiling with the following (with Ubuntu terminal within mytretest):

gcc test-tre-01.cpp -I tre/include

The compiler throws me the following two errors:

/tmp/ccoaMdiA.o: In function `main':
test-tre-01.cpp:(.text+0x2b): undefined reference to `tre_regcomp'
test-tre-01.cpp:(.text+0xbb): undefined reference to `tre_regaexec'
collect2: error: ld returned 1 exit status

Since tre_regcomp and tre_regaexec are indeed defined in the included tre.h file (which resides in the tre/include subfolder), this seems clearly a compiler link issue. I am, however, a bit of at a loss on how to proceed to solve that since I did not find much more documentation on how to use TRE.

Could anyone please point me in the right direction? While I have worked with regex libraries in other languages, I am not very experienced with C++.

XZN
  • 11
  • 1
  • This appears to be a linker error. Is there a library that you need to link with that you are missing? – jkb Feb 18 '21 at 04:28

1 Answers1

0

Using Ubuntu 20.04 you can achieve what you want as shown below:

After compiling tre and the program test-tre-01.cpp successfully we created a softlink to the library libtre.so.5.0.0 in /usr/lib so the system can find it, then the program runs without issues.

mytretest$ ls
test-tre-01.cpp  tre

mytretest$ cd tre

tre$ make

tre$ make DESTDIR=/home/developer/mytretest/binlib install

tre$ cd ..

mytretest$ ls
binlib  test-tre-01.cpp  tre

mytretest$ gcc test-tre-01.cpp -I./tre/include -L./binlib/usr/local/lib -ltre

mytretest$ ls
a.out  binlib  test-tre-01.cpp  tre

mytretest$ sudo ln -s -T /home/developer/mytretest/binlib/usr/local/lib/libtre.so.5.0.0 /usr/lib/libtre.so.5

mytretest$ ./a.out
Levenshtein distance: 1
Angel
  • 13
  • 3
  • Thanks for your answer. Why is `sudo /sbin/ldconfig -v` necessary here, since I did not touch system installed libraries? Also, is the error you were getting before it the one that said that `libtre.so.5` could not be found? – XZN Feb 18 '21 at 07:24