Skip to contentSkip to conversation

build: Fix link errors on some systems

PATCH In review Roi Dayan 2 participants 4 messages Jan 12, 2021 – Feb 22, 2021

Subsystems (3)
lore
Share & tools
lore

Roi Dayan

author
#0 lore raw

PATCH v4 1/1 build: Fix link errors on some systems

Since moving get_rate() and get_size() from tc to lib, on some systems we fail to link because of missing math lib. Move the functions that require math lib to their own c file and add -lm to dcb that now use those functions.

../lib/libutil.a(utils.o): In function `get_rate':
utils.c:(.text+0x10dc): undefined reference to floor' ../lib/libutil.a(utils.o): In function get_size':
utils.c:(.text+0x1394): undefined reference to floor' ../lib/libutil.a(json_print.o): In function sprint_size':
json_print.c:(.text+0x14c0): undefined reference to rint' json_print.c:(.text+0x14f4): undefined reference to rint' json_print.c:(.text+0x157c): undefined reference to `rint'

Fixes f3be0e6366ac ("lib: Move get_rate(), get_rate64() from tc here")
Fixes 44396bdfcc0a ("lib: Move get_size() from tc here")
Fixes adbe5de96662 ("lib: Move sprint_size() from tc here, add print_size()")

---

Notes:
v4
- Use SPDX license excerpt

v3
- Modify _IS_JSON_CONTEXT comparison order.

v2
- As suggested by Petr.
Instead of adding -lm to all utils move the functions that require math lib to seperate c files and add -lm to dcb that use those functions.

 dcb/Makefile          |   1 +
 include/json_print.h  |   3 ++
 lib/Makefile          |   4 +-
 lib/json_print.c      |  33 ------------
 lib/json_print_math.c |  37 +++++++++++++
 lib/utils.c           | 114 ---------------------------------------
 lib/utils_math.c      | 123 ++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 166 insertions(+), 149 deletions(-)
 create mode 100644 lib/json_print_math.c
 create mode 100644 lib/utils_math.c
dcb/Makefile +1 -0
index 4add954b4bba..7c09bb4f2e00 100644
@@ -7,6 +7,7 @@ ifeq ($(HAVE_MNL),y)
 
 DCBOBJ = dcb.o dcb_buffer.o dcb_ets.o dcb_maxrate.o dcb_pfc.o
 TARGETS += dcb
+LDLIBS += -lm
 
 endif
 
include/json_print.h +3 -0
index 1a1ad5ffa552..6fcf9fd910ec 100644
@@ -15,6 +15,9 @@
 #include "json_writer.h"
 #include "color.h"
 
+#define _IS_JSON_CONTEXT(type) (is_json_context() && (type & PRINT_JSON || type & PRINT_ANY))
+#define _IS_FP_CONTEXT(type)   (!is_json_context() && (type & PRINT_FP || type & PRINT_ANY))
+
 json_writer_t *get_json_writer(void);
 
 /*
lib/Makefile +2 -2
index 764c9137d0ec..6c98f9a61fdb 100644
@@ -3,8 +3,8 @@ include ../config.mk
 
 CFLAGS += -fPIC
 
-UTILOBJ = utils.o rt_names.o ll_map.o ll_types.o ll_proto.o ll_addr.o \
-	inet_proto.o namespace.o json_writer.o json_print.o \
+UTILOBJ = utils.o utils_math.o rt_names.o ll_map.o ll_types.o ll_proto.o ll_addr.o \
+	inet_proto.o namespace.o json_writer.o json_print.o json_print_math.o \
 	names.o color.o bpf_legacy.o bpf_glue.o exec.o fs.o cg_map.o
 
 ifeq ($(HAVE_ELF),y)
lib/json_print.c +0 -33
index b086123ad1f4..994a2f8d6ae0 100644
@@ -11,16 +11,12 @@
 
 #include <stdarg.h>
 #include <stdio.h>
-#include <math.h>
 
 #include "utils.h"
 #include "json_print.h"
 
 static json_writer_t *_jw;
 
-#define _IS_JSON_CONTEXT(type) ((type & PRINT_JSON || type & PRINT_ANY) && _jw)
-#define _IS_FP_CONTEXT(type) (!_jw && (type & PRINT_FP || type & PRINT_ANY))
-
 static void __new_json_obj(int json, bool have_array)
 {
 	if (json) {
@@ -342,32 +338,3 @@ int print_color_rate(bool use_iec, enum output_type type, enum color_attr color,
 	free(buf);
 	return rc;
 }
-
-char *sprint_size(__u32 sz, char *buf)
-{
-	long kilo = 1024;
-	long mega = kilo * kilo;
-	size_t len = SPRINT_BSIZE - 1;
-	double tmp = sz;
-
-	if (sz >= mega && fabs(mega * rint(tmp / mega) - sz) < 1024)
-		snprintf(buf, len, "%gMb", rint(tmp / mega));
-	else if (sz >= kilo && fabs(kilo * rint(tmp / kilo) - sz) < 16)
-		snprintf(buf, len, "%gKb", rint(tmp / kilo));
-	else
-		snprintf(buf, len, "%ub", sz);
-
-	return buf;
-}
-
-int print_color_size(enum output_type type, enum color_attr color,
-		     const char *key, const char *fmt, __u32 sz)
-{
-	SPRINT_BUF(buf);
-
-	if (_IS_JSON_CONTEXT(type))
-		return print_color_uint(type, color, key, "%u", sz);
-
-	sprint_size(sz, buf);
-	return print_color_string(type, color, key, fmt, buf);
-}
lib/json_print_math.c new file +37 -0
new file mode 100644
index 000000000000..f4d504995924
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0+
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <math.h>
+
+#include "utils.h"
+#include "json_print.h"
+
+char *sprint_size(__u32 sz, char *buf)
+{
+	long kilo = 1024;
+	long mega = kilo * kilo;
+	size_t len = SPRINT_BSIZE - 1;
+	double tmp = sz;
+
+	if (sz >= mega && fabs(mega * rint(tmp / mega) - sz) < 1024)
+		snprintf(buf, len, "%gMb", rint(tmp / mega));
+	else if (sz >= kilo && fabs(kilo * rint(tmp / kilo) - sz) < 16)
+		snprintf(buf, len, "%gKb", rint(tmp / kilo));
+	else
+		snprintf(buf, len, "%ub", sz);
+
+	return buf;
+}
+
+int print_color_size(enum output_type type, enum color_attr color,
+		     const char *key, const char *fmt, __u32 sz)
+{
+	SPRINT_BUF(buf);
+
+	if (_IS_JSON_CONTEXT(type))
+		return print_color_uint(type, color, key, "%u", sz);
+
+	sprint_size(sz, buf);
+	return print_color_string(type, color, key, fmt, buf);
+}
lib/utils.c +0 -114

120 lines not on this page · raw message

lib/utils_math.c new file +123 -0

123 lines not on this page · raw message

Signature · 1 line
2.26.2
Reviewed-by Tested-by on v4 1/1
#1 lore raw
Quoted 14 lines · Roi Dayan

Since moving get_rate() and get_size() from tc to lib, on some systems we fail to link because of missing math lib. Move the functions that require math lib to their own c file and add -lm to dcb that now use those functions.

../lib/libutil.a(utils.o): In function `get_rate':
utils.c:(.text+0x10dc): undefined reference to floor' ../lib/libutil.a(utils.o): In function get_size':
utils.c:(.text+0x1394): undefined reference to floor' ../lib/libutil.a(json_print.o): In function sprint_size':
json_print.c:(.text+0x14c0): undefined reference to rint' json_print.c:(.text+0x14f4): undefined reference to rint' json_print.c:(.text+0x157c): undefined reference to `rint'

Quoted 5 lines
Fixes f3be0e6366ac ("lib: Move get_rate(), get_rate64() from tc here")
Fixes 44396bdfcc0a ("lib: Move get_size() from tc here")
Fixes adbe5de96662 ("lib: Move sprint_size() from tc here, add print_size()")

Looking good:

    $ ldd ip/ip | grep libm.so
    $ ldd dcb/dcb | grep libm.so
            libm.so.6 => /lib64/libm.so.6 (0x00007f204d0c2000)
author
#2 lore raw
Quoted 23 lines · Petr Machata
Quoted 14 lines · Roi Dayan

Since moving get_rate() and get_size() from tc to lib, on some systems we fail to link because of missing math lib. Move the functions that require math lib to their own c file and add -lm to dcb that now use those functions.

../lib/libutil.a(utils.o): In function `get_rate':
utils.c:(.text+0x10dc): undefined reference to floor' ../lib/libutil.a(utils.o): In function get_size':
utils.c:(.text+0x1394): undefined reference to floor' ../lib/libutil.a(json_print.o): In function sprint_size':
json_print.c:(.text+0x14c0): undefined reference to rint' json_print.c:(.text+0x14f4): undefined reference to rint' json_print.c:(.text+0x157c): undefined reference to `rint'

Quoted 5 lines
Fixes f3be0e6366ac ("lib: Move get_rate(), get_rate64() from tc here")
Fixes 44396bdfcc0a ("lib: Move get_size() from tc here")
Fixes adbe5de96662 ("lib: Move sprint_size() from tc here, add print_size()")
Quoted 9 lines

Looking good:

     $ ldd ip/ip | grep libm.so
     $ ldd dcb/dcb | grep libm.so
             libm.so.6 => /lib64/libm.so.6 (0x00007f204d0c2000)

Hi,

Pinging about this commit. I noticed it was not taken. anything you want me to update? push again?

Thanks,
Roi

author
#3 lore raw
Quoted 36 lines · Roi Dayan
Quoted 23 lines · Petr Machata
Quoted 14 lines · Roi Dayan

Since moving get_rate() and get_size() from tc to lib, on some systems we fail to link because of missing math lib. Move the functions that require math lib to their own c file and add -lm to dcb that now use those functions.

../lib/libutil.a(utils.o): In function `get_rate':
utils.c:(.text+0x10dc): undefined reference to floor' ../lib/libutil.a(utils.o): In function get_size':
utils.c:(.text+0x1394): undefined reference to floor' ../lib/libutil.a(json_print.o): In function sprint_size':
json_print.c:(.text+0x14c0): undefined reference to rint' json_print.c:(.text+0x14f4): undefined reference to rint' json_print.c:(.text+0x157c): undefined reference to `rint'

Quoted 5 lines
Fixes f3be0e6366ac ("lib: Move get_rate(), get_rate64() from tc here")
Fixes 44396bdfcc0a ("lib: Move get_size() from tc here")
Fixes adbe5de96662 ("lib: Move sprint_size() from tc here, add print_size()")
Quoted 9 lines

Looking good:

     $ ldd ip/ip | grep libm.so
     $ ldd dcb/dcb | grep libm.so
             libm.so.6 => /lib64/libm.so.6 (0x00007f204d0c2000)
Quoted 7 lines

Hi,

Pinging about this commit. I noticed it was not taken. anything you want me to update? push again?

Thanks,
Roi

sorry, my mistake. wrong git clone :)