开发者

"file not found " error while creating a static library in c using gcc - linux

I have a file hello.c

#include<stdio.h>

void hello()
{
    printf("Hello!\n");
}

A header hello.h

#ifndef _hello_
#define _hello_

void hello();

#endif

main.c

#include<stdio.h>
#include "hello.h"

int main()
{

  hello();
  return 0;
}

I am currently in the folder /home/user/name/programs

I am trying to bui开发者_运维百科ld a static library mylib.a. Here is what I do to build it

1. gcc -c hello.c 
2. ar rcs mylib.a hello.o
3. gcc -static main.c -L/home/user/name.programs -lib -o hello

I get the following error

/usr/bin/ld: cannot find -lib
collect2: ld returned 1 exit status

My questions are

`1. why is gcc searching for the file in the folder /usr/bin - I understand this is   
    what is happening ?
 2. How do I fix this?`


Change the library name to start with lib

ar rcs libmy.a hello.o
       ^^^^^^^
gcc -static main.c -L/home/user/name.programs -lmy -o hello
                                              ^^^^^  

And its not searching in /usr/bin,. The error is split by the linker (ld) which resides in /usr/bin.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜