【问题标题】:(automake, libtool) build fails in automake when using same source file name in different directory(automake, libtool) 在不同目录中使用相同的源文件名时,automake 构建失败
【发布时间】:2015-06-09 12:36:16
【问题描述】:

操作系统:Ubuntu 14.14
automake 版本:automake (GNU automake) 1.14.1

我使用 automake 构建源代码来制作共享库。 但是我得到一个错误,automake 可能无法处理同名和不同目录的源代码。 不能用automake编译不同目录下相同文件名的源码吗?

比如lib_sample/lib.c、lib_sample/enhance/lib.c这两个文件同名但源不同、目录不同。

另外,输入configure后,我检查src目录,奇怪的目录($(LIB_DIR))被创建了。

./configure --with-lib=/home/user/workspace/lib_sample --enable-enhance
ls src/
$(LIB_DIR) Makefile Makefile.am Makefile.in mod_sample.c
--> $(LIB_DIR) 被创建。创建了意外的目录。

我在输入 make 命令时收到一条错误消息。

错误信息如下所示。

Makefile:423: /src/.deps/lib.Plo: No such file or directory
Makefile:424: /src/enhanced/.deps/lib.Plo: No such file or directory
make[1]: *** No rule to make target `/src/enhanced/.deps/lib.Plo'.  Stop.
make[1]: Leaving directory `/home/user/test/mod_sample/src'
make: *** [all-recursive] Error 1

第一个问题是,在配置脚本之后,创建了 $(LIB_DIR) 目录。

第二个问题是当我键入 make 命令时构建源代码失败。 我认为 libtool 在尝试制作 .Plo 文件时无法编译,但我无法理解的是为什么 automake 会尝试编译 lib_sample/src/lib.c。 因为在编译 ./configure 脚本中的源代码之前,我用 --with-lib 和 --enable-enhance 选项表示 LIB_DIR 路径。因此,automake 不应该管理 src/lib.c。 但是,automake 尝试编译 lib_sample/src/lib.c 并出现奇怪的消息。

你可以在下面看到我写的代码,以及详细的项目结构。

提前致谢。

这里有详细信息。

有两个目录。

/home/user/test/mod_sample,(构建失败)
/home/user/test/lib_sample(这里有其他源代码)

configure.ac (/home/user/test/mod_sample/configure.ac)

dnl Process this file with autoconf to produce a configure script.

AC_PREREQ(2.59)
AC_INIT(sample, 1.5.0)
AC_PROG_CC
AC_PROG_CXX

AC_CANONICAL_SYSTEM

AM_INIT_AUTOMAKE([subdir-objects])

AM_PROG_CC_C_O

LT_INIT

AC_PROG_LIBTOOL

AC_MSG_CHECKING(--with-lib)
AC_ARG_WITH([lib],
AC_HELP_STRING([--with-lib=DIR], [Set lib path ]),
    [LIB_DIR=$withval]
    [have_lib=yes],
    [have_lib=no])
AC_MSG_RESULT($LIB_DIR)
AC_SUBST(LIB_DIR)

AC_ARG_ENABLE([[enhance]],
  [AS_HELP_STRING([[--disable-enhance]], [enhance])], ,
  [enable_enhance=yes])
  test "x$enable_enhance" = "xno" || enable_enhance=yes
AM_CONDITIONAL([HAVE_ENHANCE], [test "x$enable_enhance" = "xyes"])

AC_CONFIG_FILES(Makefile
          include/Makefile
          src/Makefile)
AC_OUTPUT

顶层目录 Makefile.am(/home/user/test/mod_sample/Makefile.am)

SUBDIRS=src include

src/Makefile.am (/home/user/test/mod_sample/src/Makefile.am)

lib_LTLIBRARIES = libmod_sample.la
libmod_sample_la_SOURCES = mod_sample.c

if HAVE_ENHANCE
    libmod_sample_la_SOURCES+=$(LIB_DIR)/src/enhanced/lib.c
else
    libmod_sample_la_SOURCES+=$(LIB_DIR)/src/lib.c
endif

在make之前运行configure命令

自动重新配置 -if
./configure --with-lib=/home/user/workspace/lib_sample --enable-enhance

我输入“make”时的错误消息。

Makefile:423: /src/.deps/lib.Plo: No such file or directory
Makefile:424: /src/enhanced/.deps/lib.Plo: No such file or directory
make[1]: *** No rule to make target `/src/enhanced/.deps/lib.Plo'.  Stop.
make[1]: Leaving directory `/home/user/test/mod_sample/src'
make: *** [all-recursive] Error 1

lib_sample 目录结构(不同目录下同名文件)

/home/user/workspace/lib_sample/lib.c
/home/user/workspace/lib_sample/enhance/lib.c

lib_sample/lib.c

void lib()
{
  printf("hello\n");
}

lib_sample/enhance/lib.c

void lib()
{
  printf("enhance hello\n");
}

【问题讨论】:

  • 你说你有错误?从什么命令?错误是什么? 究竟是什么在这里不起作用?

标签: c build makefile automake libtool


【解决方案1】:

这是我自己回答的。

根据参考 (http://www.gnu.org/software/automake/manual/html_node/Conditional-Sources.html),用户指定的变量 $(LIB_DIR) 在这种情况下不能用于 _SOURCES 变量。但是,在我的情况下它工作正常,除非变量与相同的文件名和 if 语句一起使用。问题是用户指定的变量用于 _SOURCES 变量和具有相同文件名的 if 语句。

所以,我认为有两种可能的解决方案。一是我可能会更改其中一个文件名以避免使用相同的文件名。另一个是使用变量表示'enhance'字符串,即目录名,去掉Makefile.am中的if语句。

解决方案 1

mv lib_sample/enhance/lib.c lib_sample/enhance/lib_enhance.c
制作

lib_LTLIBRARIES = libmod_sample.la
libmod_sample_la_SOURCES = mod_sample.c

if HAVE_ENHANCE
    libmod_sample_la_SOURCES+=$(LIB_DIR)/src/enhanced/lib_enhance.c # change the file name
else
    libmod_sample_la_SOURCES+=$(LIB_DIR)/src/lib.c
endif

但是,我不能更改文件名,这个原因有点难以解释,简而言之,因为源代码许可。

解决方案 2

我就是这样解决问题的。

configure.ac 的一部分 (/home/user/test/mod_sample/configure.ac)

AC_ARG_ENABLE([[enhance]],
  [AS_HELP_STRING([[--disable-enhance]], [enhance])], ,
  [enable_enhance=yes])
  test "x$enable_enhance" = "xno" || enable_enhance=yes
AM_CONDITIONAL([HAVE_ENHANCE], [test "x$enable_enhance" = "xyes"])
AM_COND_IF([HAVE_ENHANCE], [AC_SUBST(ENHANCE, "enhance")], [AC_SUBST(ENHANCE, "")]) # Add this line

src/Makefile.am (/home/user/test/mod_sample/src/Makefile.am)

lib_LTLIBRARIES = libmod_sample.la
libmod_sample_la_SOURCES = mod_sample.c

# Use $(ENHANCE) variable to remove if statement
# $(ENHANCE) can be "enhance" or empty string.
libmod_sample_la_SOURCES+=$(LIB_DIR)/src/$(ENHANCE)/lib.c

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-05
    • 2012-12-06
    • 2013-08-27
    • 2014-11-30
    • 1970-01-01
    • 2021-11-01
    • 2011-09-24
    • 1970-01-01
    相关资源
    最近更新 更多