C/C++ 尝试创建共享库时出错.创建库的静态版本时不会发生错误

C/C++ Error when trying to create a shared library. Error does not occur when making the static version of the library

本文关键字:创建 版本 错误 静态 C++ 共享 出错      更新时间:2023-10-16

我正在尝试创建一个共享库版本的praatlib。 代码本身带有一个生成静态库的 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

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

让我们仔细看看用于查找对象文件的命令:

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

请注意,stat出现两次。 别这样。