Problem with AC_SEARCH_LIBS macro in configure.ac file - gnu

I'm trying to setup a few --with arguments in my configure script. However, I'm experiencing a weird behavior with following code:
AC_MSG_CHECKING([FFTW library directory])
AC_ARG_WITH(fftw-lib, [AC_HELP_STRING([--with-fftw-lib], [path to FFTW directory containing library file])], withfftwlib=$withval, withfftwlib="")
if test "$withfftwlib" != ""; then
if test "$fourlib" != "fftw"; then AC_MSG_ERROR([Only choose one Fourier transform library!])
fi
AC_MSG_RESULT([$withfftwlib])
if [[ -d "$withfftwlib" ]]; then
AC_SEARCH_LIBS([fftw_plan_dft], [fftw3], [AC_SUBST(FFTW_LIB, "-L${withfftwlib} -lfftw3")], [AC_MSG_ERROR([libfftw3 not found])])
AC_SEARCH_LIBS([fftwf_execute_r2r], [fftw3f], [AC_SUBST(FFTWF_LIB, "-lfftw3f")], [AC_MSG_ERROR([libfftw3f not found])])
fi
else
AC_MSG_RESULT([none])
if test "$fftwval" = "yes"; then AC_MSG_ERROR([location to FFTW library directory was not given!])
fi
fi
AC_MSG_CHECKING([CUDA directory])
AC_ARG_WITH(cuda, [AC_HELP_STRING([--with-cuda], [path to location of CUDA library])], withcuda=$withval, withcuda="")
if test "withcuda" != "yes" && test "$withcuda" != "" && test "$withcuda" != "no"; then
if test "$fourlib" != "cuda"; then AC_MSG_ERROR([Only choose one Fourier transform library!])
fi
AC_MSG_RESULT([$withcuda])
if [[ -d "$withcuda/samples/common/inc" ]]; then
AC_SUBST(CUDA_INC, "-I$withcuda/samples/common/inc")
CUDA_LIB="-lcuda -lcufft"
else
AC_MSG_ERROR([cuda library was not found in given location!])
fi
if test "$cublasval" = "yes"; then
CUDA_LIB+=" -lcublas"
fi
AC_SUBST(CUDA_LIB, "$CUDA_LIB")
else
AC_MSG_RESULT([none])
if test "$cudaval" = "yes"; then AC_MSG_ERROR([location to CUDA was not given!])
fi
fi
Here, users are expected to use either --with-fftw-lib or --with-cuda flag. The problem is, somehow, those two AC_SEARCH_LIBS macros cause unexpected output when using --with-cuda flag.
This is what it should be when I use --with-cuda (I removed the two AC_SEARCH_LIBS macros).
...
checking whether the C++ compiler works... yes
checking for C++ compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C++ compiler... yes
checking whether mpiCC accepts -g... yes
checking whether we are using the GNU Fortran compiler... yes
checking whether mpif90 accepts -g... yes
checking for gcc... mpicc
checking whether we are using the GNU C compiler... yes
checking whether mpicc accepts -g... yes
checking for mpicc option to accept ISO C89... none needed
checking whether make sets $(MAKE)... (cached) yes
checking how to run the C preprocessor... mpicc -E
checking for ar... ar
checking the archiver (ar) interface... ar
checking for ranlib... ranlib
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for stdlib.h... (cached) yes
checking for string.h... (cached) yes
checking for stdbool.h that conforms to C99... yes
checking for _Bool... yes
checking for inline... inline
checking for size_t... yes
checking for stdlib.h... (cached) yes
...
verses what I get (when I keep the macros)
...
checking whether we are using the GNU C++ compiler... no
checking whether mpiCC accepts -g... no
checking whether we are using the GNU Fortran compiler... no
checking whether mpif90 accepts -g... no
checking for gcc... mpicc
checking whether we are using the GNU C compiler... no
checking whether mpicc accepts -g... no
checking for mpicc option to accept ISO C89... unsupported
checking whether make sets $(MAKE)... (cached) yes
checking how to run the C preprocessor... mpicc -E
checking for ar... ar
checking the archiver (ar) interface... ar
checking for ranlib... ranlib
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for ANSI C header files... no
checking for sys/types.h... no
checking for sys/stat.h... no
checking for stdlib.h... no
checking for string.h... no
checking for memory.h... no
checking for strings.h... no
checking for inttypes.h... no
checking for stdint.h... no
checking for unistd.h... no
checking for stdlib.h... (cached) no
checking for string.h... (cached) no
checking for stdbool.h that conforms to C99... no
checking for _Bool... no
checking for inline... no
checking for size_t... no
checking for stdlib.h... (cached) no
...
I removed those identical parts where I do other checks. Notice how it does less checks, ex) no checking whether the C++ compiler works, and it also affects other checks that are not depends on either of the --with flags.
The other weird part is that it works just fine when I use --with-fftw-lib instead of --with-cuda.
I have tried remove small portions of codes to see what segment of the codes causes it, and it seems the two AC_SEARCH_LIBS macros are doing it.
Is it possible that an unused macro can have effects on the configure script? If then, what should I do in this case? If not, what could be the possible cause of it?

Related

How to build MFOC from sources on Kali Linux?

I have to configure and make GitHub | MFOC, but when I run ./configure it says:
# ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking whether gcc understands -c and -o together... yes
checking whether make supports the include directive... yes (GNU style)
checking dependency style of gcc... gcc3
checking whether make supports nested variables... (cached) yes
#Here the error
./configure: line 3930: syntax error near unexpected token `libnfc,'
./configure: line 3930: `PKG_CHECK_MODULES(libnfc, libnfc >= $LIBNFC_REQUIRED_VERSION, , AC_MSG_ERROR([libnfc >= $LIBNFC_REQUIRED_VERSION is mandatory.]))'
I have thought at 3 possible cause for the error:
The error occurrs becouse there are differences between the path or name requested.
In fact, synaptic says that libnfc in my Kali Linux is:
library: libnfc5, 1.7.1-b4
binaries: libnfc-bin, 1.7.1-b4
So maybe the problem is that 5 ahead libnfc
The path expected is different
Libnfc 1.7.1 is not supported, as last update (line 16 of ChangeLog) is for 1.7.0.
Can you help me please?
I report the help for configure, maybe it is interesting
# ./configure -help
`configure' configures mfoc 0.10.7 to adapt to many kinds of systems.
Usage: ./configure [OPTION]... [VAR=VALUE]...
To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE. See below for descriptions of some of the useful variables.
Defaults for the options are specified in brackets.
Configuration:
-h, --help display this help and exit
--help=short display options specific to this package
--help=recursive display the short help of all the included packages
-V, --version display version information and exit
-q, --quiet, --silent do not print `checking ...' messages
--cache-file=FILE cache test results in FILE [disabled]
-C, --config-cache alias for `--cache-file=config.cache'
-n, --no-create do not create output files
--srcdir=DIR find the sources in DIR [configure dir or `..']
Installation directories:
--prefix=PREFIX install architecture-independent files in PREFIX
[/usr/local]
--exec-prefix=EPREFIX install architecture-dependent files in EPREFIX
[PREFIX]
By default, `make install' will install all the files in
`/usr/local/bin', `/usr/local/lib' etc. You can specify
an installation prefix other than `/usr/local' using `--prefix',
for instance `--prefix=$HOME'.
For better control, use the options below.
Fine tuning of the installation directories:
--bindir=DIR user executables [EPREFIX/bin]
--sbindir=DIR system admin executables [EPREFIX/sbin]
--libexecdir=DIR program executables [EPREFIX/libexec]
--sysconfdir=DIR read-only single-machine data [PREFIX/etc]
--sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com]
--localstatedir=DIR modifiable single-machine data [PREFIX/var]
--runstatedir=DIR modifiable per-process data [LOCALSTATEDIR/run]
--libdir=DIR object code libraries [EPREFIX/lib]
--includedir=DIR C header files [PREFIX/include]
--oldincludedir=DIR C header files for non-gcc [/usr/include]
--datarootdir=DIR read-only arch.-independent data root [PREFIX/share]
--datadir=DIR read-only architecture-independent data [DATAROOTDIR]
--infodir=DIR info documentation [DATAROOTDIR/info]
--localedir=DIR locale-dependent data [DATAROOTDIR/locale]
--mandir=DIR man documentation [DATAROOTDIR/man]
--docdir=DIR documentation root [DATAROOTDIR/doc/mfoc]
--htmldir=DIR html documentation [DOCDIR]
--dvidir=DIR dvi documentation [DOCDIR]
--pdfdir=DIR pdf documentation [DOCDIR]
--psdir=DIR ps documentation [DOCDIR]
Program names:
--program-prefix=PREFIX prepend PREFIX to installed program names
--program-suffix=SUFFIX append SUFFIX to installed program names
--program-transform-name=PROGRAM run sed PROGRAM on installed program names
Optional Features:
--disable-option-checking ignore unrecognized --enable/--with options
--disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no)
--enable-FEATURE[=ARG] include FEATURE [ARG=yes]
--enable-silent-rules less verbose build output (undo: "make V=1")
--disable-silent-rules verbose build output (undo: "make V=0")
--enable-dependency-tracking
do not reject slow dependency extractors
--disable-dependency-tracking
speeds up one-time build
Some influential environment variables:
CC C compiler command
CFLAGS C compiler flags
LDFLAGS linker flags, e.g. -L<lib dir> if you have libraries in a
nonstandard directory <lib dir>
LIBS libraries to pass to the linker, e.g. -l<library>
CPPFLAGS (Objective) C/C++ preprocessor flags, e.g. -I<include dir> if
you have headers in a nonstandard directory <include dir>
CPP C preprocessor
Use these variables to override the choices made by `configure' or to help
it to find libraries and programs with nonstandard names/locations.
The error message does not mean that there would be something wrong with libnfc on your system. There is something wrong in the shell script configure.
The configure script did not perform a check for libnfc. The error message is about a syntax error in the shell script at a line that was intended to do this check.
./configure: line 3930: syntax error near unexpected token `libnfc,'
./configure: line 3930: `PKG_CHECK_MODULES(libnfc, libnfc >= $LIBNFC_REQUIRED_VERSION, , AC_MSG_ERROR([libnfc >= $LIBNFC_REQUIRED_VERSION is mandatory.]))'
This doesn't exactly match the line numbers in the script you show on pastebin.
Here the error is at line 3963.
Script snippet starting at line 3949
...
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $am_cv_make_support_nested_variables" >&5
$as_echo "$am_cv_make_support_nested_variables" >&6; }
if test $am_cv_make_support_nested_variables = yes; then
AM_V='$(V)'
AM_DEFAULT_V='$(AM_DEFAULT_VERBOSITY)'
else
AM_V=$AM_DEFAULT_VERBOSITY
AM_DEFAULT_V=$AM_DEFAULT_VERBOSITY
fi
AM_BACKSLASH='\'
# Checks for pkg-config modules.
LIBNFC_REQUIRED_VERSION=1.7.0
PKG_CHECK_MODULES(libnfc, libnfc >= $LIBNFC_REQUIRED_VERSION, , AC_MSG_ERROR([libnfc >= $LIBNFC_REQUIRED_VERSION is mandatory.]))
PKG_CONFIG_REQUIRES="libnfc"
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for inline" >&5
$as_echo_n "checking for inline... " >&6; }
if ${ac_cv_c_inline+:} false; then :
$as_echo_n "(cached) " >&6
else
ac_cv_c_inline=no
for ac_kw in inline __inline__ __inline; do
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
...
Before the comment line # Checks for pkg-config modules. it looks like a normal configure script.
The line PKG_CHECK_MODULES(libnfc, libnfc >= $LIBNFC_REQUIRED_VERSION, , AC_MSG_ERROR([libnfc >= $LIBNFC_REQUIRED_VERSION is mandatory.])) is wrong. This is autotools M5 macro code, not shell script.
From { $as_echo "$as_me:${as_lineno-$LINENO}: checking for inline" >&5 it looks normal again.
The configure script is generated from configure.in or configure.ac. This source is a M5 macro script using special autotools macros. All text that is not a known macro is copied to the output unchanged.
The problematic line PKG_CHECK_MODULES(libnfc, libnfc >= $LIBNFC_REQUIRED_VERSION, , AC_MSG_ERROR([libnfc >= $LIBNFC_REQUIRED_VERSION is mandatory.])) is a M5 macro call PKG_CHECK_MODULES() with several parameters whereof the last one is another macro call AC_MSG_ERROR() with a quoted argument.
I don't know why this ended up in the configure script, but this is obviously wrong. Maybe there is a bug in the configure.ac script, maybe a wrong version of the autotools was used to generate the configure script.
Thank you #Bodo, I was simply cloning the wrong branch. I know I'm a great noob, but slowly I'm learning. Thank you very much again.

gcc arguments: -pthread. What does it do?

I'm getting started with multi-thread programming using gcc under Debian 8. I've successfully written and run a multi-threaded test app (foobar.c), but I'm confused by the Makefile (copied from an example). In particular, the command that works is
gcc foobar.c -o foobar -pthread
I'm confused by "-pthread". is that
(a) an option "-p" with value "thread", or
(b) an argument "-pthread" ?
if either case, what is it actually doing? Including some library? Including some object? Setting some other option?
BTW - A similar question 15929739 was asked but never answered. Question 20924412 was not helpful either.
From the man page:
-pthread
Adds support for multithreading with the pthreads library. This option sets flags for both the preprocessor and linker.
To be specific, as of GCC 6.2.1, -pthread will:
#define _REENTRANT 1
Add -lpthread to link against libpthread
How do I know this, you ask?
What preprocessor flags are added?
Let's dump the preprocessor defines and compare them:
$ diff <(gcc -dM -E - < /dev/null) <(gcc -pthread -dM -E - < /dev/null)
> #define _REENTRANT 1
What linker options are added?
Let's dump the ld options passed by GCC and compare them:
diff <(gcc -### -o foo empty.c 2>&1) <(gcc -### -pthread -o foo empty.c 2>&1)
The output here is a bit more verbose, but if we ignore the temporary filename differences, we find:
-lpthread
"-plugin-opt=-pass-through=-lpthread"

libtool install preserves temporary rpath of executable

I am modifying a project that is very similar to examples provided by the Automake/libtool documentation. Excerpts:
Top-leve configure.ac:
LT_INIT
Top-level Makefile.am:
ACLOCAL_AMFLAGS = -I m4
SUBDIRS = src doc
./src Makefile.am:
lib_LTLIBRARIES = libname.la
libname_la_SOURCES = <my cc file list>
libname_la_LDFLAGS = -no-undefined -version-info $(GENERIC_LIBRARY_VERSION)
include_HEADERS = <my h file list>
bin_PROGRAMS = progname
progname_SOURCES = <my cc file list>
progname_LDADD = libname.la
progname_LDFLAGS = -static
In a fakeroot environment provided by my package-creation software, I execute the following commands
$ autogen.sh # contains the usual calls to aclocal, libtoolize, automake, autoconf.
$ ./configure --prefix="/usr" --disable-static
$ make
...
/bin/sh ../libtool --tag=CXX --mode=link g++ -Wall -g -O2 -static -o progname progname.o libname.la <-lLIBRARY_NAME list>
libtool: link: g++ -Wall -g -O2 -o progname progname.o ./.libs/libname.so <-lLIBRARY_NAME list> -Wl,-rpath -Wl,<build_dir>/src/.libs
...
$ objdump -x src/progname | grep -i rpath
RPATH <build_dir>/src/.libs
$ make install
$ objdump -x <fakeroot_dir>/usr/bin/progname | grep -i rpath
RPATH <build_dir>/src/.libs
In all three *.la files, libdir='/usr/lib':
/src/libname.la
/src/.libs/libname.la
/usr/lib/libname.la
I understand that RPATH is set for /src/progname to allow execution directly after make. However I was under the impression that during the install rule, libtool would remove this temporary RPATH and replace it with libdir ("/usr/lib" as specified above to configure). Furthermore, modern libtool releases would actually remove RPATH if libdir was present in the system's ld.so search path.
Why is this not happening? As it stands, the temporary RPATH directory is a security risk, allowing anyone to load a malicious libname.so from /src/.libs.
The Fedora RPath Packaging Draft contains some quit useful suggestions to remove RPATH, however I would prefer answers that work within the Autotools framework.
I think what's happening here is that libtool is getting confused by your usage of -static — what you want is what usually happens by default with libtool, and that is to trigger relinking of the binaries so that it drops the DT_RPATH definition.
But since you're telling the tool that you want a full static build, it expects the relinking to be unnecessary, and thus does not perform it.
On the other hand I'm surprised that libtool does not error out when you use -static and --disable-static.
Tell configure.ac to patch the generated $(top_srcdir)/libtool so that -Wl,-rpath -Wl,/... get kicked out
TL;DR - Append the following (β) to configure.ac:
AC_MSG_RESULT([configure: removing rpath from libtool...])
sed -i.old s/'^hardcode_libdir_flag_spec.*$'/'hardcode_libdir_flag_spec="-D__LIBTOOL_IS_A_FOOL__"'/g libtool
diff -u0 libtool.old libtool
If you use LT_OUTPUT (please stop doing that), β should be after LT_OUTPUT
Otherwise β should be after AC_OUTPUT

Difference between -shared and -Wl,-shared of the GCC options

I know -Wl,-shared is a option of ld. I've seen some person compile like this,
$ gcc -shared -Wl,-soname,libtest.so -o libtest.so *.o
And some person like this
$ gcc -Wl,-shared -Wl,-soname,libtest.so -o libtest.so *.o
So, I want to know if there is some difference between -shared and -Wl,-shared.
Thanks.
There is a difference between passing -shared to gcc or -shared to ld (via -Wl). Passing -shared to GCC may enable or disable other flags at link time. In particular, different crt* files might be involved.
To get more information, grep for -shared in GCC's gcc/config/ directory and subdirectories.
Edit: To give a specific example: on i386 FreeBSD, gcc -shared will link in object file crtendS.o, while without -shared, it will link in crtend.o instead. Thus, -shared and -Wl,-shared are not equivalent.
I don't think there is any difference. -shared is not a supported option of gcc and it is passed to linker whether you specify it with -Wl or not. -Wl option of gcc is used to specify that a comma separated list of options is to be passed to linker for further processing.

Makefile for Shared Libraries?

I've just written a Makefile to build a shared library, similar to the following:
libmystuff.so: CFLAGS+=-fPIC -shared
libmystuff.so: libmystuff.o otherstuff.o
$(CC) $(CFLAGS) -o $# $^
I like to avoid doing explicit actions when this seems like a common operation, but it seems there's no implicit rule or other built-ins to standardize this. I'm using GNU Make on Linux at the moment, but will need this to work on OS X as well.
EDIT: I'm asking about make rules rather than compiler/linker flags.
Can you recommend clean, reusable Makefile rules to build shared libs? Perhaps a %.so: or .c.so: type rule?
For portability, I'd look into integrating libtool.
define compile_rule
libtool --mode=compile \
$(CC) $(CFLAGS) $(CPPFLAGS) -c $<
endef
define link_rule
libtool --mode=link \
$(CC) $(LDFLAGS) -o $# $^ $(LDLIBS)
endef
LIBS = libmystuff.la
libmystuff_OBJS = libmystuff.lo otherstuff.lo
%.lo: %.c
$(call compile_rule)
libmystuff.la: $(libmystuff_OBJS)
$(call link_rule)
install/%.la: %.la
libtool --mode=install \
install -c $(notdir $#) $(libdir)/$(notdir $#)
install: $(addprefix install/,$(LIBS))
libtool --mode=finish $(libdir)
libtool will automatically add -fPIC/-DPIC/-shared flags as appropriate, and generate whatever .o/.a/.so files would be used on the current platform.
Or you could use Automake's libtool integration.
Building shared libraries is platform dependent. For example, the flags you are using are
ok for GCC for ELF platforms, for cygwin, for example, you do not add -fPIC for some other platforms and compilers you need other flags.
You need one of:
Provide an option to set flags for user platform.
Use standard build system like Autotools

Resources