有个 c 文件,如何才能将其编译成动态链接库?
1个回答
展开全部
有二个文件,一个 test.h, 一个 test.c
// test.h
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
int add(int a, int b);
#ifdef __cplusplus
}
#endif
// test.c
#include "test.h"
int add(int a, int b)
{
return a + b;
}
编译,生成动态链接库:
# gcc -g3 -Wall -fPIC -shared -o libtest.so test.c
# ls -l
-rwxr-xr-x 1 root root 14618 Jul 24 13:44 libtest.so
-rw-r--r-- 1 root root 63 Jul 24 13:43 test.c
-rw-r--r-- 1 root root 106 Jul 24 13:43 test.h
# nm libtest.so
00000000002006e8 a _DYNAMIC
0000000000200890 a _GLOBAL_OFFSET_TABLE_
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
w _Jv_RegisterClasses
00000000002006c8 d __CTOR_END__
00000000002006c0 d __CTOR_LIST__
00000000002006d8 d __DTOR_END__
00000000002006d0 d __DTOR_LIST__
00000000000006b8 r __FRAME_END__
00000000002006e0 d __JCR_END__
00000000002006e0 d __JCR_LIST__
00000000002008b8 d __TMC_END__
00000000002008b8 A __bss_start
w __cxa_finalize@@GLIBC_2.2.5
0000000000000620 t __do_global_ctors_aux
0000000000000550 t __do_global_dtors_aux
00000000002008b0 d __dso_handle
w __gmon_start__
00000000002008b8 A _edata
00000000002008c8 A _end
0000000000000658 T _fini
0000000000000468 T _init
0000000000000600 T add
00000000000004a0 t call_gmon_start
00000000002008b8 b completed.6557
00000000000004c0 t deregister_tm_clones
00000000002008c0 b dtor_idx.6559
00000000000005d0 t frame_dummy
0000000000000500 t register_tm_clones
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询