Integrating cJSON into CentOS 7 Projects

To integrate the cJSON library into a CentOS 7 environment, follow these streamlined steps:

  1. Install Development Tools
    Ensure essential build utilities like gcc, make, and related tools are available by installing the Development Tools group: ``` sudo yum groupinstall "Development Tools"

  2. Obtain cJSON Source Code
    Clone the official repository or download a release archive: ``` git clone https://github.com/DaveGamble/cJSON.git

    
    If Gits unavailable, manually download the source tarball from GitHub and extract it using `tar`.
    
  3. Build the Library
    Navigate into the source directory and use CMake to generate build files. Install CMake first if needed: ``` cd cJSON sudo yum install -y cmake cmake . make

    
    This compiles the static and shared libraries.
    
  4. Installl the Library System-Wide
    Deploy headers and binaries to standard system locations: ``` sudo make install

    
    By default, headers go to `/usr/local/include/cjson/` and libraries to `/usr/local/lib/`.
    
  5. Update Dynamic Linker Cache
    Since /usr/local/lib may not be in the default library search path, register it: ``` echo '/usr/local/lib' | sudo tee /etc/ld.so.conf.d/cjson.conf sudo ldconfig

    
    This ensures the runtime linker can locate `libcjson.so`.
    
  6. Write a Test Application
    Create a C file that includes the cJSON header: ``` #include <cjson/cJSON.h> #include <stdio.h>

    int main() { cJSON *root = cJSON_CreateObject(); cJSON_AddStringToObject(root, "message", "cJSON integrated successfully"); char *rendered = cJSON_Print(root); printf("%s\n", rendered); cJSON_Delete(root); free(rendered); return 0; }

  7. Compile and Link
    Compile your program while linking against the cJSON library: ``` gcc -o test_app test_app.c -lcjson

  8. Run the Application
    Execute the binary to verify functionality: ``` ./test_app

    
    A successful run outputs a JSON string, confirming correct integration.
    

Tags: cJSON CentOS 7 CMake GCC JSON Parsing

Posted on Wed, 21 Oct 2026 16:20:15 +0000 by Lautarox