Automake & Autoconf - 程序无法识别刚刚构建的静态库

Automake & Autoconf - Program Doesn't Recognize Static Library Just Built

本文关键字:构建 静态 识别 Autoconf 程序 Automake      更新时间:2023-10-16

我正在开发一个名为libspellcheck的库,以及一个使用它来检查拼写的程序,该程序名为spellcheck。这是我的目录结构:

libspellcheck
       -> doc
       -> man
       -> libspellcheck (libspellcheck source)
       -> spellcheck (spellcheck source)

我想使用配置脚本,而不是我一直使用的普通Makefile。一切都很顺利,直到我尝试编译拼写检查:

Making all in libspellcheck
make[1]: Entering directory `/home/iandun/libspellcheck-devel/libspellcheck/libspellcheck'
g++ -DHAVE_CONFIG_H -I.     -g -O2 -MT checker.o -MD -MP -MF .deps/checker.Tpo -c -o checker.o checker.cpp
mv -f .deps/checker.Tpo .deps/checker.Po
g++ -DHAVE_CONFIG_H -I.     -g -O2 -MT SpellCorrector.o -MD -MP -MF .deps/SpellCorrector.Tpo -c -o SpellCorrector.o SpellCorrector.cpp
mv -f .deps/SpellCorrector.Tpo .deps/SpellCorrector.Po
rm -f libspellcheck.a
ar cru libspellcheck.a checker.o SpellCorrector.o 
ranlib libspellcheck.a
make[1]: Leaving directory `/home/iandun/libspellcheck-devel/libspellcheck/libspellcheck'
Making all in spellcheck
make[1]: Entering directory `/home/iandun/libspellcheck-devel/libspellcheck/spellcheck'
g++ -DHAVE_CONFIG_H -I.     -g -O2 -MT spellcheck.o -MD -MP -MF .deps/spellcheck.Tpo -c -o spellcheck.o spellcheck.cpp
spellcheck.cpp: In function 'int main(int, char**)':
spellcheck.cpp:111:21: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
  char *dictionary = "/etc/english.dict"; //Default Dictionary
                     ^
spellcheck.cpp:164:14: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
   dictionary = "/etc/english.dict";
              ^
mv -f .deps/spellcheck.Tpo .deps/spellcheck.Po
g++ -DHAVE_CONFIG_H -I.     -g -O2 -MT meta.o -MD -MP -MF .deps/meta.Tpo -c -o meta.o meta.cpp
mv -f .deps/meta.Tpo .deps/meta.Po
g++  -g -O2  "../libspellcheck/libspellcheck.a" -o spellcheck spellcheck.o meta.o  
spellcheck.o: In function `correctMisspelled(std::string, std::string)':
/home/iandun/libspellcheck-devel/libspellcheck/spellcheck/spellcheck.cpp:29: undefined reference to `correctSpelling(std::string, std::string)'
spellcheck.o: In function `doFileCheck(char*, char*, char*, spelling)':
/home/iandun/libspellcheck-devel/libspellcheck/spellcheck/spellcheck.cpp:61: undefined reference to `check_spelling_file(char*, char*, std::string)'
spellcheck.o: In function `main':
/home/iandun/libspellcheck-devel/libspellcheck/spellcheck/spellcheck.cpp:193: undefined reference to `add_word(char*, char*)'
/home/iandun/libspellcheck-devel/libspellcheck/spellcheck/spellcheck.cpp:224: undefined reference to `check_spelling_string(char*, std::string, std::string)'
meta.o: In function `do_about_msg()':
/home/iandun/libspellcheck-devel/libspellcheck/spellcheck/meta.cpp:29: undefined reference to `lib_version()'
collect2: error: ld returned 1 exit status
make[1]: *** [spellcheck] Error 1
make[1]: Leaving directory `/home/iandun/libspellcheck-devel/libspellcheck/spellcheck'
make: *** [all-recursive] Error 1

我在拼写检查目录下的Makefile.am文件中引用了libspellcheck.a:

CFLAGS = -m32 -Wall 
LDFLAGS = "../libspellcheck/libspellcheck.a"
bin_PROGRAMS = spellcheck
spellcheck_SOURCES = spellcheck.cpp meta.cpp 

还更改了我对拼写检查头文件的引用:

#include "../libspellcheck/spellcheck.h"

这是我在libspellcheck文件夹中的Makefile.am:

CFLAGS = -m32 -Wall
LDFLAGS = 
lib_LIBRARIES = libspellcheck.a
libspellcheck_a_SOURCES = checker.cpp SpellCorrector.cpp 
include_HEADERS = spellcheck.h SpellCorrector.h

这是我的Makefile.am在主文件夹中:

AUTOMAKE_OPTIONS = foreign
SUBDIRS = libspellcheck spellcheck man

我的配置.ac:

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT(libspellcheck, 1.25, corinthianmonthly@hotmail.com)
AC_OUTPUT(Makefile libspellcheck/Makefile spellcheck/Makefile man/Makefile)
AC_CONFIG_SRCDIR([])
AC_CONFIG_HEADERS([])
AM_INIT_AUTOMAKE
# Checks for programs.
AC_PROG_CXX
AC_PROG_CC
AC_PROG_CXX
AC_PROG_RANLIB

# Checks for libraries.
# Checks for header files.
AC_CHECK_HEADERS([stdlib.h,iostream,fstream,string,stdio.h,sstream,cctype,algorithm,boost/algorithm/string.hpp])
# Checks for typedefs, structures, and compiler characteristics.
AC_CHECK_HEADER_STDBOOL
AC_TYPE_SIZE_T
# Checks for library functions.
AC_OUTPUT

我做错了什么?

CFLAGS = -m32 -Wall 
LDFLAGS = "../libspellcheck/libspellcheck.a"

首先,这些是用户标志。请改用AM_*表格。

第二,LDFLAGS是用于标志的。它被放置在命令行的开头附近。但是,对于链接,顺序很重要,所以您需要使用LDADD。有几种方法可以做到这一点,但也许最好的方法是使用每个目标变量:

拼写检查_LDAD=/libspellcheck/libspellcheck.a

你不需要这些引号。