静态库和头在与海湾合作委员会的另一个目录 [英] Static libraries and headers in another directories with GCC

查看:200
本文介绍了静态库和头在与海湾合作委员会的另一个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找这里如何使用GCC静态库,并且说明是pretty清晰(鄙视的事实,我不得不重写阶乘的功能)做的事:我有一个函数(fact.c),功能(fact.h)的头,和主要功能(main.c中),他们都在我的主目录。

fact.h

  INT事实(INT);

fact.c

  INT
事实上(INT F){
  如果(F == 0)
    返回1;
其他
    收益率(F *事实(F - 1));
}

的main.c

 的#include<&stdio.h中GT;
#包括fact.h
INT主(INT ARGC,CHAR *的argv [])
{
        的printf(%d个\\ N,其实(3));
        返回0;
}

所以,我必须首先生成目标文件(阶段1)...

  $ GCC -c fact.c -o fact.o

...然后生成静态库(阶段2)...

  $ AR RCS libfact.a fact.o

...后来做静态库链接过程(第3阶段)...

  $ gcc的-static的main.c -L。 -lfact -o事实

...最后运行的程序(第4阶段也是最后一个)

  $ ./fact

我的问题如下。让我们假设我的计划会这么大,我别无选择,不是把头一个头目录(/include/fact.h)和静态库也将在另一个目录(/lib/libfact.a)。在这种情况下,如何编译和/或本程序的code会改变吗?

编辑/问题解决了:一是main.c中以包括在另一个叫做包括目录中的头球​​攻门被纠正。记住这一点,在这种情况下,无论是.c文件都在主目录

的main.c

 的#include<&stdio.h中GT;
#包括包括/ fact.h
INT主(INT ARGC,CHAR *的argv [])
{
        的printf(%d个\\ N,其实(3));
        返回0;
}

二,生成另一个目录(第2阶段)的静态库,这是我做了什么:

  $ AR RCS的lib / libfact.a fact.o


解决方案

下面是你的答案,

$ gcc的-static的main.c -L。 -lfact -o事实上

-L将目录添加到目录列表中搜索-l

其在你给的链接。如果你把正确SEACH方向和低的搜索范围,这将不会是一个问题。否则它是不会编译code。因为code不知道哪里是头。

I was looking here how to do static libraries using GCC, and the explanation is pretty clear (despise the fact I had to rewrite the factorial function): I have a function (fact.c), the header of the function (fact.h), and the main function (main.c), all of them in my home directory.

fact.h

int fact (int);

fact.c

int
fact (int f) {
  if ( f == 0 )
    return 1;
else
    return (f * fact ( f - 1 ));
}

main.c

#include <stdio.h>
#include "fact.h"
int main(int argc, char *argv[])
{
        printf("%d\n", fact(3));
        return 0;
}

So I had first to generate the object file (phase 1)...

$ gcc -c fact.c -o fact.o 

...then to generate the static library (phase 2)...

$ ar rcs libfact.a fact.o

...later to do the static library linking process (phase 3)...

$ gcc -static main.c -L. -lfact -o fact

...and finally run the program (phase 4 and final)

$ ./fact 

My question is the following. Let's suppose my program will be so big that I had no alternative than put the headers in a header directory (/include/fact.h) and the static libraries will also be in another directory (/lib/libfact.a). In that case, how the compilation and/or the code of this program will change?

Edit/Problem Solved: First, the main.c was corrected in order to include a header in another directory called include. Remember that, in this case, both of the .c files are in the main directory.

main.c

#include <stdio.h>
#include "include/fact.h"
int main(int argc, char *argv[])
{
        printf("%d\n", fact(3));
        return 0;
} 

Second, to generate the static library in another directory (phase 2), this is what I had done:

$ ar rcs lib/libfact.a fact.o

解决方案

Here is your answer,

$ gcc -static main.c -L. -lfact -o fact

-L Add directory to the list of directories to be searched for -l

Its in the link that you gave. If you put the seach direction correctly and low search range, it will not be a problem. Otherwise it is not going to compile the code. Because code did not know where is the header.

这篇关于静态库和头在与海湾合作委员会的另一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆