简体   繁体   中英

undefined reference when using local header file

I encountered an undefined reference error when working with automake, which only occurs when a certain local header file ( include/lmp2atomstyle.h , which is required by a library within the same repository) exists in the repository.

I was working on combining multiple automake project source folders, lmpio and lmp2atomstyle , into a single one for convenience. Both projects are libraries which also provide a small program each for testing.

After manually merging Makefile.am , lmp2atomstyle compiles just fine, but lmpio cannot find the symbols from liblmp2atomstyle.so :

$ make check
make  lmpiotest
make[1]: Entering directory `/home/e.lorenz/code/lmputils'
/bin/sh ./libtool --tag=CXX   --mode=link g++  -g -O2 -lfftw3 -llmpio -llmp2atomstyle  -o lmpiotest src/lmpiotest.o -llammps_custom -lmpi_stubs
libtool: link: g++ -g -O2 -o .libs/lmpiotest src/lmpiotest.o  -lfftw3 /home/e.lorenz/code/lmputils/.libs/liblmpio.so /cluster/gcc/gcc-4.8.2/lib/../lib64/libstdc++.so -lm /home/e.lorenz/code/lmputils/.libs/liblmp2atomstyle.so -llammps_custom -lmpi_stubs -Wl,-rpath -Wl,/usr/local/lib -Wl,-rpath -Wl,/cluster/gcc/gcc-4.8.2/lib/../lib64
/home/e.lorenz/code/lmputils/.libs/liblmpio.so: undefined reference to `lmp2atomstyle_parse_file(void*, char const*)'
/home/e.lorenz/code/lmputils/.libs/liblmpio.so: undefined reference to `lmp2atomstyle_get_style(void*, char*, unsigned long)'
/home/e.lorenz/code/lmputils/.libs/liblmpio.so: undefined reference to `lmp2atomstyle_create()'
collect2: error: ld returned 1 exit status
make[1]: *** [lmpiotest] Error 1
make[1]: Leaving directory `/home/e.lorenz/code/lmputils'
make: *** [check-am] Error 2

Here's Makefile.am :

AUTOMAKE_OPTIONS = subdir-objects
ACLOCAL_AMFLAGS = -I m4

AM_CPPFLAGS = -Iinclude

lib_LTLIBRARIES = liblmp2atomstyle.la liblmpio.la
include_HEADERS = include/lmpio.h include/lmp2atomstyle.h
liblmp2atomstyle_la_SOURCES = src/lmp2atomstyle.c
liblmp2atomstyle_la_LDFLAGS = -version-info 1:0:0
liblmpio_la_SOURCES = src/lmpio.cpp
liblmpio_la_LDFLAGS = -version-info 1:0:0 -llmp2atomstyle

bin_PROGRAMS = lmp2atomstyle
lmp2atomstyle_SOURCES = src/lmp2atomstyle_main.c
lmp2atomstyle_LDFLAGS = -llmp2atomstyle

check_PROGRAMS = lmpiotest
lmpiotest_SOURCES = src/lmpiotest.cpp
lmpiotest_LDADD = -llammps_custom -lmpi_stubs
lmpiotest_LDFLAGS = -lfftw3 -llmpio -llmp2atomstyle

First, I assumed a wrong linking order, but after carefully evaluating the dependencies of all libraries, the linking order seemed to be fine. It worked before the repository merge, anyhow. I also used LDADD only, but it didn't help.

After tracking the changes, I found that lmpio compiles fine as long as include/lmp2atomstyle.h doesn't exist, but is included automatically from /usr/local/include. The libraries aren't installed at this point. As soon as I copy lmp2atomstyle.h to include/ , the error occurs.

What am I missing? Is there some requirement regarding local headers and libraries? How can the path of a header file cause linking errors when working with automake?

Follow-up question: If it's caused by a missing extern guard, why does it work when compiling in different directories?

Here's the lmpiotest output of a successful make check run, ie without include/lmp2atomstyle.h :

make  lmpiotest
make[1]: Entering directory `/home/e.lorenz/code/lmputils'
depbase=`echo src/lmpiotest.o | sed 's|[^/]*$|.deps/&|;s|\.o$||'`;\
  g++ -DHAVE_CONFIG_H -I.  -Iinclude   -g -O2 -MT src/lmpiotest.o -MD -MP -MF $depbase.Tpo -c -o src/lmpiotest.o src/lmpiotest.cpp &&\
  mv -f $depbase.Tpo $depbase.Po
/bin/sh ./libtool --tag=CXX   --mode=link g++  -g -O2 -lfftw3 -llmpio -llmp2atomstyle  -o lmpiotest src/lmpiotest.o -llammps_custom -lmpi_stubs
libtool: link: g++ -g -O2 -o .libs/lmpiotest src/lmpiotest.o  -lfftw3 /home/e.lorenz/code/lmputils/.libs/liblmpio.so /cluster/gcc/gcc-4.8.2/lib/../lib64/libstdc++.so -lm /home/e.lorenz/code/lmputils/.libs/liblmp2atomstyle.so -llammps_custom -lmpi_stubs -Wl,-rpath -Wl,/usr/local/lib -Wl,-rpath -Wl,/cluster/gcc/gcc-4.8.2/lib/../lib64
make[1]: Leaving directory `/home/e.lorenz/code/lmputils'

My configure.ac :

AC_INIT([lmputils], [1.0], [(email...)])
LT_INIT
AM_INIT_AUTOMAKE()
AC_CONFIG_HEADERS([config.h])
AC_PROG_CXX
AM_PROG_CC_C_O
AC_CONFIG_FILES([Makefile])
AC_OUTPUT

Finally, here's the minimal directory tree that causes the error:

./include
./include/lmpio.h
./include/lmp2atomstyle.h
./src
./src/lmp2atomstyle.c
./src/lmp2atomstyle_main.c
./src/lmpio.cpp
./src/lmpiotest.cpp
./Makefile.am
./configure.ac
./autogen.sh

Thanks :)

As Matt McNabb pointed out, it has to do with extern guards:

In include/lmp2atomstyle.h , I forgot to add an extern guard for c++:

#ifdef __cplusplus
extern "C" {
#endif

  [function headers]

#ifdef __cplusplus
}
#endif

That fixed it for me.

Follow-up question: Why did it work when compiling in different directories?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM