文章目录
-
- [1. 静态库](#1. 静态库)
- [2. 动态库](#2. 动态库)
1. 静态库
c
复制代码
[gsm@VM-4-3-centos stdio]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 1550 Dec 11 15:18 my_stdio.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 15:18 my_stdio.h
[gsm@VM-4-3-centos stdio]$ touch my_string.h
[gsm@VM-4-3-centos stdio]$ touch my_string.c
[gsm@VM-4-3-centos stdio]$ vim my_string.h
[gsm@VM-4-3-centos stdio]$ vim my_string.c
[gsm@VM-4-3-centos stdio]$ cat my_stdio.h
#pragma once
#define SIZE 1024
#define FLUSH_NONE 0
#define FLUSH_LINE 1
#define FLUSH_FULL 2
struct IO_FILE
{
int flag; // 刷新方式
int fileno; // 文件描述符
char outbuffer[SIZE];
int cap;
int size;
// TODO
};
typedef struct IO_FILE mFILE;
mFILE* mfopen(const char* filename, const char* mode);
int mfwrite(const void* ptr, int num, mFILE* stream);
void mfflush(mFILE* stream);
void mfclose(mFILE* stream);
[gsm@VM-4-3-centos stdio]$ cat my_stdio.c
#include "my_stdio.h"
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
mFILE* mfopen(const char* filename, const char* mode)
{
int fd = -1;
if (strcmp(mode, "r") == 0)
{
fd = open(filename, O_RDONLY);
}
else if (strcmp(mode, "w") == 0)
{
fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
}
else if (strcmp(mode, "a") == 0)
{
fd = open(filename, O_CREAT | O_WRONLY | O_APPEND, 0666);
}
if (fd < 0)
{
return NULL;
}
mFILE* mf = (mFILE*)malloc(sizeof(mFILE));
if (!mf)
{
close(fd);
return NULL;
}
mf->fileno = fd;
mf->flag = FLUSH_LINE;
mf->size = 0;
mf->cap = SIZE;
return mf;
}
void mfflush(mFILE* stream)
{
if (stream->size > 0)
{
// 写到内核文件的文件缓冲区中!
write(stream->fileno, stream->outbuffer, stream->size);
// 刷新到外设
fsync(stream->fileno);
stream->size = 0;
}
}
int mfwrite(const void* ptr, int num, mFILE* stream)
{
// 1. 拷贝
memcpy(stream->outbuffer + stream->size, ptr, num);
stream->size += num;
// 2. 检测是否要刷新
if (stream->flag == FLUSH_LINE && stream->size > 0 && stream->outbuffer[stream->size - 1] == '\n')
{
mfflush(stream);
}
return num;
}
void mfclose(mFILE* stream)
{
if (stream->size > 0)
{
mfflush(stream);
}
close(stream->fileno);
}
[gsm@VM-4-3-centos stdio]$ cat my_string.h
#pragma once
int my_strlen(const char* s);
[gsm@VM-4-3-centos stdio]$ cat my_string.c
#include "my_string.h"
int my_strlen(const char* s)
{
const char* end = s;
while (*end != '\0')
{
end++;
}
return end - s;
}
[gsm@VM-4-3-centos stdio]$ gcc -c my_stdio.c
[gsm@VM-4-3-centos stdio]$ gcc -c my_string.c
[gsm@VM-4-3-centos stdio]$ ll
total 24
-rw-rw-r-- 1 gsm gsm 1550 Dec 11 15:18 my_stdio.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 15:18 my_stdio.h
-rw-rw-r-- 1 gsm gsm 2848 Dec 11 15:32 my_stdio.o
-rw-rw-r-- 1 gsm gsm 156 Dec 11 15:29 my_string.c
-rw-rw-r-- 1 gsm gsm 44 Dec 11 15:24 my_string.h
-rw-rw-r-- 1 gsm gsm 1272 Dec 11 15:32 my_string.o
[gsm@VM-4-3-centos stdio]$ ar -rc libmystdio.a my_stdio.o my_string.o
[gsm@VM-4-3-centos stdio]$ ll
total 32
-rw-rw-r-- 1 gsm gsm 4374 Dec 11 15:35 libmystdio.a
-rw-rw-r-- 1 gsm gsm 1550 Dec 11 15:18 my_stdio.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 15:18 my_stdio.h
-rw-rw-r-- 1 gsm gsm 2848 Dec 11 15:32 my_stdio.o
-rw-rw-r-- 1 gsm gsm 156 Dec 11 15:29 my_string.c
-rw-rw-r-- 1 gsm gsm 44 Dec 11 15:24 my_string.h
-rw-rw-r-- 1 gsm gsm 1272 Dec 11 15:32 my_string.o
[gsm@VM-4-3-centos stdio]$ sudo cp *.h /usr/include/
[sudo] password for gsm:
[gsm@VM-4-3-centos stdio]$ ls /usr//include/my_*
/usr//include/my_stdio.h /usr//include/my_string.h
[gsm@VM-4-3-centos stdio]$ sudo cp libmystdio.a /lib64/
[gsm@VM-4-3-centos stdio]$ ls /lib64/libmystdio.a
/lib64/libmystdio.a
[gsm@VM-4-3-centos stdio]$ cd ..
[gsm@VM-4-3-centos lesson20]$ ll
total 12
-rw-rw-r-- 1 gsm gsm 496 Dec 11 15:18 main.c
drwxrwxr-x 2 gsm gsm 4096 Dec 11 15:37 other
drwxrwxr-x 2 gsm gsm 4096 Dec 11 15:35 stdio
[gsm@VM-4-3-centos lesson20]$ cd other/
[gsm@VM-4-3-centos other]$ touch main.c
[gsm@VM-4-3-centos other]$ vim main.c
[gsm@VM-4-3-centos other]$ cat main.c
#include <my_stdio.h>
#include <my_string.h>
#include <stdio.h>
int main()
{
const char* s = "abcdefg";
printf("%s: %d\n", s, my_strlen(s));
mFILE* fp = mfopen("./log.txt", "a");
if (fp == NULL)
{
return 1;
}
mfwrite(s, my_strlen(s), fp);
mfwrite(s, my_strlen(s), fp);
mfwrite(s, my_strlen(s), fp);
mfclose(fp);
return 0;
}
[gsm@VM-4-3-centos other]$ gcc main.c
/tmp/cctcrPrB.o: In function `main':
main.c:(.text+0x18): undefined reference to `my_strlen'
main.c:(.text+0x3f): undefined reference to `mfopen'
main.c:(.text+0x5d): undefined reference to `my_strlen'
main.c:(.text+0x71): undefined reference to `mfwrite'
main.c:(.text+0x7d): undefined reference to `my_strlen'
main.c:(.text+0x91): undefined reference to `mfwrite'
main.c:(.text+0x9d): undefined reference to `my_strlen'
main.c:(.text+0xb1): undefined reference to `mfwrite'
main.c:(.text+0xbd): undefined reference to `mfclose'
collect2: error: ld returned 1 exit status
[gsm@VM-4-3-centos other]$ ls /lib64
audit libdevmapper-event.so.1.02 libicudata.so.50.2 libnl-nf-3.so.200 libresolv.a libwayland-server.so.0.1.0
bind9-export libdevmapper.so.1.02 libicui18n.so.50 libnl-nf-3.so.200.23.0 libresolv.so libwebpmux.so.0
cifs-utils libdev_mgt.a libicui18n.so.50.2 libnl-route-3.so libresolv.so.2 libwebpmux.so.0.0.0
cmake libdhcpctl.so.0 libicuio.so.50 libnl-route-3.so.200 librpcsvc.a libwebp.so.4
cracklib_dict.hwm libdhcpctl.so.0.0.0 libicuio.so.50.2 libnl-route-3.so.200.23.0 librpmbuild.so.3 libwebp.so.4.0.2
cracklib_dict.pwd libdl-2.17.so libicule.so.50 libnl.so.1 librpmbuild.so.3.2.2 libwrap.so.0
cracklib_dict.pwi libdl.a libicule.so.50.2 libnl.so.1.1.4 librpmio.so.3 libwrap.so.0.7.6
crt1.o libdl.so libiculx.so.50 libnl-xfrm-3.so librpmio.so.3.2.2 libX11.so.6
crti.o libdl.so.2 libiculx.so.50.2 libnl-xfrm-3.so.200 librpmsign.so.1 libX11.so.6.3.0
crtn.o libdns.so.1102 libicutest.so.50 libnl-xfrm-3.so.200.23.0 librpmsign.so.1.2.2 libX11-xcb.so.1
dbus-1 libdns.so.1102.1.2 libicutest.so.50.2 libnm.so.0 librpm.so.3 libX11-xcb.so.1.0.0
device-mapper libdrm_amdgpu.so.1 libicutu.so.50 libnm.so.0.1.0 librpm.so.3.2.2 libXau.so.6
dyninst libdrm_amdgpu.so.1.0.0 libicutu.so.50.2 libnsl-2.17.so librt-2.17.so libXau.so.6.0.0
ebtables libdrm_intel.so.1 libicuuc.so.50 libnsl.a librt.a libxcb-composite.so.0
elfutils libdrm_intel.so.1.0.0 libicuuc.so.50.2 libnsl.so librt.so libxcb-composite.so.0.0.0
fipscheck libdrm_nouveau.so.2 libidn.so.11 libnsl.so.1 librt.so.1 libxcb-damage.so.0
games libdrm_nouveau.so.2.0.0 libidn.so.11.6.11 libnspr4.so libsasl2.so.3 libxcb-damage.so.0.0.0
gconv libdrm_radeon.so.1 libieee.a libnss3.so libsasl2.so.3.0.0 libxcb-dpms.so.0
gcrt1.o libdrm_radeon.so.1.0.1 libip4tc.so libnssckbi.so libsatyr.so.3 libxcb-dpms.so.0.0.0
gdk-pixbuf-2.0 libdrm.so.2 libip4tc.so.0 libnss_compat-2.17.so libsatyr.so.3.0.0 libxcb-dri2.so.0
gettext libdrm.so.2.4.0 libip4tc.so.0.1.0 libnss_compat.so libseccomp.so.2 libxcb-dri2.so.0.0.0
gio libdw-0.176.so libip6tc.so libnss_compat.so.2 libseccomp.so.2.3.1 libxcb-dri3.so.0
girepository-1.0 libdwarf.so.0 libip6tc.so.0 libnss_db-2.17.so libSegFault.so libxcb-dri3.so.0.0.0
glib-2.0 libdwarf.so.0.20130207.0 libip6tc.so.0.1.0 libnssdbm3.chk libselinux.so libxcb-glx.so.0
golang libdw.so libipset.so.13 libnssdbm3.so libselinux.so.1 libxcb-glx.so.0.0.0
gtk-2.0 libdw.so.1 libipset.so.13.1.0 libnss_db.so libsemanage.so.1 libxcb-present.so.0
krb5 libe2p.so.2 libiptc.so libnss_db.so.2 libsepol.so libxcb-present.so.0.0.0
ld-2.17.so libe2p.so.2.3 libiptc.so.0 libnss_dns-2.17.so libsepol.so.1 libxcb-randr.so.0
ld-linux-x86-64.so.2 libebl.a libiptc.so.0.0.0 libnss_dns.so libsgmon.so libxcb-randr.so.0.1.0
libabrt_dbus.so.0 libebtc.so libirs.so.160 libnss_dns.so.2 libsgmon.so.1.3 libxcb-record.so.0
libabrt_dbus.so.0.0.1 libedit.so.0 libirs.so.160.0.5 libnss_files-2.17.so libsgutils2.so.2 libxcb-record.so.0.0.0
libabrt.so.0 libedit.so.0.0.42 libisccc.so.160 libnss_files.so libsgutils2.so.2.0.0 libxcb-render.so.0
libabrt.so.0.0.1 libEGL_mesa.so.0 libisccc.so.160.0.3 libnss_files.so.2 libslang.so.2 libxcb-render.so.0.0.0
libacl.so.1 libEGL_mesa.so.0.0.0 libisccfg.so.160 libnss_hesiod-2.17.so libslang.so.2.2.4 libxcb-res.so.0
libacl.so.1.1.0 libEGL.so.1 libisccfg.so.160.2.1 libnss_hesiod.so libslapi-2.4.so.2 libxcb-res.so.0.0.0
libaio.so.1 libEGL.so.1.1.0 libiscsi.so.0 libnss_hesiod.so.2 libslapi-2.4.so.2.10.7 libxcb-screensaver.so.0
libaio.so.1.0.0 libelf-0.176.so libisc.so.169 libnss_myhostname.so.2 libsmartcols.so.1 libxcb-screensaver.so.0.0.0
libaio.so.1.0.1 libelf.so libisc.so.169.0.3 libnss_mymachines.so.2 libsmartcols.so.1.1.0 libxcb-shape.so.0
libanl-2.17.so libelf.so.1 libjansson.so.4 libnss_nis-2.17.so libsmime3.so libxcb-shape.so.0.0.0
libanl.a libestr.so.0 libjansson.so.4.10.0 libnss_nisplus-2.17.so libsnappy.so.1 libxcb-shm.so.0
libanl.so libestr.so.0.0.0 libjasper.so.1 libnss_nisplus.so libsnappy.so.1.1.4 libxcb-shm.so.0.0.0
libanl.so.1 libexpat.so.1 libjasper.so.1.0.0 libnss_nisplus.so.2 libsoftokn3.chk libxcb.so.1
libarchive.so.13 libexpat.so.1.6.0 libjbig85.so.2.0 libnss_nis.so libsoftokn3.so libxcb.so.1.1.0
libarchive.so.13.1.2 libext2fs.so.2 libjbig.so.2.0 libnss_nis.so.2 libsqlite3.so.0 libxcb-sync.so.1
libasm-0.176.so libext2fs.so.2.4 libjemalloc.so.1 libnsspem.so libsqlite3.so.0.8.6 libxcb-sync.so.1.0.0
libasm.so libfa.so.1 libjpeg.so.62 libnss_sss.so.2 libssh2.so.1 libxcb-xevie.so.0
libasm.so.1 libfa.so.1.4.1 libjpeg.so.62.1.0 libnsssysinit.so libssh2.so.1.0.1 libxcb-xevie.so.0.0.0
libasound.so.2 libfastjson.so.4 libjson-c.so.2 libnssutil3.so libssl3.so libxcb-xf86dri.so.0
libasound.so.2.0.0 libfastjson.so.4.0.0 libjson-c.so.2.0.1 libnuma.so libssl.so libxcb-xf86dri.so.0.0.0
libasprintf.so.0 libffi.so.6 libjson.so.0 libnuma.so.1 libssl.so.10 libxcb-xfixes.so.0
libasprintf.so.0.0.0 libffi.so.6.0.1 libjson.so.0.1.0 libnuma.so.1.0.0 libssl.so.1.0.2k libxcb-xfixes.so.0.0.0
libassuan.so.0 libfipscheck.so.1 libk5crypto.so libomapi.so.0 libsss_idmap.so.0 libxcb-xinerama.so.0
libassuan.so.0.4.0 libfipscheck.so.1.2.1 libk5crypto.so.3 libomapi.so.0.0.0 libsss_idmap.so.0.5.1 libxcb-xinerama.so.0.0.0
libatk-1.0.so.0 libfontconfig.so.1 libk5crypto.so.3.1 libopcodes-2.27-44.base.el7_9.1.so libsss_nss_idmap.so.0 libxcb-xinput.so.0
libatk-1.0.so.0.22810.1 libfontconfig.so.1.11.1 libkadm5clnt_mit.so libopts.so.25 libsss_nss_idmap.so.0.5.0 libxcb-xinput.so.0.1.0
libatomic.so.1 libform.so.5 libkadm5clnt_mit.so.11 libopts.so.25.15.0 libss.so.2 libxcb-xkb.so.1
libatomic.so.1.0.0 libform.so.5.9 libkadm5clnt_mit.so.11.0 libp11-kit.so.0 libss.so.2.0 libxcb-xkb.so.1.0.0
libattr.so.1 libformw.so.5 libkadm5clnt.so libp11-kit.so.0.3.0 libstdc++.so.6 libxcb-xselinux.so.0
libattr.so.1.1.0 libformw.so.5.9 libkadm5srv_mit.so libpamc.so.0 libstdc++.so.6.0.19 libxcb-xselinux.so.0.0.0
libaudit.so.1 libfreebl3.chk libkadm5srv_mit.so.11 libpamc.so.0.82.1 libstoragemgmt.so.1 libxcb-xtest.so.0
libaudit.so.1.0.0 libfreebl3.so libkadm5srv_mit.so.11.0 libpam_misc.so.0 libstoragemgmt.so.1.8.1 libxcb-xtest.so.0.0.0
libaugeas.so.0 libfreeblpriv3.chk libkadm5srv.so libpam_misc.so.0.82.0 libsysfs.so.2 libxcb-xvmc.so.0
libaugeas.so.0.20.0 libfreeblpriv3.so libkdb5.so libpam.so.0 libsysfs.so.2.0.1 libxcb-xvmc.so.0.0.0
libauparse.so.0 libfreetype.so.6 libkdb5.so.8 libpam.so.0.83.1 libsystemd-daemon.so libxcb-xv.so.0
libauparse.so.0.0.0 libfreetype.so.6.14.0 libkdb5.so.8.0 libpanel.so.5 libsystemd-daemon.so.0 libxcb-xv.so.0.0.0
libavahi-client.so.3 libfribidi.so.0 libkeyutils.so libpanel.so.5.9 libsystemd-daemon.so.0.0.12 libXcomposite.so.1
libavahi-client.so.3.2.9 libfribidi.so.0.4.0 libkeyutils.so.1 libpanelw.so.5 libsystemd-id128.so libXcomposite.so.1.0.0
libavahi-common.so.3 libfuse.so.2 libkeyutils.so.1.5 libpanelw.so.5.9 libsystemd-id128.so.0 libXcursor.so.1
libavahi-common.so.3.5.3 libfuse.so.2.9.2 libkmod.so.2 libpango-1.0.so.0 libsystemd-id128.so.0.0.28 libXcursor.so.1.0.2
libbfd-2.27-44.base.el7_9.1.so libg.a libkmod.so.2.2.10 libpango-1.0.so.0.4200.3 libsystemd-journal.so libXdamage.so.1
libbind9.so.160 libgailutil.so.18 libkms.so.1 libpangocairo-1.0.so.0 libsystemd-journal.so.0 libXdamage.so.1.1.0
libbind9.so.160.0.8 libgailutil.so.18.0.1 libkms.so.1.0.0 libpangocairo-1.0.so.0.4200.3 libsystemd-journal.so.0.11.5 libXext.so.6
libblkid.so.1 libgbm.so.1 libkrad.so libpangoft2-1.0.so.0 libsystemd-login.so libXext.so.6.4.0
libblkid.so.1.1.0 libgbm.so.1.0.0 libkrad.so.0 libpangoft2-1.0.so.0.4200.3 libsystemd-login.so.0 libXfixes.so.3
libboost_date_time-mt.so.1.53.0 libgcc_s-4.8.5-20150702.so.1 libkrad.so.0.0 libpangoxft-1.0.so.0 libsystemd-login.so.0.9.3 libXfixes.so.3.1.0
libboost_date_time.so.1.53.0 libgcc_s.so.1 libkrb5.so libpangoxft-1.0.so.0.4200.3 libsystemd.so libXft.so.2
libboost_filesystem-mt.so.1.53.0 libgcrypt.so.11 libkrb5.so.3 libparted-fs-resize.so libsystemd.so.0 libXft.so.2.3.2
libboost_filesystem.so.1.53.0 libgcrypt.so.11.8.2 libkrb5.so.3.3 libparted-fs-resize.so.0 libsystemd.so.0.6.0 libXinerama.so.1
libboost_regex-mt.so.1.53.0 libgdbm_compat.so.4 libkrb5support.so libparted-fs-resize.so.0.0.0 libtar.so.1 libXinerama.so.1.0.0
libboost_regex.so.1.53.0 libgdbm_compat.so.4.0.0 libkrb5support.so.0 libparted.so.2 libtar.so.1.2.11 libXi.so.6
libboost_system-mt.so.1.53.0 libgdbm.so.4 libkrb5support.so.0.1 libparted.so.2.0.0 libtasn1.so.6 libXi.so.6.1.0
libboost_system.so.1.53.0 libgdbm.so.4.0.0 liblber-2.4.so.2 libpcap.so.1 libtasn1.so.6.5.3 libxml2.so.2
libboost_thread-mt.so.1.53.0 libgdk_pixbuf-2.0.so.0 liblber-2.4.so.2.10.7 libpcap.so.1.5.3 libtcl8.5.so libxml2.so.2.9.1
libBrokenLocale-2.17.so libgdk_pixbuf-2.0.so.0.3612.0 libldap-2.4.so.2 libpciaccess.so.0 libteamdctl.so.0 libxmlrpc_abyss.so.3
libBrokenLocale.a libgdk_pixbuf_xlib-2.0.so.0 libldap-2.4.so.2.10.7 libpciaccess.so.0.11.1 libteamdctl.so.0.1.5 libxmlrpc_abyss.so.3.32
libBrokenLocale.so libgdk_pixbuf_xlib-2.0.so.0.3612.0 libldap_r-2.4.so.2 libpci.so.3 libteam.so.5 libxmlrpc_client.so.3
libBrokenLocale.so.1 libgdk-x11-2.0.so.0 libldap_r-2.4.so.2.10.7 libpci.so.3.5.1 libteam.so.5.6.0 libxmlrpc_client.so.3.32
libbsd.a libgdk-x11-2.0.so.0.2400.31 liblua-5.1.so libpcprofile.so libtermkey.so.1 libxmlrpc_server_abyss.so.3
libbsd-compat.a libGeoIP.so.1 liblvm2app.so.2.2 libpcre16.so libtermkey.so.1.14.0 libxmlrpc_server_abyss.so.3.32
libbtrfs.so.0 libGeoIP.so.1.5.0 liblvm2cmd.so.2.02 libpcre16.so.0 libthai.so.0 libxmlrpc_server_cgi.so.3
libbtrfs.so.0.1 libgettextlib-0.19.8.1.so liblwres.so.160 libpcre16.so.0.2.0 libthai.so.0.1.6 libxmlrpc_server_cgi.so.3.32
libbz2.so.1 libgettextpo.so.0 liblwres.so.160.0.2 libpcre32.so libthread_db-1.0.so libxmlrpc_server.so.3
libbz2.so.1.0.6 libgettextpo.so.0.5.4 liblz4.so.1 libpcre32.so.0 libthread_db.so libxmlrpc_server.so.3.32
libc-2.17.so libgettextsrc-0.19.8.1.so liblz4.so.1.8.3 libpcre32.so.0.0.0 libthread_db.so.1 libxmlrpc.so.3
libc.a libgfortran.so.3 liblzma.so libpcrecpp.so libtic.so.5 libxmlrpc.so.3.32
libcairo-script-interpreter.so.2 libgfortran.so.3.0.0 liblzma.so.5 libpcrecpp.so.0 libtic.so.5.9 libxmlrpc_util.so.3
libcairo-script-interpreter.so.2.11512.0 libgio-2.0.so liblzma.so.5.2.2 libpcrecpp.so.0.0.0 libtiff.so.5 libxmlrpc_util.so.3.32
libcairo.so.2 libgio-2.0.so.0 liblzo2.so.2 libpcreposix.so libtiff.so.5.2.0 libxpmem.so.0
libcairo.so.2.11512.0 libgio-2.0.so.0.5600.1 liblzo2.so.2.0.0 libpcreposix.so.0 libtiffxx.so.5 libxpmem.so.0.0.0
libcap-ng.so.0 libgirepository-1.0.so.1 libm-2.17.so libpcreposix.so.0.0.1 libtiffxx.so.5.2.0 libXrandr.so.2
libcap-ng.so.0.0.0 libgirepository-1.0.so.1.0.0 libm.a libpcre.so libtinfo.so.5 libXrandr.so.2.2.0
libcap.so.2 libglapi.so.0 libmagic.so.1 libpcre.so.1 libtinfo.so.5.9 libXrender.so.1
libcap.so.2.22 libglapi.so.0.0.0 libmagic.so.1.0.0 libpcre.so.1.2.0 libtirpc.so.1 libXrender.so.1.3.0
libcidn-2.17.so libGLdispatch.so.0 libmcheck.a libpipeline.so.1 libtirpc.so.1.0.10 libxshmfence.so.1
libcidn.so libGLdispatch.so.0.0.0 libmemusage.so libpipeline.so.1.2.3 libtk8.5.so libxshmfence.so.1.0.0
libcidn.so.1 libglib-2.0.so libmenu.so.5 libpixman-1.so.0 libtomcrypt.so.0 libxtables.so
libcmdif.a libglib-2.0.so.0 libmenu.so.5.9 libpixman-1.so.0.34.0 libtomcrypt.so.0.0.117 libxtables.so.10
libc_nonshared.a libglib-2.0.so.0.5600.1 libmenuw.so.5 libplc4.so libtommath.so.0 libxtables.so.10.0.0
libcom_err.so libGL.so.1 libmenuw.so.5.9 libplds4.so libtommath.so.0.0.41 libXxf86vm.so.1
libcom_err.so.2 libGL.so.1.7.0 libmnl.so libply-boot-client.so.2 libtools_layouts.a libXxf86vm.so.1.0.0
libcom_err.so.2.1 libGLX_mesa.so.0 libmnl.so.0 libply-boot-client.so.2.1.0 libucm.so libyajl.so.2
libconfig.so.9 libGLX_mesa.so.0.0.0 libmnl.so.0.1.0 libply.so.2 libucm.so.0 libyajl.so.2.0.4
libconfig++.so.9 libGLX.so.0 libmodman.so.1 libply.so.2.1.0 libucm.so.0.0.0 libyaml-0.so.2
libconfig.so.9.1.3 libGLX.so.0.0.0 libmodman.so.1.0.0 libply-splash-core.so.2 libucp.so libyaml-0.so.2.0.4
libconfig++.so.9.1.3 libGLX_system.so.0 libmount.so.1 libply-splash-core.so.2.1.0 libucp.so.0 libz.so
libcpupower.so.0 libgmodule-2.0.so libmount.so.1.1.0 libpng15.so.15 libucp.so.0.0.0 libz.so.1
libcpupower.so.0.0.0 libgmodule-2.0.so.0 libmozjs-17.0.so libpng15.so.15.13.0 libucs.so libz.so.1.2.7
libcrack.so.2 libgmodule-2.0.so.0.5600.1 libmpc.so.3 libpolkit-agent-1.so.0 libucs.so.0 lua
libcrack.so.2.9.0 libgmp.so.10 libmpc.so.3.0.0 libpolkit-agent-1.so.0.0.0 libucs.so.0.0.0 man-db
libcroco-0.6.so.3 libgmp.so.10.2.0 libmpfr.so.4 libpolkit-gobject-1.so.0 libuct.so Mcrt1.o
libcroco-0.6.so.3.0.1 libgmpxx.so.4 libmpfr.so.4.1.1 libpolkit-gobject-1.so.0.0.0 libuct.so.0 mft
libcrypt-2.17.so libgmpxx.so.4.4.0 libmsgpackc.so.2 libpopt.so.0 libuct.so.0.0.0 mstflint
libcrypt.a libgobject-2.0.so libmsgpackc.so.2.0.0 libpopt.so.0.0.0 libudev.so mysql
libcrypto.so libgobject-2.0.so.0 libm.so libprocps.so.4 libudev.so.1 NetworkManager
libcrypto.so.10 libgobject-2.0.so.0.5600.1 libm.so.6 libprocps.so.4.0.0 libudev.so.1.6.2 nss
libcrypto.so.1.0.2k libgomp.so.1 libmtcr_ul.a libproxy libulockmgr.so.1 openssl
libcryptsetup.so.12 libgomp.so.1.0.0 libmystdio.a libproxy.so.1 libulockmgr.so.1.0.1 p11-kit-proxy.so
libcryptsetup.so.12.3.0 libgpg-error.so.0 libncurses.so.5 libproxy.so.1.0.0 libunibilium.so.4 p11-kit-trust.so
libcryptsetup.so.4 libgpg-error.so.0.10.0 libncurses++.so.5 libpthread-2.17.so libunibilium.so.4.0.0 perl5
libcryptsetup.so.4.7.0 libgpgme-pthread.so.11 libncurses.so.5.9 libpthread.a libunistring.so.0 pkcs11
libcrypt.so libgpgme-pthread.so.11.8.1 libncurses++.so.5.9 libpthread_nonshared.a libunistring.so.0.1.2 pkgconfig
libcrypt.so.1 libgpgme.so.11 libncurses++w.so.5 libpthread.so libusb-1.0.so.0 plymouth
libc.so libgpgme.so.11.8.1 libncursesw.so.5 libpthread.so.0 libusb-1.0.so.0.1.0 pm-utils
libc.so.6 libgpm.so.2 libncurses++w.so.5.9 libpth.so.20 libuser python2.7
libc_stubs.a libgpm.so.2.1.0 libncursesw.so.5.9 libpth.so.20.0.27 libuser.so.1 python3.6
libcupscgi.so.1 libgraphite2.so.3 libndp.so.0 libpwquality.so.1 libuser.so.1.5.0 rsyslog
libcupsimage.so.2 libgraphite2.so.3.0.1 libndp.so.0.0.0 libpwquality.so.1.0.2 libustr-1.0.so.1 rtkaio
libcupsmime.so.1 libgssapi_krb5.so libnetfilter_conntrack.so.3 libpyglib-2.0-python.so libustr-1.0.so.1.0.4 sasl2
libcupsppdc.so.1 libgssapi_krb5.so.2 libnetfilter_conntrack.so.3.6.0 libpyglib-2.0-python.so.0 libutempter.so.0 Scrt1.o
libcups.so.2 libgssapi_krb5.so.2.2 libnewt.so.0.52 libpyglib-2.0-python.so.0.0.0 libutempter.so.1.1.6 security
libcurl.so.4 libgssrpc.so libnewt.so.0.52.15 libpython2.7.so libutil-2.17.so sse2
libcurl.so.4.3.0 libgssrpc.so.4 libnfnetlink.so.0 libpython2.7.so.1.0 libutil.a sssd
libdaemon.so.0 libgssrpc.so.4.2 libnfnetlink.so.0.2.0 libpython3.6m.so.1.0 libutil.so systemtap
libdaemon.so.0.5.0 libgthread-2.0.so libnl libpython3.so libutil.so.1 tc
libdb-5.3.so libgthread-2.0.so.0 libnl-3.so libqrencode.so.3 libuuid.so.1 tcl8.5
libdb-5.so libgthread-2.0.so.0.5600.1 libnl-3.so.200 libqrencode.so.3.4.1 libuuid.so.1.3.0 tk8.5
libdb.so libgtk-x11-2.0.so.0 libnl-3.so.200.23.0 libquadmath.so.0 libuv tls
libdbus-1.so.3 libgtk-x11-2.0.so.0.2400.31 libnl-cli-3.so libquadmath.so.0.0.0 libuv.so.1 ucx
libdbus-1.so.3.14.14 libhandle.so.1 libnl-cli-3.so.200 libreadline.so.6 libuv.so.1.0.0 umad2sim
libdbus-glib-1.so.2 libhandle.so.1.0.3 libnl-cli-3.so.200.23.0 libreadline.so.6.2 libverto.so valgrind
libdbus-glib-1.so.2.2.2 libharfbuzz.so.0 libnl-genl-3.so libreg_access.a libverto.so.1 X11
libdevmapper-event-lvm2mirror.so libharfbuzz.so.0.10705.0 libnl-genl-3.so.200 libreg.so libverto.so.1.0.0 xtables
libdevmapper-event-lvm2raid.so libhistory.so.6 libnl-genl-3.so.200.23.0 libreport.so.0 libvterm.so.0
libdevmapper-event-lvm2snapshot.so libhistory.so.6.2 libnl-idiag-3.so libreport.so.0.0.1 libvterm.so.0.0.0
libdevmapper-event-lvm2.so.2.02 libhunspell-1.3.so.0 libnl-idiag-3.so.200 libreport-web.so.0 libwayland-client.so.0
libdevmapper-event-lvm2thin.so libhunspell-1.3.so.0.0.0 libnl-idiag-3.so.200.23.0 libreport-web.so.0.0.1 libwayland-client.so.0.3.0
libdevmapper-event-lvm2vdo.so libicudata.so.50 libnl-nf-3.so libresolv-2.17.so libwayland-server.so.0
[gsm@VM-4-3-centos other]$ gcc main.c -lmystdio
[gsm@VM-4-3-centos other]$ ll
total 16
-rwxrwxr-x 1 gsm gsm 8952 Dec 11 16:14 a.out
-rw-rw-r-- 1 gsm gsm 382 Dec 11 15:59 main.c
[gsm@VM-4-3-centos other]$ ./a.out
abcdefg: 7
[gsm@VM-4-3-centos other]$ ll
total 20
-rwxrwxr-x 1 gsm gsm 8952 Dec 11 16:14 a.out
-rw-rw-r-- 1 gsm gsm 21 Dec 11 16:15 log.txt
-rw-rw-r-- 1 gsm gsm 382 Dec 11 15:59 main.c
[gsm@VM-4-3-centos other]$ cat log.txt
abcdefgabcdefgabcdefg[gsm@VM-4-3-centos other]$ gcc main.c -o main -lmystdio
[gsm@VM-4-3-centos other]$ ll
total 32
-rwxrwxr-x 1 gsm gsm 8952 Dec 11 16:14 a.out
-rw-rw-r-- 1 gsm gsm 21 Dec 11 16:15 log.txt
-rwxrwxr-x 1 gsm gsm 8952 Dec 11 16:21 main
-rw-rw-r-- 1 gsm gsm 382 Dec 11 15:59 main.c
[gsm@VM-4-3-centos other]$ ./main
abcdefg: 7
[gsm@VM-4-3-centos other]$ cat log.txt
abcdefgabcdefgabcdefgabcdefgabcdefgabcdefg[gsm@VM-4-3-centos other]$
c
复制代码
[gsm@VM-4-3-centos stdio]$ ll
total 32
-rw-rw-r-- 1 gsm gsm 4374 Dec 11 15:35 libmystdio.a
-rw-rw-r-- 1 gsm gsm 1550 Dec 11 15:18 my_stdio.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 15:18 my_stdio.h
-rw-rw-r-- 1 gsm gsm 2848 Dec 11 15:32 my_stdio.o
-rw-rw-r-- 1 gsm gsm 156 Dec 11 15:29 my_string.c
-rw-rw-r-- 1 gsm gsm 44 Dec 11 15:24 my_string.h
-rw-rw-r-- 1 gsm gsm 1272 Dec 11 15:32 my_string.o
[gsm@VM-4-3-centos stdio]$ cp *.h ../other/
[gsm@VM-4-3-centos stdio]$ cp *.a ../other/
[gsm@VM-4-3-centos stdio]$ cd ../other/
[gsm@VM-4-3-centos other]$ ll
total 20
-rw-rw-r-- 1 gsm gsm 4374 Dec 11 16:27 libmystdio.a
-rw-rw-r-- 1 gsm gsm 382 Dec 11 15:59 main.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 16:27 my_stdio.h
-rw-rw-r-- 1 gsm gsm 44 Dec 11 16:27 my_string.h
[gsm@VM-4-3-centos other]$ vim main.c
[gsm@VM-4-3-centos other]$ cat main.c
#include "my_stdio.h"
#include "my_string.h"
#include <stdio.h>
int main()
{
const char* s = "abcdefg";
printf("%s: %d\n", s, my_strlen(s));
mFILE* fp = mfopen("./log.txt", "a");
if (fp == NULL)
{
return 1;
}
mfwrite(s, my_strlen(s), fp);
mfwrite(s, my_strlen(s), fp);
mfwrite(s, my_strlen(s), fp);
mfclose(fp);
return 0;
}
[gsm@VM-4-3-centos other]$ gcc main.c -o main
/tmp/ccUv5qgV.o: In function `main':
main.c:(.text+0x18): undefined reference to `my_strlen'
main.c:(.text+0x3f): undefined reference to `mfopen'
main.c:(.text+0x5d): undefined reference to `my_strlen'
main.c:(.text+0x71): undefined reference to `mfwrite'
main.c:(.text+0x7d): undefined reference to `my_strlen'
main.c:(.text+0x91): undefined reference to `mfwrite'
main.c:(.text+0x9d): undefined reference to `my_strlen'
main.c:(.text+0xb1): undefined reference to `mfwrite'
main.c:(.text+0xbd): undefined reference to `mfclose'
collect2: error: ld returned 1 exit status
[gsm@VM-4-3-centos other]$ gcc main.c -o main -lmystdio
/usr/bin/ld: cannot find -lmystdio
collect2: error: ld returned 1 exit status
[gsm@VM-4-3-centos other]$ gcc main.c -o main -L.
/tmp/cc2nD4vE.o: In function `main':
main.c:(.text+0x18): undefined reference to `my_strlen'
main.c:(.text+0x3f): undefined reference to `mfopen'
main.c:(.text+0x5d): undefined reference to `my_strlen'
main.c:(.text+0x71): undefined reference to `mfwrite'
main.c:(.text+0x7d): undefined reference to `my_strlen'
main.c:(.text+0x91): undefined reference to `mfwrite'
main.c:(.text+0x9d): undefined reference to `my_strlen'
main.c:(.text+0xb1): undefined reference to `mfwrite'
main.c:(.text+0xbd): undefined reference to `mfclose'
collect2: error: ld returned 1 exit status
[gsm@VM-4-3-centos other]$ gcc main.c -o main -L. -lmystdio
[gsm@VM-4-3-centos other]$ ll
total 32
-rw-rw-r-- 1 gsm gsm 4374 Dec 11 16:27 libmystdio.a
-rwxrwxr-x 1 gsm gsm 8952 Dec 11 16:41 main
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 16:27 my_stdio.h
-rw-rw-r-- 1 gsm gsm 44 Dec 11 16:27 my_string.h
[gsm@VM-4-3-centos other]$ ./main
abcdefg: 7
c
复制代码
[gsm@VM-4-3-centos stdio]$ ll
total 20
-rw-rw-r-- 1 gsm gsm 220 Dec 11 17:02 Makefile
-rw-rw-r-- 1 gsm gsm 1550 Dec 11 15:18 my_stdio.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 15:18 my_stdio.h
-rw-rw-r-- 1 gsm gsm 156 Dec 11 15:29 my_string.c
-rw-rw-r-- 1 gsm gsm 44 Dec 11 15:24 my_string.h
[gsm@VM-4-3-centos stdio]$ vim Makefile
[gsm@VM-4-3-centos stdio]$ cat Makefile
libmystdio.a:my_stdio.o my_string.o
@ar -rc $@ $^
@echo "build $^ to $@ ... done"
%.o:%.c
@gcc -c $<
@echo "compiling $< to $@ ... done"
.PHONY:clean
clean:
@rm -rf *.a *.o stdc*
@echo "clean ... done"
.PHONY:output
output:
@mkdir -p stdc/include
@mkdir -p stdc/lib
@cp -f *.h stdc/include
@cp -f *.a stdc/lib
@tar czf stdc.tgz stdc
@echo "output stdc ... done"
[gsm@VM-4-3-centos stdio]$ make
compiling my_stdio.c to my_stdio.o ... done
compiling my_string.c to my_string.o ... done
build my_stdio.o my_string.o to libmystdio.a ... done
[gsm@VM-4-3-centos stdio]$ ll
total 36
-rw-rw-r-- 1 gsm gsm 4374 Dec 11 17:17 libmystdio.a
-rw-rw-r-- 1 gsm gsm 376 Dec 11 17:17 Makefile
-rw-rw-r-- 1 gsm gsm 1550 Dec 11 15:18 my_stdio.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 15:18 my_stdio.h
-rw-rw-r-- 1 gsm gsm 2848 Dec 11 17:17 my_stdio.o
-rw-rw-r-- 1 gsm gsm 156 Dec 11 15:29 my_string.c
-rw-rw-r-- 1 gsm gsm 44 Dec 11 15:24 my_string.h
-rw-rw-r-- 1 gsm gsm 1272 Dec 11 17:17 my_string.o
[gsm@VM-4-3-centos stdio]$ make output
output stdc ... done
[gsm@VM-4-3-centos stdio]$ ll
total 44
-rw-rw-r-- 1 gsm gsm 4374 Dec 11 17:17 libmystdio.a
-rw-rw-r-- 1 gsm gsm 376 Dec 11 17:17 Makefile
-rw-rw-r-- 1 gsm gsm 1550 Dec 11 15:18 my_stdio.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 15:18 my_stdio.h
-rw-rw-r-- 1 gsm gsm 2848 Dec 11 17:17 my_stdio.o
-rw-rw-r-- 1 gsm gsm 156 Dec 11 15:29 my_string.c
-rw-rw-r-- 1 gsm gsm 44 Dec 11 15:24 my_string.h
-rw-rw-r-- 1 gsm gsm 1272 Dec 11 17:17 my_string.o
drwxrwxr-x 4 gsm gsm 4096 Dec 11 17:17 stdc
-rw-rw-r-- 1 gsm gsm 1724 Dec 11 17:17 stdc.tgz
[gsm@VM-4-3-centos stdio]$ make clean
clean ... done
[gsm@VM-4-3-centos stdio]$ ll
total 20
-rw-rw-r-- 1 gsm gsm 376 Dec 11 17:17 Makefile
-rw-rw-r-- 1 gsm gsm 1550 Dec 11 15:18 my_stdio.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 15:18 my_stdio.h
-rw-rw-r-- 1 gsm gsm 156 Dec 11 15:29 my_string.c
-rw-rw-r-- 1 gsm gsm 44 Dec 11 15:24 my_string.h
[gsm@VM-4-3-centos stdio]$ make
compiling my_stdio.c to my_stdio.o ... done
compiling my_string.c to my_string.o ... done
build my_stdio.o my_string.o to libmystdio.a ... done
[gsm@VM-4-3-centos stdio]$ make output
output stdc ... done
[gsm@VM-4-3-centos stdio]$ ll
total 44
-rw-rw-r-- 1 gsm gsm 4374 Dec 11 17:17 libmystdio.a
-rw-rw-r-- 1 gsm gsm 376 Dec 11 17:17 Makefile
-rw-rw-r-- 1 gsm gsm 1550 Dec 11 15:18 my_stdio.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 15:18 my_stdio.h
-rw-rw-r-- 1 gsm gsm 2848 Dec 11 17:17 my_stdio.o
-rw-rw-r-- 1 gsm gsm 156 Dec 11 15:29 my_string.c
-rw-rw-r-- 1 gsm gsm 44 Dec 11 15:24 my_string.h
-rw-rw-r-- 1 gsm gsm 1272 Dec 11 17:17 my_string.o
drwxrwxr-x 4 gsm gsm 4096 Dec 11 17:17 stdc
-rw-rw-r-- 1 gsm gsm 1723 Dec 11 17:17 stdc.tgz
[gsm@VM-4-3-centos stdio]$ cd ../other/
[gsm@VM-4-3-centos other]$ ll
total 36
-rw-rw-r-- 1 gsm gsm 4374 Dec 11 16:27 libmystdio.a
-rw-rw-r-- 1 gsm gsm 21 Dec 11 16:41 log.txt
-rwxrwxr-x 1 gsm gsm 8952 Dec 11 16:41 main
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 16:27 my_stdio.h
-rw-rw-r-- 1 gsm gsm 44 Dec 11 16:27 my_string.h
[gsm@VM-4-3-centos other]$ rm *.a *.h
[gsm@VM-4-3-centos other]$ rm main
[gsm@VM-4-3-centos other]$ rm log.txt
[gsm@VM-4-3-centos other]$ ll
total 4
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
[gsm@VM-4-3-centos other]$ cat main.c
#include "my_stdio.h"
#include "my_string.h"
#include <stdio.h>
int main()
{
const char* s = "abcdefg";
printf("%s: %d\n", s, my_strlen(s));
mFILE* fp = mfopen("./log.txt", "a");
if (fp == NULL)
{
return 1;
}
mfwrite(s, my_strlen(s), fp);
mfwrite(s, my_strlen(s), fp);
mfwrite(s, my_strlen(s), fp);
mfclose(fp);
return 0;
}
[gsm@VM-4-3-centos other]$ cp ../stdio/stdc.tgz .
[gsm@VM-4-3-centos other]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
-rw-rw-r-- 1 gsm gsm 1723 Dec 11 17:19 stdc.tgz
[gsm@VM-4-3-centos other]$ tar xzf stdc.tgz
[gsm@VM-4-3-centos other]$ ll
total 12
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
drwxrwxr-x 4 gsm gsm 4096 Dec 11 17:17 stdc
-rw-rw-r-- 1 gsm gsm 1723 Dec 11 17:19 stdc.tgz
[gsm@VM-4-3-centos other]$ tree stdc
stdc
|-- include
| |-- my_stdio.h
| `-- my_string.h
`-- lib
`-- libmystdio.a
2 directories, 3 files
[gsm@VM-4-3-centos other]$ rm stdc.tgz
[gsm@VM-4-3-centos other]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
drwxrwxr-x 4 gsm gsm 4096 Dec 11 17:17 stdc
[gsm@VM-4-3-centos other]$ gcc main.c
main.c:1:22: fatal error: my_stdio.h: No such file or directory
#include "my_stdio.h"
^
compilation terminated.
[gsm@VM-4-3-centos other]$ gcc main.c -Istdc/include
/tmp/ccK3pCPX.o: In function `main':
main.c:(.text+0x18): undefined reference to `my_strlen'
main.c:(.text+0x3f): undefined reference to `mfopen'
main.c:(.text+0x5d): undefined reference to `my_strlen'
main.c:(.text+0x71): undefined reference to `mfwrite'
main.c:(.text+0x7d): undefined reference to `my_strlen'
main.c:(.text+0x91): undefined reference to `mfwrite'
main.c:(.text+0x9d): undefined reference to `my_strlen'
main.c:(.text+0xb1): undefined reference to `mfwrite'
main.c:(.text+0xbd): undefined reference to `mfclose'
collect2: error: ld returned 1 exit status
[gsm@VM-4-3-centos other]$ gcc main.c -Istdc/include -Lstdc/lib
/tmp/ccPiN2tF.o: In function `main':
main.c:(.text+0x18): undefined reference to `my_strlen'
main.c:(.text+0x3f): undefined reference to `mfopen'
main.c:(.text+0x5d): undefined reference to `my_strlen'
main.c:(.text+0x71): undefined reference to `mfwrite'
main.c:(.text+0x7d): undefined reference to `my_strlen'
main.c:(.text+0x91): undefined reference to `mfwrite'
main.c:(.text+0x9d): undefined reference to `my_strlen'
main.c:(.text+0xb1): undefined reference to `mfwrite'
main.c:(.text+0xbd): undefined reference to `mfclose'
collect2: error: ld returned 1 exit status
[gsm@VM-4-3-centos other]$ gcc main.c -Istdc/include -Lstdc/lib -lmystdio
[gsm@VM-4-3-centos other]$ ll
total 20
-rwxrwxr-x 1 gsm gsm 8952 Dec 11 17:24 a.out
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
drwxrwxr-x 4 gsm gsm 4096 Dec 11 17:17 stdc
[gsm@VM-4-3-centos other]$ ./a.out
abcdefg: 7
2. 动态库
c
复制代码
[gsm@VM-4-3-centos stdio]$ ll
total 20
-rw-rw-r-- 1 gsm gsm 519 Dec 11 17:48 Makefile
-rw-rw-r-- 1 gsm gsm 1550 Dec 11 15:18 my_stdio.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 15:18 my_stdio.h
-rw-rw-r-- 1 gsm gsm 156 Dec 11 15:29 my_string.c
-rw-rw-r-- 1 gsm gsm 44 Dec 11 15:24 my_string.h
[gsm@VM-4-3-centos stdio]$ vim Makefile
[gsm@VM-4-3-centos stdio]$ head -8 Makefile
libmystdio.so:my_stdio.o my_string.o
gcc -o $@ $^ -shared
%.o:%.c
gcc -fPIC -c $<
.PHONY:clean
clean:
rm -rf *.so *.o
[gsm@VM-4-3-centos stdio]$ make
gcc -fPIC -c my_stdio.c
gcc -fPIC -c my_string.c
gcc -o libmystdio.so my_stdio.o my_string.o -shared
[gsm@VM-4-3-centos stdio]$ ll
total 40
-rwxrwxr-x 1 gsm gsm 8520 Dec 11 17:48 libmystdio.so
-rw-rw-r-- 1 gsm gsm 519 Dec 11 17:48 Makefile
-rw-rw-r-- 1 gsm gsm 1550 Dec 11 15:18 my_stdio.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 15:18 my_stdio.h
-rw-rw-r-- 1 gsm gsm 2904 Dec 11 17:48 my_stdio.o
-rw-rw-r-- 1 gsm gsm 156 Dec 11 15:29 my_string.c
-rw-rw-r-- 1 gsm gsm 44 Dec 11 15:24 my_string.h
-rw-rw-r-- 1 gsm gsm 1272 Dec 11 17:48 my_string.o
[gsm@VM-4-3-centos stdio]$ sudo cp *.h /usr/include/
[sudo] password for gsm:
[gsm@VM-4-3-centos stdio]$ ls /usr/include/my*
/usr/include/my_stdio.h /usr/include/my_string.h
[gsm@VM-4-3-centos stdio]$ sudo cp libmystdio.so /lib64
[gsm@VM-4-3-centos stdio]$ ls /lib64/libmy*
/lib64/libmystdio.so
[gsm@VM-4-3-centos stdio]$ cd ../other/
[gsm@VM-4-3-centos other]$ ll
total 24
-rwxrwxr-x 1 gsm gsm 8952 Dec 11 17:24 a.out
-rw-rw-r-- 1 gsm gsm 21 Dec 11 17:25 log.txt
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
drwxrwxr-x 4 gsm gsm 4096 Dec 11 17:17 stdc
[gsm@VM-4-3-centos other]$ rm a.out
[gsm@VM-4-3-centos other]$ rm log.txt
[gsm@VM-4-3-centos other]$ rm -rf stdc/
[gsm@VM-4-3-centos other]$ ll
total 4
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
[gsm@VM-4-3-centos other]$ gcc main.c
/tmp/ccXWm0vg.o: In function `main':
main.c:(.text+0x18): undefined reference to `my_strlen'
main.c:(.text+0x3f): undefined reference to `mfopen'
main.c:(.text+0x5d): undefined reference to `my_strlen'
main.c:(.text+0x71): undefined reference to `mfwrite'
main.c:(.text+0x7d): undefined reference to `my_strlen'
main.c:(.text+0x91): undefined reference to `mfwrite'
main.c:(.text+0x9d): undefined reference to `my_strlen'
main.c:(.text+0xb1): undefined reference to `mfwrite'
main.c:(.text+0xbd): undefined reference to `mfclose'
collect2: error: ld returned 1 exit status
[gsm@VM-4-3-centos other]$ gcc main.c -lmystdio
[gsm@VM-4-3-centos other]$ ll
total 16
-rwxrwxr-x 1 gsm gsm 8520 Dec 11 17:53 a.out
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
[gsm@VM-4-3-centos other]$ ldd a.out
linux-vdso.so.1 => (0x00007fffecdec000)
libmystdio.so => /lib64/libmystdio.so (0x00007fd298d39000)
libc.so.6 => /lib64/libc.so.6 (0x00007fd29896b000)
/lib64/ld-linux-x86-64.so.2 (0x00007fd298f3b000)
[gsm@VM-4-3-centos other]$ ./a.out
abcdefg: 7
[gsm@VM-4-3-centos other]$ ll
total 20
-rwxrwxr-x 1 gsm gsm 8520 Dec 11 17:53 a.out
-rw-rw-r-- 1 gsm gsm 21 Dec 11 17:54 log.txt
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
[gsm@VM-4-3-centos other]$ cat log.txt
abcdefgabcdefgabcdefg[gsm@VM-4-3-centos other]$ sudo rm /usr/include/my_st*
[sudo] password for gsm:
[gsm@VM-4-3-centos other]$ sudo rm /lib64/libmystdio.so
[gsm@VM-4-3-centos other]$ ll
total 20
-rwxrwxr-x 1 gsm gsm 8520 Dec 11 17:53 a.out
-rw-rw-r-- 1 gsm gsm 21 Dec 11 17:54 log.txt
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
[gsm@VM-4-3-centos other]$ ./a.out
./a.out: error while loading shared libraries: libmystdio.so: cannot open shared object file: No such file or directory
[gsm@VM-4-3-centos other]$ ldd a.out
linux-vdso.so.1 => (0x00007ffd517db000)
libmystdio.so => not found
libc.so.6 => /lib64/libc.so.6 (0x00007f3f5bbed000)
/lib64/ld-linux-x86-64.so.2 (0x00007f3f5bfbb000)
[gsm@VM-4-3-centos other]$ ldd /usr/bin/ls
linux-vdso.so.1 => (0x00007ffc15fba000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f1c2a2a6000)
libcap.so.2 => /lib64/libcap.so.2 (0x00007f1c2a0a1000)
libacl.so.1 => /lib64/libacl.so.1 (0x00007f1c29e98000)
libc.so.6 => /lib64/libc.so.6 (0x00007f1c29aca000)
libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f1c29868000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f1c29664000)
/lib64/ld-linux-x86-64.so.2 (0x00007f1c2a4cd000)
libattr.so.1 => /lib64/libattr.so.1 (0x00007f1c2945f000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f1c29243000)
c
复制代码
[gsm@VM-4-3-centos stdio]$ ll
total 40
-rwxrwxr-x 1 gsm gsm 8520 Dec 11 17:48 libmystdio.so
-rw-rw-r-- 1 gsm gsm 519 Dec 11 17:48 Makefile
-rw-rw-r-- 1 gsm gsm 1550 Dec 11 15:18 my_stdio.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 15:18 my_stdio.h
-rw-rw-r-- 1 gsm gsm 2904 Dec 11 17:48 my_stdio.o
-rw-rw-r-- 1 gsm gsm 156 Dec 11 15:29 my_string.c
-rw-rw-r-- 1 gsm gsm 44 Dec 11 15:24 my_string.h
-rw-rw-r-- 1 gsm gsm 1272 Dec 11 17:48 my_string.o
[gsm@VM-4-3-centos stdio]$ cp *.h ../other/
[gsm@VM-4-3-centos stdio]$ cp *.so ../other/
[gsm@VM-4-3-centos stdio]$ cd ../other/
[gsm@VM-4-3-centos other]$ ll
total 24
-rwxrwxr-x 1 gsm gsm 8520 Dec 11 18:07 libmystdio.so
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 18:07 my_stdio.h
-rw-rw-r-- 1 gsm gsm 44 Dec 11 18:07 my_string.h
[gsm@VM-4-3-centos other]$ gcc main.c -o main
/tmp/ccreO3L5.o: In function `main':
main.c:(.text+0x18): undefined reference to `my_strlen'
main.c:(.text+0x3f): undefined reference to `mfopen'
main.c:(.text+0x5d): undefined reference to `my_strlen'
main.c:(.text+0x71): undefined reference to `mfwrite'
main.c:(.text+0x7d): undefined reference to `my_strlen'
main.c:(.text+0x91): undefined reference to `mfwrite'
main.c:(.text+0x9d): undefined reference to `my_strlen'
main.c:(.text+0xb1): undefined reference to `mfwrite'
main.c:(.text+0xbd): undefined reference to `mfclose'
collect2: error: ld returned 1 exit status
[gsm@VM-4-3-centos other]$ gcc main.c -o main -L.
/tmp/ccj3ZHhW.o: In function `main':
main.c:(.text+0x18): undefined reference to `my_strlen'
main.c:(.text+0x3f): undefined reference to `mfopen'
main.c:(.text+0x5d): undefined reference to `my_strlen'
main.c:(.text+0x71): undefined reference to `mfwrite'
main.c:(.text+0x7d): undefined reference to `my_strlen'
main.c:(.text+0x91): undefined reference to `mfwrite'
main.c:(.text+0x9d): undefined reference to `my_strlen'
main.c:(.text+0xb1): undefined reference to `mfwrite'
main.c:(.text+0xbd): undefined reference to `mfclose'
collect2: error: ld returned 1 exit status
[gsm@VM-4-3-centos other]$ gcc main.c -o main -L. -lmystdio
[gsm@VM-4-3-centos other]$ ll
total 36
-rwxrwxr-x 1 gsm gsm 8520 Dec 11 18:07 libmystdio.so
-rwxrwxr-x 1 gsm gsm 8520 Dec 11 21:22 main
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 18:07 my_stdio.h
-rw-rw-r-- 1 gsm gsm 44 Dec 11 18:07 my_string.h
[gsm@VM-4-3-centos other]$ ./main
abcdefg: 7
c
复制代码
[gsm@VM-4-3-centos stdio]$ ll
total 40
-rwxrwxr-x 1 gsm gsm 8520 Dec 11 17:48 libmystdio.so
-rw-rw-r-- 1 gsm gsm 519 Dec 11 17:48 Makefile
-rw-rw-r-- 1 gsm gsm 1550 Dec 11 15:18 my_stdio.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 15:18 my_stdio.h
-rw-rw-r-- 1 gsm gsm 2904 Dec 11 17:48 my_stdio.o
-rw-rw-r-- 1 gsm gsm 156 Dec 11 15:29 my_string.c
-rw-rw-r-- 1 gsm gsm 44 Dec 11 15:24 my_string.h
-rw-rw-r-- 1 gsm gsm 1272 Dec 11 17:48 my_string.o
[gsm@VM-4-3-centos stdio]$ vim Makefile
[gsm@VM-4-3-centos stdio]$ head -16 Makefile
libmystdio.so:my_stdio.o my_string.o
gcc -o $@ $^ -shared
%.o:%.c
gcc -fPIC -c $<
.PHONY:clean
clean:
rm -rf *.so *.o stdc*
.PHONY:output
output:
mkdir -p stdc/include
mkdir -p stdc/lib
cp -f *.h stdc/include
cp -f *.so stdc/lib
tar czf stdc.tgz stdc
[gsm@VM-4-3-centos stdio]$ make clean
rm -rf *.so *.o stdc*
[gsm@VM-4-3-centos stdio]$ ll
total 20
-rw-rw-r-- 1 gsm gsm 658 Dec 11 21:33 Makefile
-rw-rw-r-- 1 gsm gsm 1550 Dec 11 15:18 my_stdio.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 15:18 my_stdio.h
-rw-rw-r-- 1 gsm gsm 156 Dec 11 15:29 my_string.c
-rw-rw-r-- 1 gsm gsm 44 Dec 11 15:24 my_string.h
[gsm@VM-4-3-centos stdio]$ make
gcc -fPIC -c my_stdio.c
gcc -fPIC -c my_string.c
gcc -o libmystdio.so my_stdio.o my_string.o -shared
[gsm@VM-4-3-centos stdio]$ ll
total 40
-rwxrwxr-x 1 gsm gsm 8520 Dec 11 21:33 libmystdio.so
-rw-rw-r-- 1 gsm gsm 658 Dec 11 21:33 Makefile
-rw-rw-r-- 1 gsm gsm 1550 Dec 11 15:18 my_stdio.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 15:18 my_stdio.h
-rw-rw-r-- 1 gsm gsm 2904 Dec 11 21:33 my_stdio.o
-rw-rw-r-- 1 gsm gsm 156 Dec 11 15:29 my_string.c
-rw-rw-r-- 1 gsm gsm 44 Dec 11 15:24 my_string.h
-rw-rw-r-- 1 gsm gsm 1272 Dec 11 21:33 my_string.o
[gsm@VM-4-3-centos stdio]$ make output
mkdir -p stdc/include
mkdir -p stdc/lib
cp -f *.h stdc/include
cp -f *.so stdc/lib
tar czf stdc.tgz stdc
[gsm@VM-4-3-centos stdio]$ ll
total 48
-rwxrwxr-x 1 gsm gsm 8520 Dec 11 21:33 libmystdio.so
-rw-rw-r-- 1 gsm gsm 658 Dec 11 21:33 Makefile
-rw-rw-r-- 1 gsm gsm 1550 Dec 11 15:18 my_stdio.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 15:18 my_stdio.h
-rw-rw-r-- 1 gsm gsm 2904 Dec 11 21:33 my_stdio.o
-rw-rw-r-- 1 gsm gsm 156 Dec 11 15:29 my_string.c
-rw-rw-r-- 1 gsm gsm 44 Dec 11 15:24 my_string.h
-rw-rw-r-- 1 gsm gsm 1272 Dec 11 21:33 my_string.o
drwxrwxr-x 4 gsm gsm 4096 Dec 11 21:33 stdc
-rw-rw-r-- 1 gsm gsm 3346 Dec 11 21:33 stdc.tgz
[gsm@VM-4-3-centos stdio]$ tree stdc
stdc
|-- include
| |-- my_stdio.h
| `-- my_string.h
`-- lib
`-- libmystdio.so
2 directories, 3 files
[gsm@VM-4-3-centos stdio]$ cd ../other/
[gsm@VM-4-3-centos other]$ ll
total 4
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
[gsm@VM-4-3-centos other]$ cp ../stdio/stdc.tgz .
[gsm@VM-4-3-centos other]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
-rw-rw-r-- 1 gsm gsm 3346 Dec 11 21:40 stdc.tgz
[gsm@VM-4-3-centos other]$ tar xzf stdc.tgz
[gsm@VM-4-3-centos other]$ ll
total 12
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
drwxrwxr-x 4 gsm gsm 4096 Dec 11 21:33 stdc
-rw-rw-r-- 1 gsm gsm 3346 Dec 11 21:40 stdc.tgz
[gsm@VM-4-3-centos other]$ tree stdc
stdc
|-- include
| |-- my_stdio.h
| `-- my_string.h
`-- lib
`-- libmystdio.so
2 directories, 3 files
[gsm@VM-4-3-centos other]$ rm stdc.tgz
[gsm@VM-4-3-centos other]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
drwxrwxr-x 4 gsm gsm 4096 Dec 11 21:33 stdc
[gsm@VM-4-3-centos other]$ gcc main.c -o main
main.c:1:22: fatal error: my_stdio.h: No such file or directory
#include "my_stdio.h"
^
compilation terminated.
[gsm@VM-4-3-centos other]$ gcc main.c -o main -Istdc/include
/tmp/ccctSiYm.o: In function `main':
main.c:(.text+0x18): undefined reference to `my_strlen'
main.c:(.text+0x3f): undefined reference to `mfopen'
main.c:(.text+0x5d): undefined reference to `my_strlen'
main.c:(.text+0x71): undefined reference to `mfwrite'
main.c:(.text+0x7d): undefined reference to `my_strlen'
main.c:(.text+0x91): undefined reference to `mfwrite'
main.c:(.text+0x9d): undefined reference to `my_strlen'
main.c:(.text+0xb1): undefined reference to `mfwrite'
main.c:(.text+0xbd): undefined reference to `mfclose'
collect2: error: ld returned 1 exit status
[gsm@VM-4-3-centos other]$ gcc main.c -o main -Istdc/include -Lstdc/lib
/tmp/ccg0izSf.o: In function `main':
main.c:(.text+0x18): undefined reference to `my_strlen'
main.c:(.text+0x3f): undefined reference to `mfopen'
main.c:(.text+0x5d): undefined reference to `my_strlen'
main.c:(.text+0x71): undefined reference to `mfwrite'
main.c:(.text+0x7d): undefined reference to `my_strlen'
main.c:(.text+0x91): undefined reference to `mfwrite'
main.c:(.text+0x9d): undefined reference to `my_strlen'
main.c:(.text+0xb1): undefined reference to `mfwrite'
main.c:(.text+0xbd): undefined reference to `mfclose'
collect2: error: ld returned 1 exit status
[gsm@VM-4-3-centos other]$ gcc main.c -o main -Istdc/include -Lstdc/lib -lmystdio
[gsm@VM-4-3-centos other]$ ll
total 20
-rwxrwxr-x 1 gsm gsm 8520 Dec 11 21:42 main
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
drwxrwxr-x 4 gsm gsm 4096 Dec 11 21:33 stdc
[gsm@VM-4-3-centos other]$ ./main
./main: error while loading shared libraries: libmystdio.so: cannot open shared object file: No such file or directory
[gsm@VM-4-3-centos other]$ ldd main
linux-vdso.so.1 => (0x00007ffc46d5d000)
libmystdio.so => not found
libc.so.6 => /lib64/libc.so.6 (0x00007f9005e90000)
/lib64/ld-linux-x86-64.so.2 (0x00007f900625e000)
[gsm@VM-4-3-centos other]$ sudo ln -s /home/gsm/linux/112/lesson20/other/stdc/lib/libmystdio.so /lib64/libmystdio.so
[sudo] password for gsm:
[gsm@VM-4-3-centos other]$ ll /lib64/libmystdio.so
lrwxrwxrwx 1 root root 57 Dec 11 21:56 /lib64/libmystdio.so -> /home/gsm/linux/112/lesson20/other/stdc/lib/libmystdio.so
[gsm@VM-4-3-centos other]$ ldd main
linux-vdso.so.1 => (0x00007fff0f1e9000)
libmystdio.so => /lib64/libmystdio.so (0x00007f4aa1979000)
libc.so.6 => /lib64/libc.so.6 (0x00007f4aa15ab000)
/lib64/ld-linux-x86-64.so.2 (0x00007f4aa1b7b000)
[gsm@VM-4-3-centos other]$ ./main
abcdefg: 7
[gsm@VM-4-3-centos other]$ sudo unlink /lib64/libmystdio.so
[gsm@VM-4-3-centos other]$ ldd main
linux-vdso.so.1 => (0x00007ffe1a996000)
libmystdio.so => not found
libc.so.6 => /lib64/libc.so.6 (0x00007f3772d5e000)
/lib64/ld-linux-x86-64.so.2 (0x00007f377312c000)
[gsm@VM-4-3-centos other]$ ./main
./main: error while loading shared libraries: libmystdio.so: cannot open shared object file: No such file or directory
[gsm@VM-4-3-centos other]$ env
XDG_SESSION_ID=120553
HOSTNAME=VM-4-3-centos
TERM=xterm
SHELL=/bin/bash
HISTSIZE=3000
SSH_CLIENT=101.94.68.192 59466 22
OLDPWD=/home/gsm/linux/112/lesson20/stdio
SSH_TTY=/dev/pts/0
USER=gsm
LD_LIBRARY_PATH=:/home/gsm/.VimForCpp/vim/bundle/YCM.so/el7.x86_64
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
MAIL=/var/spool/mail/gsm
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/gsm/.local/bin:/home/gsm/bin
PWD=/home/gsm/linux/112/lesson20/other
LANG=en_US.utf8
SHLVL=1
HOME=/home/gsm
LOGNAME=gsm
SSH_CONNECTION=101.94.68.192 59466 10.0.4.3 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
PROMPT_COMMAND=history -a; history -a; printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"
XDG_RUNTIME_DIR=/run/user/1001
HISTTIMEFORMAT=%F %T
_=/usr/bin/env
[gsm@VM-4-3-centos other]$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/gsm/linux/112/lesson20/other/stdc/lib
[gsm@VM-4-3-centos other]$ echo $LD_LIBRARY_PATH
:/home/gsm/.VimForCpp/vim/bundle/YCM.so/el7.x86_64:/home/gsm/linux/112/lesson20/other/stdc/lib
[gsm@VM-4-3-centos other]$ ldd main
linux-vdso.so.1 => (0x00007ffe6278a000)
libmystdio.so => /home/gsm/linux/112/lesson20/other/stdc/lib/libmystdio.so (0x00007fcd4da9d000)
libc.so.6 => /lib64/libc.so.6 (0x00007fcd4d6cf000)
/lib64/ld-linux-x86-64.so.2 (0x00007fcd4dc9f000)
[gsm@VM-4-3-centos other]$ ./main
abcdefg: 7
// 关了xshell重新打开
[gsm@VM-4-3-centos other]$ ll
total 24
-rw-rw-r-- 1 gsm gsm 42 Dec 11 22:10 log.txt
-rwxrwxr-x 1 gsm gsm 8520 Dec 11 21:42 main
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
drwxrwxr-x 4 gsm gsm 4096 Dec 11 21:33 stdc
[gsm@VM-4-3-centos other]$ ldd main
linux-vdso.so.1 => (0x00007ffdb935a000)
libmystdio.so => not found
libc.so.6 => /lib64/libc.so.6 (0x00007f4f2be38000)
/lib64/ld-linux-x86-64.so.2 (0x00007f4f2c206000)
[gsm@VM-4-3-centos other]$ ls /etc/ld.so.conf.d/
bind-export-x86_64.conf kernel-3.10.0-1160.119.1.el7.x86_64.conf
dyninst-x86_64.conf mariadb-x86_64.conf
[gsm@VM-4-3-centos other]$ cat /etc/ld.so.conf.d/mariadb-x86_64.conf
/usr/lib64/mysql
[gsm@VM-4-3-centos other]$ ls /etc/ld.so.conf.d/ -l
total 16
-rw-r--r-- 1 root root 26 Jun 11 2024 bind-export-x86_64.conf
-rw-r--r-- 1 root root 19 Aug 9 2019 dyninst-x86_64.conf
-r--r--r-- 1 root root 63 Jun 4 2024 kernel-3.10.0-1160.119.1.el7.x86_64.conf
-rw-r--r-- 1 root root 17 Oct 2 2020 mariadb-x86_64.conf
[gsm@VM-4-3-centos other]$ sudo touch /etc/ld.so.conf.d/112.conf
[sudo] password for gsm:
[gsm@VM-4-3-centos other]$ ll /etc/ld.so.conf.d/
total 16
-rw-r--r-- 1 root root 0 Dec 11 22:32 112.conf
-rw-r--r-- 1 root root 26 Jun 11 2024 bind-export-x86_64.conf
-rw-r--r-- 1 root root 19 Aug 9 2019 dyninst-x86_64.conf
-r--r--r-- 1 root root 63 Jun 4 2024 kernel-3.10.0-1160.119.1.el7.x86_64.conf
-rw-r--r-- 1 root root 17 Oct 2 2020 mariadb-x86_64.conf
[gsm@VM-4-3-centos other]$ sudo vim /etc/ld.so.conf.d/112.conf
[sudo] password for gsm:
[gsm@VM-4-3-centos other]$ sudo cat /etc/ld.so.conf.d/112.conf
/home/gsm/linux/112/lesson20/other/stdc/lib
[gsm@VM-4-3-centos other]$ sudo ldconfig
[gsm@VM-4-3-centos other]$ ldd main
linux-vdso.so.1 => (0x00007ffdc1f49000)
libmystdio.so => /home/gsm/linux/112/lesson20/other/stdc/lib/libmystdio.so (0x00007fa36f053000)
libc.so.6 => /lib64/libc.so.6 (0x00007fa36ec85000)
/lib64/ld-linux-x86-64.so.2 (0x00007fa36f255000)
[gsm@VM-4-3-centos other]$ ./main
abcdefg: 7
c
复制代码
[gsm@VM-4-3-centos stdio]$ ll
total 48
-rwxrwxr-x 1 gsm gsm 8520 Dec 11 21:33 libmystdio.so
-rw-rw-r-- 1 gsm gsm 658 Dec 11 21:33 Makefile
-rw-rw-r-- 1 gsm gsm 1550 Dec 11 15:18 my_stdio.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 15:18 my_stdio.h
-rw-rw-r-- 1 gsm gsm 2904 Dec 11 21:33 my_stdio.o
-rw-rw-r-- 1 gsm gsm 156 Dec 11 15:29 my_string.c
-rw-rw-r-- 1 gsm gsm 44 Dec 11 15:24 my_string.h
-rw-rw-r-- 1 gsm gsm 1272 Dec 11 21:33 my_string.o
drwxrwxr-x 4 gsm gsm 4096 Dec 11 21:33 stdc
-rw-rw-r-- 1 gsm gsm 3346 Dec 11 21:33 stdc.tgz
[gsm@VM-4-3-centos stdio]$ vim Makefile
[gsm@VM-4-3-centos stdio]$ cat Makefile
#libmystdio.so:my_stdio.o my_string.o
# gcc -o $@ $^ -shared
#%.o:%.c
# gcc -fPIC -c $<
#
#.PHONY:clean
#clean:
# rm -rf *.so *.o stdc*
#
#.PHONY:output
#output:
# mkdir -p stdc/include
# mkdir -p stdc/lib
# cp -f *.h stdc/include
# cp -f *.so stdc/lib
# tar czf stdc.tgz stdc
libmystdio.a:my_stdio.o my_string.o
@ar -rc $@ $^
@echo "build $^ to $@ ... done"
%.o:%.c
@gcc -c $<
@echo "compiling $< to $@ ... done"
.PHONY:clean
clean:
@rm -rf *.a *.o stdc*
@echo "clean ... done"
.PHONY:output
output:
@mkdir -p stdc/include
@mkdir -p stdc/lib
@cp -f *.h stdc/include
@cp -f *.a stdc/lib
@tar czf stdc.tgz stdc
@echo "output stdc ... done"
[gsm@VM-4-3-centos stdio]$ make clean
clean ... done
[gsm@VM-4-3-centos stdio]$ ll
total 32
-rwxrwxr-x 1 gsm gsm 8520 Dec 11 21:33 libmystdio.so
-rw-rw-r-- 1 gsm gsm 654 Dec 11 22:55 Makefile
-rw-rw-r-- 1 gsm gsm 1550 Dec 11 15:18 my_stdio.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 15:18 my_stdio.h
-rw-rw-r-- 1 gsm gsm 156 Dec 11 15:29 my_string.c
-rw-rw-r-- 1 gsm gsm 44 Dec 11 15:24 my_string.h
[gsm@VM-4-3-centos stdio]$ make
compiling my_stdio.c to my_stdio.o ... done
compiling my_string.c to my_string.o ... done
build my_stdio.o my_string.o to libmystdio.a ... done
[gsm@VM-4-3-centos stdio]$ make output
output stdc ... done
[gsm@VM-4-3-centos stdio]$ ll
total 56
-rw-rw-r-- 1 gsm gsm 4374 Dec 11 22:55 libmystdio.a
-rwxrwxr-x 1 gsm gsm 8520 Dec 11 21:33 libmystdio.so
-rw-rw-r-- 1 gsm gsm 654 Dec 11 22:55 Makefile
-rw-rw-r-- 1 gsm gsm 1550 Dec 11 15:18 my_stdio.c
-rw-rw-r-- 1 gsm gsm 446 Dec 11 15:18 my_stdio.h
-rw-rw-r-- 1 gsm gsm 2848 Dec 11 22:55 my_stdio.o
-rw-rw-r-- 1 gsm gsm 156 Dec 11 15:29 my_string.c
-rw-rw-r-- 1 gsm gsm 44 Dec 11 15:24 my_string.h
-rw-rw-r-- 1 gsm gsm 1272 Dec 11 22:55 my_string.o
drwxrwxr-x 4 gsm gsm 4096 Dec 11 22:55 stdc
-rw-rw-r-- 1 gsm gsm 1722 Dec 11 22:55 stdc.tgz
[gsm@VM-4-3-centos stdio]$ tree stdc
stdc
|-- include
| |-- my_stdio.h
| `-- my_string.h
`-- lib
`-- libmystdio.a
2 directories, 3 files
[gsm@VM-4-3-centos stdio]$ cp libmystdio.a ../other/stdc/lib/
[gsm@VM-4-3-centos stdio]$ cd ../other/
[gsm@VM-4-3-centos other]$ ll
total 24
-rw-rw-r-- 1 gsm gsm 63 Dec 11 22:44 log.txt
-rwxrwxr-x 1 gsm gsm 8520 Dec 11 21:42 main
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
drwxrwxr-x 4 gsm gsm 4096 Dec 11 21:33 stdc
[gsm@VM-4-3-centos other]$ pwd
/home/gsm/linux/112/lesson20/other
[gsm@VM-4-3-centos other]$ tree .
.
|-- log.txt
|-- main
|-- main.c
`-- stdc
|-- include
| |-- my_stdio.h
| `-- my_string.h
`-- lib
|-- libmystdio.a
`-- libmystdio.so
3 directories, 7 files
[gsm@VM-4-3-centos other]$ rm log.txt
[gsm@VM-4-3-centos other]$ rm main
[gsm@VM-4-3-centos other]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
drwxrwxr-x 4 gsm gsm 4096 Dec 11 21:33 stdc
[gsm@VM-4-3-centos other]$ gcc main.c -o main
main.c:1:22: fatal error: my_stdio.h: No such file or directory
#include "my_stdio.h"
^
compilation terminated.
[gsm@VM-4-3-centos other]$ gcc main.c -o main -I stdc/include/
/tmp/ccw0ED7f.o: In function `main':
main.c:(.text+0x18): undefined reference to `my_strlen'
main.c:(.text+0x3f): undefined reference to `mfopen'
main.c:(.text+0x5d): undefined reference to `my_strlen'
main.c:(.text+0x71): undefined reference to `mfwrite'
main.c:(.text+0x7d): undefined reference to `my_strlen'
main.c:(.text+0x91): undefined reference to `mfwrite'
main.c:(.text+0x9d): undefined reference to `my_strlen'
main.c:(.text+0xb1): undefined reference to `mfwrite'
main.c:(.text+0xbd): undefined reference to `mfclose'
collect2: error: ld returned 1 exit status
[gsm@VM-4-3-centos other]$ gcc main.c -o main -I stdc/include/ -L stdc/lib
/tmp/ccSiBF9W.o: In function `main':
main.c:(.text+0x18): undefined reference to `my_strlen'
main.c:(.text+0x3f): undefined reference to `mfopen'
main.c:(.text+0x5d): undefined reference to `my_strlen'
main.c:(.text+0x71): undefined reference to `mfwrite'
main.c:(.text+0x7d): undefined reference to `my_strlen'
main.c:(.text+0x91): undefined reference to `mfwrite'
main.c:(.text+0x9d): undefined reference to `my_strlen'
main.c:(.text+0xb1): undefined reference to `mfwrite'
main.c:(.text+0xbd): undefined reference to `mfclose'
collect2: error: ld returned 1 exit status
[gsm@VM-4-3-centos other]$ gcc main.c -o main -I stdc/include/ -L stdc/lib -lmystdio
[gsm@VM-4-3-centos other]$ ldd main
linux-vdso.so.1 => (0x00007ffca33d7000)
libmystdio.so => /home/gsm/linux/112/lesson20/other/stdc/lib/libmystdio.so (0x00007efe608d7000)
libc.so.6 => /lib64/libc.so.6 (0x00007efe60509000)
/lib64/ld-linux-x86-64.so.2 (0x00007efe60ad9000)
[gsm@VM-4-3-centos other]$ file main
main: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=c138579624a8ea6c707142ed070eb80410168616, not stripped
[gsm@VM-4-3-centos other]$ ./main
abcdefg: 7
[gsm@VM-4-3-centos other]$ gcc main.c -o main -I stdc/include/ -L stdc/lib -lmystdio -static
[gsm@VM-4-3-centos other]$ ./main
abcdefg: 7
[gsm@VM-4-3-centos other]$ ldd main
not a dynamic executable
[gsm@VM-4-3-centos other]$ file main
main: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.32, BuildID[sha1]=e327b77eb762c3f960c30115c91f7cd2f9a49289, not stripped
[gsm@VM-4-3-centos other]$ tree stdc
stdc
|-- include
| |-- my_stdio.h
| `-- my_string.h
`-- lib
|-- libmystdio.a
`-- libmystdio.so
2 directories, 4 files
[gsm@VM-4-3-centos other]$ mv stdc/lib/*.a ..
[gsm@VM-4-3-centos other]$ tree .
.
|-- log.txt
|-- main
|-- main.c
`-- stdc
|-- include
| |-- my_stdio.h
| `-- my_string.h
`-- lib
`-- libmystdio.so
3 directories, 6 files
[gsm@VM-4-3-centos other]$ rm main
[gsm@VM-4-3-centos other]$ gcc main.c -o main -I stdc/include/ -L stdc/lib -lmystdio -static
/usr/bin/ld: cannot find -lmystdio
collect2: error: ld returned 1 exit status
[gsm@VM-4-3-centos other]$ mv ../*.a stdc/lib
[gsm@VM-4-3-centos other]$ tree .
.
|-- log.txt
|-- main.c
`-- stdc
|-- include
| |-- my_stdio.h
| `-- my_string.h
`-- lib
|-- libmystdio.a
`-- libmystdio.so
3 directories, 6 files
[gsm@VM-4-3-centos other]$ mv stdc/lib/libmystdio.so ..
[gsm@VM-4-3-centos other]$ tree .
.
|-- log.txt
|-- main.c
`-- stdc
|-- include
| |-- my_stdio.h
| `-- my_string.h
`-- lib
`-- libmystdio.a
3 directories, 5 files
[gsm@VM-4-3-centos other]$ gcc main.c -o main -I stdc/include/ -L stdc/lib -lmystdio -static
[gsm@VM-4-3-centos other]$ ./main
abcdefg: 7
[gsm@VM-4-3-centos other]$ rm main
[gsm@VM-4-3-centos other]$ ll
total 12
-rw-rw-r-- 1 gsm gsm 63 Dec 11 23:10 log.txt
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
drwxrwxr-x 4 gsm gsm 4096 Dec 11 21:33 stdc
[gsm@VM-4-3-centos other]$ gcc main.c -o main -I stdc/include/ -L stdc/lib -lmystdio
[gsm@VM-4-3-centos other]$ ll
total 24
-rw-rw-r-- 1 gsm gsm 63 Dec 11 23:10 log.txt
-rwxrwxr-x 1 gsm gsm 8952 Dec 11 23:11 main
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
drwxrwxr-x 4 gsm gsm 4096 Dec 11 21:33 stdc
[gsm@VM-4-3-centos other]$ ldd main
linux-vdso.so.1 => (0x00007fff6dffe000)
libc.so.6 => /lib64/libc.so.6 (0x00007faf5a98d000)
/lib64/ld-linux-x86-64.so.2 (0x00007faf5ad5b000)
[gsm@VM-4-3-centos other]$ file main
main: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=e934140fb902f927c82188abd9d76e9de9ecfd8b, not stripped
[gsm@VM-4-3-centos other]$ ./main
abcdefg: 7
[gsm@VM-4-3-centos other]$ rm stdc -rf
[gsm@VM-4-3-centos other]$ ll
total 20
-rw-rw-r-- 1 gsm gsm 84 Dec 11 23:12 log.txt
-rwxrwxr-x 1 gsm gsm 8952 Dec 11 23:11 main
-rw-rw-r-- 1 gsm gsm 382 Dec 11 16:38 main.c
[gsm@VM-4-3-centos other]$ ./main
abcdefg: 7