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

标签 c++ static-libraries autoconf automake

我正在开发一个名为 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

还有我的configure.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。有几种方法可以做到这一点,但最好的方法可能是使用 per-target 变量:

spellcheck_LDADD = ../libspellcheck/libspellcheck.a

你不需要那些引号。

关于c++ - Automake 和 Autoconf - 程序无法识别刚刚构建的静态库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18062212/

相关文章:

c++ - 添加到 std::map 的元素是否会自动初始化?

c++ - byte中提取bit的效率

bash - autoconf 使用 sh,我需要 SHELL=BASH,如何强制 autoconf 使用 bash?

dynamic-linking - 切换到动态链接

linux - 如何使用 Autoconf 检查 Linux 版本?

c++ - 为 C/C++ 程序编写检测工具

linux - 静态链接依赖

Code::Blocks + MinGW:最小化静态库的大小

c++ - 将对象从 C++ 存档 (.a) 包含到共享库中

c++ - 我可以使用 popt 库一次读取两个选项值吗?