【问题标题】:C/C++ Error when trying to create a shared library. Error does not occur when making the static version of the library尝试创建共享库时出现 C/C++ 错误。制作库的静态版本时不会出现错误
【发布时间】:2012-05-16 20:59:19
【问题描述】:

我正在尝试创建praatlib 的共享库版本。代码本身带有一个生成静态库的 Makefile,但出于我的目的,我需要一个共享库。我已经尝试了两件事。我尝试做的第一件事是编辑 Makefile 以生成一个共享库以及静态库。

这是生成静态库的 makefile 部分:

libpraat.a:
   cd GSL; make
   cd num; make
   cd num/glpk; make
   cd kar; make
   cd audio; make
   cd mp3; make
   cd FLAC; make
   cd stat; make
   cd fon; make
   cd dwsys; make
   cd dwtools; make
   cd LPC; make
   cd FFNet; make
   cd artsynth; make
   cd library; make
   rm -f libpraat.a
   ar r libpraat.a `find num glpk audio stat LPC FFNet dwtools artsynth fon stat dwsys GSL kar FLAC mp3 library -name "*.o"`

这是我为生成共享库而添加的内容。

libpraat.so:
   cd GSL; make
   cd num; make
   cd num/glpk; make
   cd kar; make
   cd audio; make
   cd mp3; make
   cd FLAC; make
   cd stat; make
   cd fon; make
   cd dwsys; make
   cd dwtools; make
   cd LPC; make
   cd FFNet; make
   cd artsynth; make
   cd library; make
   rm -f libpraat.so
   $(CC) -shared -Wl,-soname,libpraat.so -o libpraat.so `find num glpk audio stat LPC FFNet dwtools artsynth fon stat dwsys GSL kar FLAC mp3 library -name "*.o"`

创建静态库时没有遇到任何问题,但是当它尝试创建共享库时会出错。

这是错误信息的开头(有很多错误,但它们都与我在下面粘贴的基本相同)。

gcc -std=gnu99 -DUNIX -Dlinux -DCONSOLE_APPLICATION -I /usr/local/include -I /usr/X11R6/include -Wimplicit -Wreturn-type -Wunused -Wunused-parameter -Wuninitialized -O -fPIC -Wall -shared -Wl,-soname,libpraat.so -o libpraat.so `find num glpk audio stat LPC FFNet dwtools artsynth fon stat dwsys GSL kar FLAC mp3 library -name "*.o"`
stat/Table.o: In function `Table_getStringValue':
Table.c:(.text+0x246): multiple definition of `Table_getStringValue'
stat/Table.o:Table.c:(.text+0x246): first defined here
stat/Table.o:(.data.rel+0x174): multiple definition of `classTable'
stat/Table.o:(.data.rel+0x174): first defined here
stat/Table.o:(.data.rel+0xb4): multiple definition of `classTableRow'
stat/Table.o:(.data.rel+0xb4): first defined here
stat/Table.o: In function `Table_appendRow':
Table.c:(.text+0x1147): multiple definition of `Table_appendRow'
stat/Table.o:Table.c:(.text+0x1147): first defined here
stat/Table.o: In function `Table_initWithoutColumnNames':
Table.c:(.text+0x11a5): multiple definition of `Table_initWithoutColumnNames'
stat/Table.o:Table.c:(.text+0x11a5): first defined here
stat/Table.o: In function `Table_createWithoutColumnNames':
Table.c:(.text+0x123f): multiple definition of `Table_createWithoutColumnNames'
stat/Table.o:Table.c:(.text+0x123f): first defined here
stat/Table.o: In function `Table_insertColumn':
Table.c:(.text+0x1298): multiple definition of `Table_insertColumn'
stat/Table.o:Table.c:(.text+0x1298): first defined here
stat/Table.o: In function `Table_appendColumn':
Table.c:(.text+0x151e): multiple definition of `Table_appendColumn'

在尝试了很多不同的事情并感到沮丧之后,我尝试使用以下命令将我拥有的静态库直接转换为共享库:

g++ -std=c++98 -fpic -g -O1 -shared -o libpraat.so -Wl,--whole-archive libpraat.a

但我收到了与以前类似的错误消息。我在构建大型项目或库方面不是很有经验,所以我不知道如何让它工作。如果有人能解释导致我遇到错误的原因以及如何修复它,我将不胜感激。

【问题讨论】:

  • 如果去掉所有的“-Wl”位会怎样?

标签: c++ c g++ shared-libraries static-libraries


【解决方案1】:

让我们仔细看看你用来查找目标文件的命令:

find num glpk audio stat LPC FFNet dwtools artsynth fon \
        stat dwsys GSL kar FLAC mp3 library -name "*.o"

注意stat 出现了两次。不要那样做。

【讨论】:

  • 谢谢。不幸的是,原作者写的东西,我认为错误是由我介绍的东西引起的,所以我从来没有过多关注这个 find 语句。
  • 我认为当你制作静态库时它没有效果,因为每个静态库只能保存一个具有给定名称的目标文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-18
  • 2018-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多