Skip to contentSkip to conversation

configure: properly check if -lrt and -lm is needed

PATCH Superseded Natanael Copa 5 participants 18 messages Jun 14, 2012 – Sep 15, 2012 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 configure: properly check if -lrt and -lm is needed

Fixes build against uClibc.

uClibc provides 2 versions of clock_gettime(), one with realtime support and one without (this is so you can avoid linking in -lrt unless actually needed). This means that the clock_gettime() don't need -lrt. We still need it for timer_create() so we check for this function in addition.

We also need check if -lm is needed for isnan().

---
 Makefile        |    4 ++--
 Makefile.target |    4 +---
 configure       |   33 +++++++++++++++++++++++++++++++--
 3 files changed, 34 insertions(+), 7 deletions(-)
Makefile +2 -2
index 32550cb..9dfa01a 100644
@@ -35,7 +35,7 @@ configure: ;
 
 $(call set-vpath, $(SRC_PATH))
 
-LIBS+=-lz $(LIBS_TOOLS)
+LIBS+=-lz $(LIBS_TOOLS) $(LIBM) $(LIBRT)
 
 HELPERS-$(CONFIG_LINUX) = qemu-bridge-helper$(EXESUF)
 
@@ -172,7 +172,7 @@ qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx
 	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@,"  GEN   $@")
 
 qapi-dir := $(BUILD_DIR)/qapi-generated
-qemu-ga$(EXESUF): LIBS = $(LIBS_QGA)
+qemu-ga$(EXESUF): LIBS = $(LIBS_QGA) $(LIBRT) $(LIBM)
 qemu-ga$(EXESUF): QEMU_CFLAGS += -I $(qapi-dir)
 
 gen-out-type = $(subst .,-,$(suffix $@))
Makefile.target +1 -3
index 2907aad..d214d2c 100644
@@ -34,9 +34,7 @@ PROGS+=$(QEMU_PROGW)
 endif
 STPFILES=
 
-ifndef CONFIG_HAIKU
-LIBS+=-lm
-endif
+LIBS+=$(LIBM) $(LIBRT)
 
 config-target.h: config-target.h-timestamp
 config-target.h-timestamp: config-target.mak
configure +31 -2
index c2366ee..f925973 100755
@@ -102,6 +102,8 @@ audio_win_int=""
 cc_i386=i386-pc-linux-gnu-gcc
 libs_qga=""
 debug_info="yes"
+libm=""
+librt=""
 
 target_list=""
 
@@ -2568,17 +2570,42 @@ fi
 
 
 ##########################################
+# Do we need libm
+cat > $TMPC << EOF
+#include <math.h>
+int main(void) { return isnan(0.0); }
+EOF
+if compile_prog "" "" ; then
+  libm=
+elif compile_prog "" "-lm" ; then
+  libm="-lm"
+else
+  echo
+  echo "Error: libm check failed"
+  echo
+  exit 1
+fi
+
+##########################################
 # Do we need librt
 cat > $TMPC <<EOF
 #include <signal.h>
 #include <time.h>
-int main(void) { return clock_gettime(CLOCK_REALTIME, NULL); }
+int main(void) {
+  timer_create(CLOCK_REALTIME, NULL, NULL);
+  return clock_gettime(CLOCK_REALTIME, NULL);
+}
 EOF
 
 if compile_prog "" "" ; then
   :
 elif compile_prog "" "-lrt" ; then
-  LIBS="-lrt $LIBS"
+  librt="-lrt"
+else
+  echo
+  echo "Error: librt check failed"
+  echo
+  exit 1
 fi
 
 if test "$darwin" != "yes" -a "$mingw32" != "yes" -a "$solaris" != yes -a \
@@ -3442,6 +3469,8 @@ echo "LIBS_TOOLS+=$libs_tools" >> $config_host_mak
 echo "EXESUF=$EXESUF" >> $config_host_mak
 echo "LIBS_QGA+=$libs_qga" >> $config_host_mak
 echo "POD2MAN=$POD2MAN" >> $config_host_mak
+echo "LIBM=$libm" >> $config_host_mak
+echo "LIBRT=$librt" >> $config_host_mak
 
 # generate list of library paths for linker script
 
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH v2 1/1 configure: properly check if -lrt and -lm is needed

Fixes build against uClibc.

uClibc provides 2 versions of clock_gettime(), one with realtime support and one without (this is so you can avoid linking in -lrt unless actually needed). This means that the clock_gettime() don't need -lrt. We still need it for timer_create() so we check for this function in addition.

We also need check if -lm is needed for isnan().

Both -lm and -lrt are needed for libs_qga.

---
The Xen people have nagged me to get this patch upstream so I have come up with a rebased v2 patch after consulting with pm215 on IRC.

Please consider include this.

Changes v1->v2:
- Check for sin() in addition to isnan()
- Add comment on why we also check for timer_create
- Use $LIBS and $libs_qga instead of $libm and $librt, based on feedback from pm215 on IRC
- Do not remove the explicit add of -lm unless Haiku. This was due to http://www.mail-archive.com/[email protected]/msg102965.html
I am not sure if this is valid, though.

 configure | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)
configure +32 -1
index edf9da4..a351f9b 100755
@@ -2624,17 +2624,48 @@ fi
 
 
 ##########################################
+# Do we need libm
+cat > $TMPC << EOF
+#include <math.h>
+int main(void) { return isnan(sin(0.0)); }
+EOF
+if compile_prog "" "" ; then
+  :
+elif compile_prog "" "-lm" ; then
+  LIBS="-lm $LIBS"
+  libs_qga="-lm $libs_qga"
+else
+  echo
+  echo "Error: libm check failed"
+  echo
+  exit 1
+fi
+
+##########################################
 # Do we need librt
+# uClibc provides 2 versions of clock_gettime(), one with realtime
+# support and one without. This means that the clock_gettime() don't
+# need -lrt. We still need it for timer_create() so we check for this
+# function in addition.
 cat > $TMPC <<EOF
 #include <signal.h>
 #include <time.h>
-int main(void) { return clock_gettime(CLOCK_REALTIME, NULL); }
+int main(void) {
+  timer_create(CLOCK_REALTIME, NULL, NULL);
+  return clock_gettime(CLOCK_REALTIME, NULL);
+}
 EOF
 
 if compile_prog "" "" ; then
   :
 elif compile_prog "" "-lrt" ; then
   LIBS="-lrt $LIBS"
+  libs_qga="-lrt $libs_qga"
+else
+  echo
+  echo "Error: librt check failed"
+  echo
+  exit 1
 fi
 
 if test "$darwin" != "yes" -a "$mingw32" != "yes" -a "$solaris" != yes -a \
Signature · 1 line
1.7.11.4
Reviewed-by on v2 1/1
#2 lore raw
Quoted 8 lines · Natanael Copa

Fixes build against uClibc.

uClibc provides 2 versions of clock_gettime(), one with realtime support and one without (this is so you can avoid linking in -lrt unless actually needed). This means that the clock_gettime() don't need -lrt. We still need it for timer_create() so we check for this function in addition.

Quoted 5 lines

We also need check if -lm is needed for isnan().

Both -lm and -lrt are needed for libs_qga.

#3 lore raw
In reply to Natanael Copa
Quoted 19 lines · Natanael Copa

Fixes build against uClibc.

uClibc provides 2 versions of clock_gettime(), one with realtime support and one without (this is so you can avoid linking in -lrt unless actually needed). This means that the clock_gettime() don't need -lrt. We still need it for timer_create() so we check for this function in addition.

We also need check if -lm is needed for isnan().

Both -lm and -lrt are needed for libs_qga.

---
The Xen people have nagged me to get this patch upstream so I have come up with a rebased v2 patch after consulting with pm215 on IRC.

Please consider include this.

Quoted 8 lines

Changes v1->v2:
- Check for sin() in addition to isnan()
- Add comment on why we also check for timer_create
- Use $LIBS and $libs_qga instead of $libm and $librt, based on feedback from pm215 on IRC
- Do not remove the explicit add of -lm unless Haiku. This was due to http://www.mail-archive.com/[email protected]/msg102965.html
I am not sure if this is valid, though.

Certainly building a linux-user target works for me even without that explicit 'LIBS+=-lm' in Makefile.target. Blue, can you remember what you had in mind when you wrote that email?

thanks
-- PMM

#4 lore raw
Quoted 29 lines · Peter Maydell
Quoted 19 lines · Natanael Copa

Fixes build against uClibc.

uClibc provides 2 versions of clock_gettime(), one with realtime support and one without (this is so you can avoid linking in -lrt unless actually needed). This means that the clock_gettime() don't need -lrt. We still need it for timer_create() so we check for this function in addition.

We also need check if -lm is needed for isnan().

Both -lm and -lrt are needed for libs_qga.

---
The Xen people have nagged me to get this patch upstream so I have come up with a rebased v2 patch after consulting with pm215 on IRC.

Please consider include this.

Quoted 8 lines

Changes v1->v2:
- Check for sin() in addition to isnan()
- Add comment on why we also check for timer_create
- Use $LIBS and $libs_qga instead of $libm and $librt, based on feedback from pm215 on IRC
- Do not remove the explicit add of -lm unless Haiku. This was due to http://www.mail-archive.com/[email protected]/msg102965.html
I am not sure if this is valid, though.

Quoted 3 lines

Certainly building a linux-user target works for me even without that explicit 'LIBS+=-lm' in Makefile.target. Blue, can you remember what you had in mind when you wrote that email?

Build failed since math library functions (sin() etc.) were not found.

Quoted 3 lines

thanks
-- PMM

#5 lore raw
Quoted 10 lines · On Mon, 20 Aug 2012 19:53:22 +0000 Blue Swirl <[email protected]> wrote:
Quoted 8 lines
Quoted 4 lines

- Do not remove the explicit add of -lm unless Haiku. This was due to
http://www.mail-archive.com/[email protected]/msg102965.html I am not sure if this is valid, though.

Certainly building a linux-user target works for me even without that explicit 'LIBS+=-lm' in Makefile.target. Blue, can you remember what you had in mind when you wrote that email?

Build failed since math library functions (sin() etc.) were not found.

The v2 and v3 patches I sent tests for sin() in addition to isnan (which normally is a macro). It might explain why it works now.

Adding a second -lm does not hurt, but omitting it when needed breaks build on uclibc for sure, so it would be nice if the v3 patch could be applied.

Thanks!

-nc

#6 lore raw
Quoted 12 lines · Natanael Copa
Quoted 9 lines · On Mon, 20 Aug 2012 19:53:22 +0000 Blue Swirl <[email protected]> wrote:
Quoted 8 lines
Quoted 4 lines

- Do not remove the explicit add of -lm unless Haiku. This was due to
http://www.mail-archive.com/[email protected]/msg102965.html I am not sure if this is valid, though.

Certainly building a linux-user target works for me even without that explicit 'LIBS+=-lm' in Makefile.target. Blue, can you remember what you had in mind when you wrote that email?

Quoted 8 lines
Quoted 1 line

Build failed since math library functions (sin() etc.) were not found.

The v2 and v3 patches I sent tests for sin() in addition to isnan (which normally is a macro). It might explain why it works now.

Adding a second -lm does not hurt, but omitting it when needed breaks build on uclibc for sure, so it would be nice if the v3 patch could be applied.

Now I get this on mingw32:
config-host.mak is out-of-date, running configure

Error: librt check failed

Quoted 4 lines

Thanks!

-nc

#7 lore raw

PATCH v4 1/1 configure: properly check if -lrt and -lm is needed

Fixes build against uClibc.

uClibc provides 2 versions of clock_gettime(), one with realtime support and one without (this is so you can avoid linking in -lrt unless actually needed). This means that the clock_gettime() don't need -lrt. We still need it for timer_create() so we check for this function in addition.

We also need check if -lm is needed for isnan().

Both -lm and -lrt are needed for libs_qga.

---
Changes v3->v4:
- Use $pthread_lib from previous pthread test
We don't need to add it to $LIBS since it should be there already

 configure | 36 ++++++++++++++++++++++++++++++++++--
 1 file changed, 34 insertions(+), 2 deletions(-)
configure +34 -2
index edf9da4..31eee91 100755
@@ -2624,17 +2624,49 @@ fi
 
 
 ##########################################
+# Do we need libm
+cat > $TMPC << EOF
+#include <math.h>
+int main(void) { return isnan(sin(0.0)); }
+EOF
+if compile_prog "" "" ; then
+  :
+elif compile_prog "" "-lm" ; then
+  LIBS="-lm $LIBS"
+  libs_qga="-lm $libs_qga"
+else
+  echo
+  echo "Error: libm check failed"
+  echo
+  exit 1
+fi
+
+##########################################
 # Do we need librt
+# uClibc provides 2 versions of clock_gettime(), one with realtime
+# support and one without. This means that the clock_gettime() don't
+# need -lrt. We still need it for timer_create() so we check for this
+# function in addition.
 cat > $TMPC <<EOF
 #include <signal.h>
 #include <time.h>
-int main(void) { return clock_gettime(CLOCK_REALTIME, NULL); }
+int main(void) {
+  timer_create(CLOCK_REALTIME, NULL, NULL);
+  return clock_gettime(CLOCK_REALTIME, NULL);
+}
 EOF
 
 if compile_prog "" "" ; then
   :
-elif compile_prog "" "-lrt" ; then
+# we need pthread for static linking. use previous pthread test result 
+elif compile_prog "" "-lrt $pthread_lib" ; then
   LIBS="-lrt $LIBS"
+  libs_qga="-lrt $libs_qga"
+else
+  echo
+  echo "Error: librt check failed"
+  echo
+  exit 1
 fi
 
 if test "$darwin" != "yes" -a "$mingw32" != "yes" -a "$solaris" != yes -a \
Signature · 1 line
1.7.12
#8 lore raw
Quoted 5 lines · On Tue, 21 Aug 2012 18:12:05 +0000 Blue Swirl <[email protected]> wrote:

Now I get this on mingw32:
config-host.mak is out-of-date, running configure

Error: librt check failed

Any news on the v4 patch, which should fix this?

Thanks!

#9 lore raw
Quoted 9 lines · Natanael Copa
Quoted 5 lines · On Tue, 21 Aug 2012 18:12:05 +0000 Blue Swirl <[email protected]> wrote:

Now I get this on mingw32:
config-host.mak is out-of-date, running configure

Error: librt check failed

Any news on the v4 patch, which should fix this?

No change:
config-host.mak is out-of-date, running configure

Error: librt check failed

Quoted 3 lines

Thanks!

#10 lore raw
Quoted 8 lines · On Tue, 28 Aug 2012 17:16:18 +0000 Blue Swirl <[email protected]> wrote:
Quoted 6 lines · Natanael Copa
Quoted 4 lines · On Tue, 21 Aug 2012 18:12:05 +0000 Blue Swirl <[email protected]> wrote:

Now I get this on mingw32:
config-host.mak is out-of-date, running configure

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Error: librt check failed

Any news on the v4 patch, which should fix this?

No change:
config-host.mak is out-of-date, running configure

Error: librt check failed

I have run out of guesses. Could I please have the last lines from your config.log?

Thanks!

-nc

#11 lore raw
Quoted 15 lines · Natanael Copa
Quoted 12 lines · On Tue, 28 Aug 2012 17:16:18 +0000 Blue Swirl <[email protected]> wrote:
Quoted 9 lines · Natanael Copa
Quoted 5 lines · On Tue, 21 Aug 2012 18:12:05 +0000 Blue Swirl <[email protected]> wrote:

Now I get this on mingw32:
config-host.mak is out-of-date, running configure

Error: librt check failed

Any news on the v4 patch, which should fix this?

Quoted 7 lines
Quoted 4 lines

No change:
config-host.mak is out-of-date, running configure

Error: librt check failed

I have run out of guesses. Could I please have the last lines from your config.log?

i586-mingw32msvc-gcc -m32 -D__USE_MINGW_ANSI_STDIO=1 -DWIN32_LEAN_AND_MEAN -DWINVER=0x501 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -Wstrict-prototypes -Wredundant-decls -Wall -Wundef -Wwrite-strings -Wmissing-prototypes -fno-strict-aliasing -fstack-protector-all -Wendif-labels -Wmissing-include-dirs -Wempty-body -Wnested-externs -Wformat-security -Wformat-y2k -Winit-self -Wignored-qualifiers -Wold-style-declaration -Wold-style-definition -Wtype-limits -o /tmp/qemu-conf-7196-16398-8534.exe /tmp/qemu-conf-21413-16398-5412.c -m32 -g
/tmp/qemu-conf-21413-16398-5412.c: In function 'main':
/tmp/qemu-conf-21413-16398-5412.c:4: warning: implicit declaration of function 'timer_create'
/tmp/qemu-conf-21413-16398-5412.c:4: warning: nested extern declaration of 'timer_create' /tmp/qemu-conf-21413-16398-5412.c:4: error: 'CLOCK_REALTIME' undeclared (first use in this function) /tmp/qemu-conf-21413-16398-5412.c:4: error: (Each undeclared identifier is reported only once /tmp/qemu-conf-21413-16398-5412.c:4: error: for each function it appears in.) /tmp/qemu-conf-21413-16398-5412.c:5: warning: implicit declaration of function 'clock_gettime'
/tmp/qemu-conf-21413-16398-5412.c:5: warning: nested extern declaration of 'clock_gettime'
i586-mingw32msvc-gcc -m32 -D__USE_MINGW_ANSI_STDIO=1 -DWIN32_LEAN_AND_MEAN -DWINVER=0x501 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -Wstrict-prototypes -Wredundant-decls -Wall -Wundef -Wwrite-strings -Wmissing-prototypes -fno-strict-aliasing -fstack-protector-all -Wendif-labels -Wmissing-include-dirs -Wempty-body -Wnested-externs -Wformat-security -Wformat-y2k -Winit-self -Wignored-qualifiers -Wold-style-declaration -Wold-style-definition -Wtype-limits -o /tmp/qemu-conf-7196-16398-8534.exe /tmp/qemu-conf-21413-16398-5412.c -m32 -g -lrt -lpthreadGC2
/tmp/qemu-conf-21413-16398-5412.c: In function 'main':
/tmp/qemu-conf-21413-16398-5412.c:4: warning: implicit declaration of function 'timer_create'
/tmp/qemu-conf-21413-16398-5412.c:4: warning: nested extern declaration of 'timer_create' /tmp/qemu-conf-21413-16398-5412.c:4: error: 'CLOCK_REALTIME' undeclared (first use in this function) /tmp/qemu-conf-21413-16398-5412.c:4: error: (Each undeclared identifier is reported only once /tmp/qemu-conf-21413-16398-5412.c:4: error: for each function it appears in.) /tmp/qemu-conf-21413-16398-5412.c:5: warning: implicit declaration of function 'clock_gettime'
/tmp/qemu-conf-21413-16398-5412.c:5: warning: nested extern declaration of 'clock_gettime'

Quoted 4 lines

Thanks!

-nc

#12 lore raw

PATCH v5 1/1 configure: properly check if -lrt and -lm is needed

Fixes build against uClibc.

uClibc provides 2 versions of clock_gettime(), one with realtime support and one without (this is so you can avoid linking in -lrt unless actually needed). This means that the clock_gettime() don't need -lrt. We still need it for timer_create() so we check for this function in addition.

We also need check if -lm is needed for isnan().

Both -lm and -lrt are needed for libs_qga.

---
Changes v4->v5:

- Do not exit with error if librt fails.
Apparently, mingw32 does not use those functions at all so we should not exit with error.

This is how it originally worked.

 configure | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
configure +29 -2
index edf9da4..c1ed856 100755
@@ -2624,17 +2624,44 @@ fi
 
 
 ##########################################
+# Do we need libm
+cat > $TMPC << EOF
+#include <math.h>
+int main(void) { return isnan(sin(0.0)); }
+EOF
+if compile_prog "" "" ; then
+  :
+elif compile_prog "" "-lm" ; then
+  LIBS="-lm $LIBS"
+  libs_qga="-lm $libs_qga"
+else
+  echo
+  echo "Error: libm check failed"
+  echo
+  exit 1
+fi
+
+##########################################
 # Do we need librt
+# uClibc provides 2 versions of clock_gettime(), one with realtime
+# support and one without. This means that the clock_gettime() don't
+# need -lrt. We still need it for timer_create() so we check for this
+# function in addition.
 cat > $TMPC <<EOF
 #include <signal.h>
 #include <time.h>
-int main(void) { return clock_gettime(CLOCK_REALTIME, NULL); }
+int main(void) {
+  timer_create(CLOCK_REALTIME, NULL, NULL);
+  return clock_gettime(CLOCK_REALTIME, NULL);
+}
 EOF
 
 if compile_prog "" "" ; then
   :
-elif compile_prog "" "-lrt" ; then
+# we need pthread for static linking. use previous pthread test result 
+elif compile_prog "" "-lrt $pthread_lib" ; then
   LIBS="-lrt $LIBS"
+  libs_qga="-lrt $libs_qga"
 fi
 
 if test "$darwin" != "yes" -a "$mingw32" != "yes" -a "$solaris" != yes -a \
Signature · 1 line
1.7.12
Applied
#13 lore raw

Thanks, applied.

Quoted 79 lines · Natanael Copa

Fixes build against uClibc.

uClibc provides 2 versions of clock_gettime(), one with realtime support and one without (this is so you can avoid linking in -lrt unless actually needed). This means that the clock_gettime() don't need -lrt. We still need it for timer_create() so we check for this function in addition.

We also need check if -lm is needed for isnan().

Both -lm and -lrt are needed for libs_qga.

---
Changes v4->v5:

- Do not exit with error if librt fails.
Apparently, mingw32 does not use those functions at all so we should not exit with error.

This is how it originally worked.

 configure | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
configure +29 -3
index edf9da4..c1ed856 100755
@@ -2624,17 +2624,44 @@ fi
 
 
 ##########################################
+# Do we need libm
+cat > $TMPC << EOF
+#include <math.h>
+int main(void) { return isnan(sin(0.0)); }
+EOF
+if compile_prog "" "" ; then
+  :
+elif compile_prog "" "-lm" ; then
+  LIBS="-lm $LIBS"
+  libs_qga="-lm $libs_qga"
+else
+  echo
+  echo "Error: libm check failed"
+  echo
+  exit 1
+fi
+
+##########################################
 # Do we need librt
+# uClibc provides 2 versions of clock_gettime(), one with realtime
+# support and one without. This means that the clock_gettime() don't
+# need -lrt. We still need it for timer_create() so we check for this
+# function in addition.
 cat > $TMPC <<EOF
 #include <signal.h>
 #include <time.h>
-int main(void) { return clock_gettime(CLOCK_REALTIME, NULL); }
+int main(void) {
+  timer_create(CLOCK_REALTIME, NULL, NULL);
+  return clock_gettime(CLOCK_REALTIME, NULL);
+}
 EOF
 
 if compile_prog "" "" ; then
   :
-elif compile_prog "" "-lrt" ; then
+# we need pthread for static linking. use previous pthread test result
+elif compile_prog "" "-lrt $pthread_lib" ; then
   LIBS="-lrt $LIBS"
+  libs_qga="-lrt $libs_qga"
 fi
 
 if test "$darwin" != "yes" -a "$mingw32" != "yes" -a "$solaris" != yes -a \
--

1.7.12

#14 lore raw
In reply to Natanael Copa
Quoted 11 lines · Natanael Copa

Fixes build against uClibc.

uClibc provides 2 versions of clock_gettime(), one with realtime support and one without (this is so you can avoid linking in -lrt unless actually needed). This means that the clock_gettime() don't need -lrt. We still need it for timer_create() so we check for this function in addition.

We also need check if -lm is needed for isnan().

Both -lm and -lrt are needed for libs_qga.

This patch breaks building the linux-user targets with --static:

cam-vm-266:precise:qemu$ ./configure --target-list=arm-linux-user --static

Error: librt check failed

The test program is failing to link with this /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/librt.a(timer_create.o):
In function `timer_create':
(.text+0x121): undefined reference to `pthread_once'

and a lot of similar errors.

(This seems to me like a glibc bug but since it's out there we rather have to deal with it I think.)

-- PMM

#15 lore raw
Quoted 20 lines · Peter Maydell
Quoted 11 lines · Natanael Copa

Fixes build against uClibc.

uClibc provides 2 versions of clock_gettime(), one with realtime support and one without (this is so you can avoid linking in -lrt unless actually needed). This means that the clock_gettime() don't need -lrt. We still need it for timer_create() so we check for this function in addition.

We also need check if -lm is needed for isnan().

Both -lm and -lrt are needed for libs_qga.

This patch breaks building the linux-user targets with --static:

cam-vm-266:precise:qemu$ ./configure --target-list=arm-linux-user --static

Error: librt check failed

Quoted 6 lines

The test program is failing to link with this /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/librt.a(timer_create.o):
In function `timer_create':
(.text+0x121): undefined reference to `pthread_once'

and a lot of similar errors.

smells missing -lpthread.

Quoted 2 lines

(This seems to me like a glibc bug but since it's out there we rather have to deal with it I think.)

Agree.

Will configure pass if you:

configure +4 -0
@@ -2661,6 +2661,10 @@ if compile_prog "" "" ; then
 elif compile_prog "" "-lrt" ; then
   LIBS="-lrt $LIBS"
   libs_qga="-lrt $libs_qga"
+# we might need -lpthread in case static linking on glibc
+elif compile_prog "" "-lrt -lpthread" ; then
+  LIBS="-lrt -lpthread $LIBS"
+  libs_qga="-lrt -lpthread $libs_qga"
 else
   echo
   echo "Error: librt check failed"

Thanks for feedback!

Signature · 1 line
Natanael Copa
#16 lore raw
Quoted 19 lines · Natanael Copa
Quoted 6 lines · Peter Maydell

The test program is failing to link with this /usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/librt.a(timer_create.o):
In function `timer_create':
(.text+0x121): undefined reference to `pthread_once'

and a lot of similar errors.

smells missing -lpthread.

Quoted 2 lines

(This seems to me like a glibc bug but since it's out there we rather have to deal with it I think.)

Agree.

Will configure pass if you:

configure if compile_prog "" "" ; then · line 2670

Quoted 10 lines
configure +4 -0
@@ -2661,6 +2661,10 @@ if compile_prog "" "" ; then
 elif compile_prog "" "-lrt" ; then
   LIBS="-lrt $LIBS"
   libs_qga="-lrt $libs_qga"
+# we might need -lpthread in case static linking on glibc
+elif compile_prog "" "-lrt -lpthread" ; then
+  LIBS="-lrt -lpthread $LIBS"
+  libs_qga="-lrt -lpthread $libs_qga"
 else
   echo
   echo "Error: librt check failed"

Yes, that will work.

-- PMM

#17 lore raw

PATCH v3 1/1 configure: properly check if -lrt and -lm is needed

Fixes build against uClibc.

uClibc provides 2 versions of clock_gettime(), one with realtime support and one without (this is so you can avoid linking in -lrt unless actually needed). This means that the clock_gettime() don't need -lrt. We still need it for timer_create() so we check for this function in addition.

We also need check if -lm is needed for isnan().

Both -lm and -lrt are needed for libs_qga.

---
Changes v2->v3:
- Check if -lpthread is needed with static -lrt

 configure | 37 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)
configure +36 -1
index edf9da4..3d7fe69 100755
@@ -2624,17 +2624,52 @@ fi
 
 
 ##########################################
+# Do we need libm
+cat > $TMPC << EOF
+#include <math.h>
+int main(void) { return isnan(sin(0.0)); }
+EOF
+if compile_prog "" "" ; then
+  :
+elif compile_prog "" "-lm" ; then
+  LIBS="-lm $LIBS"
+  libs_qga="-lm $libs_qga"
+else
+  echo
+  echo "Error: libm check failed"
+  echo
+  exit 1
+fi
+
+##########################################
 # Do we need librt
+# uClibc provides 2 versions of clock_gettime(), one with realtime
+# support and one without. This means that the clock_gettime() don't
+# need -lrt. We still need it for timer_create() so we check for this
+# function in addition.
 cat > $TMPC <<EOF
 #include <signal.h>
 #include <time.h>
-int main(void) { return clock_gettime(CLOCK_REALTIME, NULL); }
+int main(void) {
+  timer_create(CLOCK_REALTIME, NULL, NULL);
+  return clock_gettime(CLOCK_REALTIME, NULL);
+}
 EOF
 
 if compile_prog "" "" ; then
   :
 elif compile_prog "" "-lrt" ; then
   LIBS="-lrt $LIBS"
+  libs_qga="-lrt $libs_qga"
+# we might need -lpthread in case static linking
+elif compile_prog "" "-lrt -lpthread" ; then
+  LIBS="-lrt -lpthread $LIBS"
+  libs_qga="-lrt -lpthread $libs_qga"
+else
+  echo
+  echo "Error: librt check failed"
+  echo
+  exit 1
 fi
 
 if test "$darwin" != "yes" -a "$mingw32" != "yes" -a "$solaris" != yes -a \
Signature · 1 line
1.7.12