关于静太库中多个同名函数调用的例子

###################################################################################
###################################################################################
为了对以下几个问题进行分析:
        1:对静态库中的多个同名函数是否可以存在?
        2:如存在在调用时会如何调用(顺序??)?
        3:如库函数与不是打包在在库中的源代源中同时存在(如在main函数所在的文件里)又是如何调用?

 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++

1:问题1与问题2用的源码如下。问题3只是对main.c进行一个同名函数的增加(请看下文)


[mengfh@red244 tt2]$ cat *.c
main.c

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

int main()
{
        fprintf(stderr,"this is main.c \n");
        display();

        return 0;
}


t1.c

#include<stdio.h>

void display()
{
        fprintf(stderr,"this is t1.c\n");
}


t2.c

#include<stdio.h>
void display()
{
        fprintf(stderr,"this is t2.c update display() function\n");

}

----------------------------------------------------------------------------------
[mengfh@red244 tt2]$ clear

[mengfh@red244 tt2]$ ls
head.h  t1.c  t1.o  t2.c
[mengfh@red244 tt2]$ cc -c *.c
[mengfh@red244 tt2]$ ls
head.h  t1.c  t1.o  t2.c  t2.o
[mengfh@red244 tt2]$ ll
×üó?á? 20
-rw-rw-r--    1 mengfh   mengfh         60  1??  9 07:58 head.h
-rw-rw-r--    1 mengfh   mengfh         73  1??  9 07:55 t1.c
-rw-rw-r--    1 mengfh   mengfh        832  1??  9 07:59 t1.o
-rw-rw-r--    1 mengfh   mengfh        100  1??  9 07:59 t2.c
-rw-rw-r--    1 mengfh   mengfh        868  1??  9 07:59 t2.o
[mengfh@red244 tt2]$ ar -curv libtt.a t1.o
a - t1.o===================================================================先增加t1.o
[mengfh@red244 tt2]$ ls
head.h  libtt.a  t1.c  t1.o  t2.c  t2.o
[mengfh@red244 tt2]$ cat >main.c
#include "head.h"
#include <stdio.h>

int main()
{
        fprintf(stderr,"this is main.c \n");
        display();

        return 0;
}
í?3?
[mengfh@red244 tt2]$ ls
head.h  libtt.a  main.c  t1.c  t1.o  t2.c  t2.o
[mengfh@red244 tt2]$ cc -o main main.c
/tmp/ccCAHz6s.o(.text+0x27): In function `main':
: undefined reference to `display'
collect2: ld returned 1 exit status
[mengfh@red244 tt2]$ cc -o main main.c -ltt
/usr/bin/ld: cannot find -ltt
collect2: ld returned 1 exit status
[mengfh@red244 tt2]$ cc -o main main.c -ltt -L.
[mengfh@red244 tt2]$ ls
head.h  libtt.a  main  main.c  t1.c  t1.o  t2.c  t2.o
[mengfh@red244 tt2]$ ./main
this is main.c
this is t1.c
[mengfh@red244 tt2]$ ar -curv libtt.a t2.o
a - t2.o================================================================再增加t2.o======>>>同一库文件里可以增加多个同名的函数在不同的模块中
[mengfh@red244 tt2]$ ls
head.h  libtt.a  main  main.c  t1.c  t1.o  t2.c  t2.o
[mengfh@red244 tt2]$ cc -o main main.c -ltt -L.
[mengfh@red244 tt2]$ ./main
this is main.c
this is t1.c

============================在libtt.a中加入了二个同名函数void display(),但是在进行编译时,其按顺序在静态库中查找
[mengfh@red244 tt2]$ cat t1.c t2.c
#include<stdio.h>

void display()
{
        fprintf(stderr,"this is t1.c\n");
}
#include<stdio.h>
void display()
{
        fprintf(stderr,"this is t2.c update display() function\n");

}
[mengfh@red244 tt2]$
[mengfh@red244 tt2]$
[mengfh@red244 tt2]$ rm main
[mengfh@red244 tt2]$ cc -o main main.c -ltt -L.
[mengfh@red244 tt2]$ ./main
this is main.c
this is t1.c
[mengfh@red244 tt2]$ ar -t *.a
t1.o
t2.o
[mengfh@red244 tt2]$ cat main.c
#include "head.h"
#include <stdio.h>

int main()
{
        fprintf(stderr,"this is main.c \n");
        display();

        return 0;
}
[mengfh@red244 tt2]$ ./main
this is main.c
this is t1.c


+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2:于是我经过一阵的分析,
m:该操作是在一个库中移动成员。当库中如果有若干模块有相同的符号定义(如函数定义),则成员的位置顺序很重要。如果没有指定任选项,任何指定的成员将移到库的最后。也可以使用'a','b',或'I'任选项移动到指定的位置。

进行如下的编译:


[mengfh@red244 tt2]$ ls
head.h  head.hbak  libtt.a  mai2  main  main.c  t1.c  t1.o  t2.c  t2.o
[mengfh@red244 tt2]$ ar -t *.a
t1.o
[mengfh@red244 tt2]$ cc -o main main.c  -ltt -L.
[mengfh@red244 tt2]$ ./main
this is main.c
this is t1.c
[mengfh@red244 tt2]$ ar -b libtt.a t2.o
ar: no operation specified
[mengfh@red244 tt2]$ ar -b  t1.so libtt.a t2.o
ar: no operation specified
[mengfh@red244 tt2]$ ar -bcvru t1.so libtt.a t2.o
a - t2.o
---------------------------------本是想写成ar -bcvru t1.o libtt.a t2.o======》即将t2.o加入到t1.o前,但是没有找到t1.so于是就是加在尾部了。
[mengfh@red244 tt2]$ ls
head.h  head.hbak  libtt.a  mai2  main  main.c  t1.c  t1.o  t2.c  t2.o
[mengfh@red244 tt2]$ ar -t *.a
t1.o
t2.o
[mengfh@red244 tt2]$ cc -o main main.c -ltt -L.
[mengfh@red244 tt2]$ ./main
this is main.c
this is t1.c

-----------------------------于是为完成ar -bcvru t1.o libtt.a t2.o(因为在前面造成了书写错误,于是手动进行更改模块顺序)
[mengfh@red244 tt2]$ ar -mb t1.o libtt.a t2.o
[mengfh@red244 tt2]$ ar -t *.a
t2.o
t1.o
[mengfh@red244 tt2]$ cc -o main  main .c -ltt -L.
cc: .c: ??óD???????t?ò????
[mengfh@red244 tt2]$ cc -o main  main.c -ltt -L.
[mengfh@red244 tt2]$ ./main
this is main.c
this is t2.c update display() function
[mengfh@red244 tt2]$

 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

3:于是我在main函数里又写了一个同名函数。
[mengfh@red244 tt2]$ ls
head.h  head.hbak  libtt.a  mai2  main  main.c  t1.c  t1.o  t2.c  t2.o
[mengfh@red244 tt2]$ vi main.c

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

void display();
int main()
{
        fprintf(stderr,"this is main.c \n");
        display();

        return 0;
}


void display()
{


        fprintf(stderr,"-------------------------in main\n");
}


"main.c" [ÒÑת»»] 20L, 231C ÒÑдÈë                                                                                 
[mengfh@red244 tt2]$ cc -o main  main.c -ltt -L.
[mengfh@red244 tt2]$ ./main
this is main.c
-------------------------in main
[mengfh@red244 tt2]$

 

=============================================================
得出结果:
        1:一个库文件里可以有多个同名函数。
        2:编译时在库文件中搜索函数时,与函数在库文件中的位置有关(模块之间的位置)
        3:非库文件中的函数优先与库文件被搜索,优先被调用。

 

转载于:https://my.oschina.net/3pgp/blog/189949

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值