Question

I'm trying to replace libgeoip by libmaxminddb. I'm using CMake build system. It fails with undefined reference when linking, but -lmaxminddb is present.

liblua_modules.a is the library containing the call to MMDB_open().

Linking CXX executable sauer_server
cd /home/piernov/suckerserv/trunk/suckerserv-v5/release_build/src && /usr/bin/cmake -E cmake_link_script CMakeFiles/sauer_server.dir/link.txt --verbose=1
/usr/bin/c++       CMakeFiles/sauer_server.dir/engine/server.cpp.o CMakeFiles/sauer_server.dir/fpsgame/server.cpp.o CMakeFiles/sauer_server.dir/hopmod/netbans.cpp.o CMakeFiles/sauer_server.dir/hopmod/startup.cpp.o CMakeFiles/sauer_server.dir/hopmod/scheduler.cpp.o CMakeFiles/sauer_server.dir/hopmod/lua.cpp.o CMakeFiles/sauer_server.dir/hopmod/core_bindings.cpp.o CMakeFiles/sauer_server.dir/hopmod/events.cpp.o CMakeFiles/sauer_server.dir/hopmod/signals.cpp.o  -o sauer_server -rdynamic -lrt -lz libsauertools.so liblua_modules.a -lboost_thread -lenet libsauertools.so -lrt -lz -llua -lmaxminddb fungu/src/net/http/libfungu_http.a -pthread fungu/src/libfungu_string.a -lboost_system -lssl -lcrypto -lenet -Wl,-rpath,/home/piernov/suckerserv/trunk/suckerserv-v5/release_build/src: 
liblua_modules.a(geoip.cpp.o): In function `load_geoip_database(lua_State*)':
geoip.cpp:(.text+0x13e): undefined reference to `MMDB_open(char const*, unsigned int, MMDB_s*)'
geoip.cpp:(.text+0x153): undefined reference to `MMDB_strerror(int)'
Was it helpful?

Solution 2

Solution found: #include <maxminddb.h> has to be inside extern "C"{} when working with C++.

OTHER TIPS

Have you considered altering the linking order? This can often help with your kind of problem.

Instead of

target_link_libraries(foo maxminddb object1 object2 object3)

move maxminddb further back so that the linker already knows what functions to look for target_link_libraries(foo object1 object2 object3 maxminddb)

note: If libmaxminddb itself needs other libraries, say liba and libb make sure to still place them behind maxminddb

target_link_libraries(foo object1 object2 object3 maxminddb liba libb)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top