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++.