问题描述
在我的项目中,将模块整理在子目录中以保持整洁.
In my project, modules are organized in subdirs for tidiness.
我的项目目录层次结构:
$ ls -R
.: configure.in Makefile.am Makefile.cvs src
./src: log Makefile.am main.cpp
./src/log: log.cpp Makefile.am
configure.in:
AC_INIT(configure.in)
AM_CONFIG_HEADER(config.h)
AM_INIT_AUTOMAKE(myapp, 0.1)
AC_LANG_CPLUSPLUS
AC_PROG_CXX
AM_PROG_LIBTOOL
AC_OUTPUT(Makefile src/Makefile src/log/Makefile)
Makefile.am:
AUTOMAKE_OPTIONS = foreign
SUBDIRS = src
Makefile.cvs:
default:
aclocal
libtoolize --force --copy
autoheader
automake --add-missing --copy
autoconf
src/Makefile.am
bin_PROGRAMS = myapp
myapp_SOURCES = main.cpp
SUBDIRS = log
myapp_LDADD = $(top_builddir)/src/log/liblog.la
src/log/Makefile.am:
INCLUDES = $(all_includes)
METASOURCES = AUTO
noinst_LTLIBRARIES = liblog.la
liblog_la_SOURCES = log.cpp
src/main.cpp:包含int main(){}
, src/log/log.cpp 包含void f(){}
.
调用make
会产生(为简便起见进行编辑):
Invoking make
produces (edited for brevity):
libtool --mode=compile g++ -MT log.lo -MD -MP -MF .deps/log.Tpo -c -o log.lo log.cpp
libtool: compile: g++ -MT log.lo -MD -MP -MF .deps/log.Tpo -c log.cpp -fPIC -DPIC -o .libs/log.o
libtool: compile: g++ -MT log.lo -MD -MP -MF .deps/log.Tpo -c log.cpp -o log.o >/dev/null 2>&1
mv -f .deps/log.Tpo .deps/log.Plo
libtool --mode=link g++ -o liblog.la log.lo
libtool: link: ar cru .libs/liblog.a .libs/log.o
libtool: link: ranlib .libs/liblog.a
libtool: link: ( cd ".libs" && rm -f "liblog.la" && ln -s "../liblog.la" "liblog.la" )
g++ -MT main.o -MD -MP -MF .deps/main.Tpo -c -o main.o main.cpp
mv -f .deps/main.Tpo .deps/main.Po
libtool --mode=link g++ -o myapp main.o ../src/log/liblog.la
libtool: link: g++ -o myapp main.o ../src/log/.libs/liblog.a
问题是前三行: log.cpp编译了两次!
问题:为什么不只编译一次-花费一半的时间?
注意:我不知道我在做什么-自动工具对我来说是不可思议的,但是我们必须在自己的地方使用它.这些文档令我难以理解.
note: I have no idea what I'm doing -- autotools is black magic to me, but we have to use it in our place. The docs are incomprehensible to me.
推荐答案
默认情况下,Libtool创建两种类型的库:静态库和共享库. (又名libfoo.a和libfoo.so)
By default Libtool creates two types of libraries: static and shared. (aka libfoo.a and libfoo.so)
静态和碎片需要不同的编译标志.动态库-共享对象使用具有以下gcc标志的位置独立代码:
Static and shard require different compilation flags. Dynamic libraries -- shared objectsuse Position Independent Code with following gcc flags:
-fPIC -DPIC
静态不是.您可以通过指定
Static are not. You can force build only one type by specifing
./configure --disable-shared
或
./configure --disable-static
理论上
通常,当为用户提供库时,它会提供两种设置-静态用于开发,允许创建纯静态构建,动态用于大多数人使用使用此库的程序.因此,通常在系统中安装库时您只安装共享对象. (又名libfoo_XYZ.deb)
Usually when library is provided for user it provide in two setups -- static for development that allows create pure static builds and dynamic for use by mostof programs that use this library. So, usually when you install library in the systemyou only install shared object. (aka libfoo_XYZ.deb)
当您添加开发版本(也称为libfoo-dev_XYZ.deb)时,您将添加标头并静态地库的构建版本,允许用户根据需要进行静态构建.
When you add development version (aka libfoo-dev_XYZ.deb) you add headers and staticallybuild version of the library that allows users make staic builds if they whant.
这是在UNIX中运送库的常见做法.因此,libtool这样做是为了您会自动.
This is common practice for shipping libraries in UNIX. Thus libtool does this foryou automatically.
这篇关于Libtool速度慢,是双重构建吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!