how to compile and link static/dynamic library in linux
2009-12-06 11:26
489 查看
for static library:
1) use gcc to compile the target files, for example:
gcc –c Strlen.c Strnlen.c
2) generates the static library with the following command:
ar –rc libstr.a Strlen.o Strnlen.o
3) how to use the static library
when we need to generate the executable code ( header files of the static library should be included )base on the static library, use the following commands[http://dev.csdn.net/article/84/84562.shtm]
gcc -o main1 -L/home/hcj/xxxxxxxx main.o libstr.a
其
中-I/home/hcj/xxxxxxxx和-L/home/hcj/xxxxxxxx是通过-I和-L指定对应的头文件和库文件的路径。
libstr.a是对应的静态库的名称。这样对应的静态库已经编译到对应的可执行程序中。执行对应的可执行文件便可以对应得函数调用的结果。
for the dynamic library, use the following commands:
[http://dev.csdn.net/article/84/84562.shtm]
gcc -fpic -shared -o libstr.so Strlen.c Strnlen.c
-fpic 使输出的对象模块是按照可重定位地址方式生成的。
-shared指定把对应的源文件生成对应的动态链接库文件libstr.so文件。
动态库的分为隐式调用和显式调用两种调用方法:
隐式调用的使用使用方法和静态库的调用差不多,具体方法如下:
gcc -c -I/home/hcj/xxxxxxxx main.c
gcc -o main1 -L/home/hcj/xxxxxxxx main.o libstr.so //这里是*.so
在这种调用方式中,需要维护动态链接库的配置文件/etc/ld.so.conf来让动态链接库为系统所使用,通常将动态链接库所在目录名追加到动
态链接库配置文件中。否则在执行相关的可执行文件的时候就会出现载入动态链接库失败的现象。在编译所引用的动态库时,可以在gcc采用
–l或-L选项或直接引用所需的动态链接库方式进行编译。在Linux里面,可以采用ldd命令来检查程序依赖共享库。
显式调用:
#include<dlfcn.h>
int (*pStrlenFun)(char* pStr); //声明对应的函数的函数指针
void *pdlHandle;
char *pszErr;
pdlHandle = dlopen("./libstr.so", RTLD_LAZY); //加载链接库/libstr.so
if(!pdlHandle)
{
printf("Failed load library/n");
}
pszErr = dlerror();
if(pszErr != NULL)
{
printf("%s/n", pszErr);
return 0;
}
//get function from lib
pStrlenFun = dlsym(pdlHandle, "Strlen"); //获取函数的地址
pszErr = dlerror();
if(pszErr != NULL)
{
printf("%s/n", pszErr);
return 0;
}
1) use gcc to compile the target files, for example:
gcc –c Strlen.c Strnlen.c
2) generates the static library with the following command:
ar –rc libstr.a Strlen.o Strnlen.o
3) how to use the static library
when we need to generate the executable code ( header files of the static library should be included )base on the static library, use the following commands[http://dev.csdn.net/article/84/84562.shtm]
gcc -o main1 -L/home/hcj/xxxxxxxx main.o libstr.a
其
中-I/home/hcj/xxxxxxxx和-L/home/hcj/xxxxxxxx是通过-I和-L指定对应的头文件和库文件的路径。
libstr.a是对应的静态库的名称。这样对应的静态库已经编译到对应的可执行程序中。执行对应的可执行文件便可以对应得函数调用的结果。
for the dynamic library, use the following commands:
[http://dev.csdn.net/article/84/84562.shtm]
gcc -fpic -shared -o libstr.so Strlen.c Strnlen.c
-fpic 使输出的对象模块是按照可重定位地址方式生成的。
-shared指定把对应的源文件生成对应的动态链接库文件libstr.so文件。
动态库的分为隐式调用和显式调用两种调用方法:
隐式调用的使用使用方法和静态库的调用差不多,具体方法如下:
gcc -c -I/home/hcj/xxxxxxxx main.c
gcc -o main1 -L/home/hcj/xxxxxxxx main.o libstr.so //这里是*.so
在这种调用方式中,需要维护动态链接库的配置文件/etc/ld.so.conf来让动态链接库为系统所使用,通常将动态链接库所在目录名追加到动
态链接库配置文件中。否则在执行相关的可执行文件的时候就会出现载入动态链接库失败的现象。在编译所引用的动态库时,可以在gcc采用
–l或-L选项或直接引用所需的动态链接库方式进行编译。在Linux里面,可以采用ldd命令来检查程序依赖共享库。
显式调用:
#include<dlfcn.h>
int (*pStrlenFun)(char* pStr); //声明对应的函数的函数指针
void *pdlHandle;
char *pszErr;
pdlHandle = dlopen("./libstr.so", RTLD_LAZY); //加载链接库/libstr.so
if(!pdlHandle)
{
printf("Failed load library/n");
}
pszErr = dlerror();
if(pszErr != NULL)
{
printf("%s/n", pszErr);
return 0;
}
//get function from lib
pStrlenFun = dlsym(pdlHandle, "Strlen"); //获取函数的地址
pszErr = dlerror();
if(pszErr != NULL)
{
printf("%s/n", pszErr);
return 0;
}
相关文章推荐
- How to Use the Dynamic Link Library in C++ Linux (C++调用Delphi写的.so文件)
- How to use, monitor, and disable transparent hugepages in Red Hat Enterprise Linux 6
- How to create and extract zip, tar, tar.gz and tar.bz2 files in Linux
- How to Install Nvidia Kernel Module Cuda and Pyrit in Kali Linux
- What is SGID and how to set SGID in Linux?
- how to compile and install linux kernel
- How to Mount and Unmount Filesystem / Partition in Linux (Mount/Umount Command Examples)
- Java: How to compile and run Java application in command line(在命令行中编译运行java程序)
- How to compile and debug VLC player source code in eclipse IDE on Ubuntu 10.04 (lucid)?
- How to compile GCC-4.3.3 in the Linux?
- How to build & install GLFW 3 and use it in a Linux project
- How to solve problem caused when the CRT library and MFC libraries are linked in the wrong order
- How to burn Linux Kernel into nor flash and rootfs in USB stick at Mindspeed c1k
- How to Set and Unset Local, User and System Wide Environment Variables in Linux
- HowTo compile native C codes to a library for iOS development in Xcode – take Mosquitto as an exampl
- FFmpeg Understanding(4. How to compile and run testing files utilizing FFMPEG in VC?)
- How to install, compile, and utilize Rebar in Windows
- How to install NDK in window and compile ndk project
- How To Improve Laptop Battery Life And Usage In Linux Using TLP
- How to install LAMP (Apache, PHP and MySQL in Linux) using Yum