Ubuntu下CMake引入mongocxx驱动出现未定义方法链接问题
Hey there! It’s super frustrating when a setup works smoothly on macOS but hits a wall on Ubuntu—let’s walk through the most likely fixes for your libmongocxx linking problem.
1. Double-Check Library Installation
First, make sure you’ve got the right development packages installed on Ubuntu. Unlike macOS (where brew handles dependencies cleanly), Ubuntu’s repos often require explicit installs:
sudo apt-get update && sudo apt-get install libmongocxx-dev libbsoncxx-dev
If you installed the libraries from source instead of the repo, confirm they’re placed in a path CMake can find (like /usr/local/lib and /usr/local/include).
2. Tweak Your CMakeLists.txt for Ubuntu
macOS’s brew setup auto-configures paths for CMake, but Ubuntu usually needs more explicit hints. Try updating your config with one of these reliable approaches:
Option A: Use CMake’s Built-in Find Modules
# libmongocxx requires C++11 or newer—enable this first set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Locate the mongocxx and bsoncxx libraries find_package(mongocxx REQUIRED) find_package(bsoncxx REQUIRED) # Link includes and libraries to your target include_directories(${LIBMONGOCXX_INCLUDE_DIR}) include_directories(${LIBBSONCXX_INCLUDE_DIR}) link_directories(${LIBMONGOCXX_LIBRARY_DIR}) link_directories(${LIBBSONCXX_LIBRARY_DIR}) target_link_libraries(your_target_name_here ${LIBMONGOCXX_LIBRARIES} ${LIBBSONCXX_LIBRARIES} )
Option B: Use pkg-config (More Reliable for Ubuntu)
If the above doesn’t work, let pkg-config handle path resolution for you:
# Enable pkg-config support find_package(PkgConfig REQUIRED) # Locate libraries via pkg-config pkg_check_modules(LIBMONGOCXX REQUIRED libmongocxx) pkg_check_modules(LIBBSONCXX REQUIRED libbsoncxx) # Set up includes and links include_directories(${LIBMONGOCXX_INCLUDE_DIRS}) include_directories(${LIBBSONCXX_INCLUDE_DIRS}) link_directories(${LIBMONGOCXX_LIBRARY_DIRS}) link_directories(${LIBBSONCXX_LIBRARY_DIRS}) target_link_libraries(your_target_name_here ${LIBMONGOCXX_LIBRARIES} ${LIBBSONCXX_LIBRARIES} ) # Add compiler flags from pkg-config add_definitions(${LIBMONGOCXX_CFLAGS_OTHER}) add_definitions(${LIBBSONCXX_CFLAGS_OTHER})
3. Fix Version Compatibility Gaps
Ubuntu 17.10’s default repo version of libmongocxx might be outdated compared to what you’re using on macOS. If you’re seeing undefined reference errors or mismatched header issues, compile the latest version from source:
- Install base dependencies first:
sudo apt-get install libmongoc-1.0-0 libbson-1.0-0 libmongoc-dev libbson-dev build-essential cmake
- Compile and install bsoncxx:
git clone https://github.com/mongodb/bsoncxx.git cd bsoncxx && mkdir build && cd build cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DBSONCXX_POLY_USE_STD=ON .. make && sudo make install
- Compile and install mongocxx:
git clone https://github.com/mongodb/mongocxx.git cd mongocxx && mkdir build && cd build cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DMONGOCXX_POLY_USE_STD=ON .. make && sudo make install
- Tell CMake to look in
/usr/localby adding this to your CMakeLists.txt beforefind_package:
set(CMAKE_PREFIX_PATH "/usr/local")
4. Debug Linking Errors
If you’re still stuck, run make VERBOSE=1 to get detailed linking logs. Look for:
- Missing library paths (check if
-Lflags point to where your libmongocxx is installed) - Undefined references (usually mean a mismatch between header and library versions)
- Missing
-lmongocxxor-lbsoncxxflags in the final link command
内容的提问来源于stack exchange,提问作者Jeff Labonté

