LINUX.ORG.RU

[gcc][ld] порядок аргументов

 ,


0

0
/* 1.c */
void a(void) {
  b();
}
/* 2.c */
void b(void) {
  exit(0);
}
/* main.c */
int main(int argc, char *argv[]) {
  a();
  return 0;
}

... компиляция поскипана ...

>$ ar qc 1.a 1.o
>$ ar qc 2.a 2.o
>$ gcc -o main main.o 2.a 1.a
1.a(1.o): In function `a':
1.c:(.text+0xa): undefined reference to `b'
collect2: ld returned 1 exit status
>$ gcc -o main main.o 1.a 2.a
>$

Всегда считал, что от порядка, в котором скармливаются статические либы не зависит ровным счетом ничего. Собственно вопрос, где про это можно почитать (в каком разделе мана это может быть)? Или, быть может, это у меня компилер с норовом?


Из мана на gcc:

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files
in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers
to functions in z, those functions may not be loaded.
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.

<skipped>

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.

eXire ★★
()
Ответ на: комментарий от akk

> Кури --as-needed.

Может топик внимательнее почитать?

--as-needed
--no-as-needed
This option affects ELF DT_NEEDED tags for *dynamic* libraries mentioned on the command line after the --as-needed option.

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