簡體   English   中英

使用本地頭文件時未定義的引用

[英]undefined reference when using local header file

在使用automake時遇到了undefined reference錯誤,只有在存儲庫中存在某個本地頭文件( include/lmp2atomstyle.h ,這是同一存儲庫中的庫所需)時才會發生。

為方便起見,我正在努力將多個automake項目源文件夾lmpiolmp2atomstyle合並為一個。 這兩個項目都是庫,它們也提供了一個用於測試的小程序。

手動合並后Makefile.amlmp2atomstyle編譯就好了,但lmpio無法找到符號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

這是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

首先,我假設一個錯誤的鏈接順序,但在仔細評估所有庫的依賴關系后,鏈接順序似乎沒問題。 無論如何,它在存儲庫合並之前有效。 我也只使用LDADD ,但它沒有幫助。

跟蹤更改后,我發現只要include/lmp2atomstyle.h不存在, lmpio編譯就可以了,但是自動包含在/ usr / local / include中。 此時未安裝庫。 只要將lmp2atomstyle.h復制到include/ ,就會發生錯誤。

我錯過了什么? 是否有關於本地標頭和庫的要求? 在使用automake時,頭文件的路徑如何導致鏈接錯誤?

后續問題:如果它是由缺少外部防護引起的,為什么它在不同目錄中編譯時有效?

這是成功的make check run的lmpiotest輸出,即沒有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'

我的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

最后,這是導致錯誤的最小目錄樹:

./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

謝謝 :)

正如Matt McNabb指出的那樣,它與外部守衛有關:

include/lmp2atomstyle.h ,我忘了為c ++添加一個外部保護:

#ifdef __cplusplus
extern "C" {
#endif

  [function headers]

#ifdef __cplusplus
}
#endif

這為我解決了這個問題。

后續問題:為什么在不同目錄中編譯時它可以工作?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM