WSL2 Berkeley DB g++ undefined reference to Db::open(DbTxn*, char const*, char const*, DBTYPE, unsigned int, int)

So, I’m trying to use Berkeley DB under WSL with g++ [11.1.0], and while trying to bind a test application I’m plagued with:

$ g++ -g -std=c++20 -I /usr/local/BerkeleyDB.18.1/include -L /usr/local/BerkeleyDB.18.1/lib/ -l 
db_cxx test.cpp
/usr/bin/ld: test.o: in function `main':
/home/codefool/work/chessgen/db/test.cpp:10: undefined reference to `Db::open(DbTxn*, char const*, 
char const*, DBTYPE, unsigned int, int)'
/usr/bin/ld: test.o: in function `__static_initialization_and_destruction_0(int, int)':
/home/codefool/work/chessgen/db/test.cpp:3: undefined reference to `Db::Db(DbEnv*, unsigned int)'
/usr/bin/ld: /home/codefool/work/chessgen/db/test.cpp:3: undefined reference to `Db::~Db()'
/usr/bin/ld: test.o:(.data.rel.local.DW.ref._ZTI11DbException[DW.ref._ZTI11DbException]+0x0): 
undefined reference to `typeinfo for DbException'
collect2: error: ld returned 1 exit status

After hours of trawling the interwebs, and finding that it’s not an uncommon problem, the usual solution is that the library is not being included. Here the library is not only being correctly included, but I dumped the symbols in the object and the library to make sure the garbled names matched.  I finally got this to build by moving the .cpp file to the start of the command line:

$ g++ test.cpp -g -std=c++20 -I /usr/local/BerkeleyDB.18.1/include -L /usr/local/BerkeleyDB.18.1/lib/ 
-l db_cxx

And thus the problem was solved.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published.