To integrate the cJSON library into a CentOS 7 environment, follow these streamlined steps:
-
Install Development Tools
Ensure essential build utilities likegcc,make, and related tools are available by installing the Development Tools group: ``` sudo yum groupinstall "Development Tools" -
Obtain cJSON Source Code
Clone the official repository or download a release archive: ``` git clone https://github.com/DaveGamble/cJSON.gitIf Gits unavailable, manually download the source tarball from GitHub and extract it using `tar`. -
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 . makeThis compiles the static and shared libraries. -
Installl the Library System-Wide
Deploy headers and binaries to standard system locations: ``` sudo make installBy default, headers go to `/usr/local/include/cjson/` and libraries to `/usr/local/lib/`. -
Update Dynamic Linker Cache
Since/usr/local/libmay not be in the default library search path, register it: ``` echo '/usr/local/lib' | sudo tee /etc/ld.so.conf.d/cjson.conf sudo ldconfigThis ensures the runtime linker can locate `libcjson.so`. -
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; }
-
Compile and Link
Compile your program while linking against the cJSON library: ``` gcc -o test_app test_app.c -lcjson -
Run the Application
Execute the binary to verify functionality: ``` ./test_appA successful run outputs a JSON string, confirming correct integration.