Link a static library in gcc without having to specify the 'lib' prefix

时间:2015-09-01 21:39:00

标签: c gcc static-libraries

According to this question, gcc's -l command requires your library to be named libXXX.a.

Is there a way to link a static library using a different command with gcc? The goal is to avoid this lib- prefix.

2 个答案:

答案 0 :(得分:6)

Just pass the library as in input file like so:

gcc main.c yourlibrary.a -o prog

答案 1 :(得分:5)

Like nunzio said. Just pass it in directly as an input file. He beat me to it, but here's a full example anyway.

mylib.c:

#include <stdio.h>
void say_hi(void) 
{ 
  printf("hi\n"); 
}

main.c:

extern void say_hi(void);
int main(int argc, char**argv)
{
  say_hi();
  return 0;
}

Makefile:

main: main.c mylib.a
    gcc -o main main.c mylib.a

mylib.a: mylib.o
    ar rcs mylib.a mylib.o

mylib.o: mylib.c
    gcc -c -o $@ $^

I realize this assumes some background knowledge in Make. To do the same thing w/o make, run these commands in order:

gcc -c -o mylib.o mylib.c
ar rcs mylib.a mylib.o
gcc -o main main.c mylib.a