You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在项目中引入mongoc源码本地编译(CMake配置,无需系统安装)

Alright, let's break down how to bundle the MongoDB C Driver (libmongoc) directly into your project so you can compile, build, and run without installing it system-wide—while keeping everything cross-platform and portable. Here's a step-by-step guide using CMake, since it's the most flexible for this kind of setup:

First, organize your project to keep dependencies clean. You can either manually clone the mongoc source or let CMake fetch it automatically:

your_project/
├── CMakeLists.txt
├── src/
│   └── main.c  # Your application code
└── # Optional: If cloning manually, add this subdirectory
    └── mongo-c-driver/

2. CMakeLists.txt Configuration

We'll cover two approaches: automatic source fetching (easiest for portability) and manual subdirectory integration. Both focus on static compilation to avoid shipping separate library files.

Option A: Auto-Fetch Mongoc with FetchContent

This method lets CMake download the exact mongoc version you specify directly into your build directory, no manual cloning needed:

cmake_minimum_required(VERSION 3.14)
project(your_project_name C)

# Enforce C99 (matches mongoc's supported standard)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)

# Enable FetchContent for dependency management
include(FetchContent)
FetchContent_Declare(
  mongo_c_driver
  GIT_REPOSITORY https://github.com/mongodb/mongo-c-driver.git
  GIT_TAG 1.25.4  # Use a stable, specific version (check GitHub for latest)
)

# Configure mongoc to build static libraries and disable unused features
set(ENABLE_SHARED_LIBS OFF CACHE BOOL "Build shared libraries" FORCE)
set(ENABLE_SSL OFF CACHE BOOL "Enable SSL support" FORCE)  # Set to ON if you need SSL
set(ENABLE_SASL OFF CACHE BOOL "Enable SASL support" FORCE)
set(ENABLE_ZLIB OFF CACHE BOOL "Enable zlib support" FORCE)
set(BUILD_EXAMPLES OFF CACHE BOOL "Skip building mongoc examples" FORCE)
set(BUILD_TESTING OFF CACHE BOOL "Skip building mongoc tests" FORCE)

# Fetch and build mongoc
FetchContent_MakeAvailable(mongo_c_driver)

# Add your application executable
add_executable(your_app src/main.c)

# Link against static mongoc and libbson libraries
target_link_libraries(your_app PRIVATE mongo::mongoc_static mongo::bson_static)

# Ensure your code can find mongoc headers
target_include_directories(your_app PRIVATE ${MONGO_C_DRIVER_INCLUDE_DIRS})

Option B: Manual Subdirectory Integration

If you've already cloned the mongoc source into your project, use this config instead:

cmake_minimum_required(VERSION 3.14)
project(your_project_name C)

set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)

# Configure mongoc build options before adding the subdirectory
set(ENABLE_SHARED_LIBS OFF CACHE BOOL "Build shared libraries" FORCE)
set(ENABLE_SSL OFF CACHE BOOL "Enable SSL support" FORCE)
set(ENABLE_SASL OFF CACHE BOOL "Enable SASL support" FORCE)
set(BUILD_EXAMPLES OFF CACHE BOOL "Skip mongoc examples" FORCE)
set(BUILD_TESTING OFF CACHE BOOL "Skip mongoc tests" FORCE)

# Add mongoc as a subproject
add_subdirectory(mongo-c-driver)

# Build your application
add_executable(your_app src/main.c)

# Link against static libraries
target_link_libraries(your_app PRIVATE mongo::mongoc_static mongo::bson_static)

3. Example Application Code (src/main.c)

Test the setup with a simple MongoDB connection snippet:

#include <mongoc/mongoc.h>
#include <stdio.h>

int main(int argc, char *argv[]) {
    mongoc_init();

    // Connect to local MongoDB instance
    const char *uri_str = "mongodb://localhost:27017";
    mongoc_uri_t *uri = mongoc_uri_new(uri_str);
    if (!uri) {
        fprintf(stderr, "Invalid MongoDB URI\n");
        return 1;
    }

    mongoc_client_t *client = mongoc_client_new_from_uri(uri);
    if (!client) {
        fprintf(stderr, "Failed to create MongoDB client\n");
        mongoc_uri_destroy(uri);
        return 1;
    }

    printf("Successfully connected to MongoDB!\n");

    // Cleanup resources
    mongoc_client_destroy(client);
    mongoc_uri_destroy(uri);
    mongoc_cleanup();

    return 0;
}

4. Compilation & Execution (Ubuntu)

Run these commands to build and test your project:

mkdir build && cd build
cmake ..
make
./your_app

5. Cross-Platform Notes

  • Windows: Use Visual Studio with CMake support or MinGW. The same config works, but if you need SSL, you'll need to point CMake to a Windows-compatible SSL library like OpenSSL.
  • macOS: No system dependencies required—CMake will handle the build natively, even without Homebrew installations.
  • Portability: By linking against static libraries, your final executable includes all necessary mongoc code, so you don't need to ship separate .so/.dll files with your app.

Key Tips

  • Always use a specific version tag with FetchContent to avoid unexpected changes from the latest master branch.
  • Disable unused features (like SSL/SASL) to reduce build time and executable size.
  • If you need SSL/SASL later, just flip the corresponding ENABLE_ flags in CMake and ensure the required libraries are available (or bundle them too, if needed).

内容的提问来源于stack exchange,提问作者mohammad-nazari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:12:44