LINUX.ORG.RU

Линковка с своей библиотекой


0

0

Доброго вам уважаемые.

Хочу часть софтины на c++ вынести в статическую библиотеку. Сделал в дереве исходников поддиректорию my_lib, перенес в нее исходники библиотеки. Библиотеку собираю так:
ar rcs libmy_lib.a src1.o src2.o src3.o
На этом этапе все ок, но при линковке основной программы с этой библиотекой появляется куча undefined reference на символы из моей библиотеки. Основную программу линкую так:
g++ -o main -Lmy_lib <other_Ldirs> -lmy_lib <other_llibs> main1.o main2.o main3.o

Если собирать основную программу не с либой, а с её объектниками, то всё ок:
g++ -o main <other_Ldirs> <other_llibs> main1.o main2.o main3.o my_lib/src1.o my_lib/src2.o my_lib/src3.o

Есть идеи что может быть не так?

anonymous

g++ -o main -Lmy_lib <other_*shared*_llibs> main1.o main2.o main3.o my_lib/static_lib0 ... my_lib/static_libN

; asgard

anonymous
()
Ответ на: комментарий от watashiwa_daredeska

--> -Lmy_lib <other_*shared*_llibs>

на случий если у топикстартера в my_lib находятся ещё и so'шки.

; asgard

anonymous
()
Ответ на: комментарий от anonymous

Спасибо за ответ. Теперь собирается. Однако, непонятно что было не так с -lmy_lib? В моей системе man g++ говорит следующее:

-llibrary
	  Use the library named library when linking.
	  The  linker  searches a standard list of directories for
          the library, which is actually a file named 
          `liblibrary.a'.  The linker  then uses this file as if it
          had been specified precisely by name.

	  The directories searched include several standard system  
          directories plus any that you specify with `-L'.
	  Normally  the  files  found  this way are library
          files--archive files whose members are object files.  The 
          linker handles an archive  file by scanning through it for
          members which define symbols that have so far been
          referenced but not defined.  However, if  the  linker
          finds an ordinary object file rather than a library, the 
          object file is linked in the usual fashion.  The only
	  difference  between  using  an `-l' option and specifying a
          file name is that `-l' surrounds library  with  `lib'  and  
          `.a'  and searches several directories.

Последнее предложение вроде бы и говорит, что между указанием через -lmy_lib и libmy_lib.a нет никакой разницы кроме того, что в первом случае поиск библиотеки идет во всех указанных через -L директориях (плюс системные). Поискал в других директориях - на винте больше нигде нет библиотек с таким именем.

anonymous
()
Ответ на: комментарий от anonymous

внимательнее читайте маны:

The linker will search an archive only once, at the location where it is specified on the command line. If the archive defines a symbol which was undefined in some object which appeared ***before*** the archive on the command line, the linker will include the appropriate file(s) from the archive. However, an undefined symbol in an object appearing later on the command line will not cause the linker to search the archive again.

; asgard

anonymous
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.