xWyatt / check_rest_api

Nagios Core plugin to check output from a RESTful JSON API
GNU General Public License v3.0
11 stars 5 forks source link

Error when compiling #24

Open domino540 opened 1 year ago

domino540 commented 1 year ago

Debian 11

Hi,

I have problem with downloaded binary even with compiling source code.

Try to run compiled

root@xxx:/home/xxxxx# ./check_rest_api-1.2.0 
./check_rest_api-1.2.0: error while loading shared libraries: libjson-c.so.2: cannot open shared object file: No such file or directory
ii  libfastjson4:amd64                   0.99.9-1                       amd64        fast json library for C
ii  libjson-c-dev                        0.15-2                         amd64        JSON manipulation library - development files
ii  libjson-c5:amd64                     0.15-2                         amd64        JSON manipulation library - shared library
ii  libjsoncpp-dev:amd64                 1.9.4-4                        amd64        library for reading and writing JSON for C++ (devel files)
ii  libjsoncpp24:amd64                   1.9.4-4                        amd64        library for reading and writing JSON for C++
rc  python-jsonschema                    2.6.0-4                        all          An(other) implementation of JSON Schema (Draft 3 and 4) - Python 2.7

Compiling error

root@xxx:/home/xxxx/check_rest_api/src# make
gcc -c -o check_rest_api.o check_rest_api.c -Wall -Werror
check_rest_api.c:5:10: fatal error: json/json_tokener.h: No such file or directory
    5 | #include <json/json_tokener.h>
      |          ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [Makefile:8: check_rest_api.o] Error 1

Thank for help

xWyatt commented 1 year ago

If you have pkg-config installed can you give me the output of:

I don't have a Debian machine handy to test on at the moment.

neubi4 commented 1 year ago

Hi,

output of pkg-config --cflags --libs json-c:

# pkg-config --cflags --libs json-c
-I/usr/include/json-c -ljson-c

Issue seems that in Debian, the header files of json-c are in /usr/include/json-c. I tried to update the code like this:

diff --git a/src/check_rest_api.c b/src/check_rest_api.c
index 9c907d2..d19a363 100644
--- a/src/check_rest_api.c
+++ b/src/check_rest_api.c
@@ -2,9 +2,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <curl/curl.h>
-#include <json/json_tokener.h>
-#include <json/json_object.h>
-#include <json/json_util.h>
+#include <json-c/json_tokener.h>
+#include <json-c/json_object.h>
+#include <json-c/json_util.h>

 #include "headers/check_rest_api.h"
 #include "headers/read_input.h"
diff --git a/src/check_thresholds.c b/src/check_thresholds.c
index a23b6a8..59d28ca 100644
--- a/src/check_thresholds.c
+++ b/src/check_thresholds.c
@@ -4,9 +4,9 @@
 #include <ctype.h>

 #include <curl/curl.h>
-#include <json/json_tokener.h>
-#include <json/json_object.h>
-#include <json/json_util.h>
+#include <json-c/json_tokener.h>
+#include <json-c/json_object.h>
+#include <json-c/json_util.h>

 #include "headers/check_rest_api.h"
 #include "headers/read_input.h"

But when compiling it i get another error:

# make
gcc -c -o check_rest_api.o check_rest_api.c -Wall -Werror
gcc -c -o read_input.o read_input.c -Wall -Werror
gcc -o check_rest_api check_rest_api.o read_input.o check_thresholds.c -Wall -Werror -lcurl -ljson-c
/usr/bin/ld: read_input.o:(.bss+0x0): multiple definition of `argVals'; check_rest_api.o:(.bss+0x0): first defined here
/usr/bin/ld: /tmp/ccLzaMMq.o:(.bss+0x0): multiple definition of `argVals'; check_rest_api.o:(.bss+0x0): first defined here
collect2: error: ld returned 1 exit status
make: *** [Makefile:11: check_rest_api] Error 1

Sine i have no c knowledge, i dotn know what the problem is :(

sinewave-2097 commented 1 year ago

FWIW, I get the exact same compilation errors on Ubuntu 22.04.2 LTS using v1.2.0 or v1.2.1.

maggu commented 5 months ago

There are two issues mentioned here. Regarding the header files in the original comment:

I checked the current status on RHEL. On RHEL 9 it's the same as on Debian and Ubuntu: The header files are placed in /usr/include/json-c and the code doesn't compile. Om RHEL 7 (soon to be EoL), they are located in /usr/include/json, but then /usr/include/json-c is created as a symlink. So I believe the includes in this repository can safely (and should) be changed from json to json-c by now.

Then there's the linking error, which is because argVals is definied in read_input.h. It should instead be declared as extern struct.

These commands make the required adjustments:

sed -i 's%^#include <json/%#include <json-c/%' *.c
sed -i 's%^struct%extern struct%' headers/read_input.h

All in all, I think this issue should be tagged as a bug, and the above changes made. (I don't believe a PR is called for here, but I can make one nonetheless if preferable.)