代码拉取完成,页面将自动刷新
实现功能:
$ tree
.
├── apps
│ ├── app1 # 编译app1的源文件
│ │ └── main.c
│ └── app2 # 编译app2的源文件
│ └── main.c
├── build # CMAKE构建输出目录
├── cmake
│ ├── app.cmake # 构建app
│ ├── config.cmake # 构建配置
│ ├── kernel.cmake # 构建KO
│ ├── lib.cmake # 构建库
│ ├── target.cmake # 构建目标
│ └── user.cmake # 用户态相关构建
├── CMakeLists.txt
├── core # 编译KO源文件
│ └── module.c
├── docs # 当前未使用
├── extern_lib # 外部三方库路径
│ ├── extlib.h
│ └── libext.so
├── include
│ ├── add
│ │ └── add.h
│ ├── print.h
│ └── sub
│ └── sub.h
├── README.md
├── src # 编译so源文件路径
│ ├── add
│ │ └── add.c
│ ├── print.c
│ └── sub
│ └── sub.c
└── tests # 当前未使用
cmake -S . -B build
-S
:表示源代码目录的路径.
:表示当前目录,CMake 在当前目录中查找 CMakeLists.txt
文件,作为项目的源代码目录。-B
:表示构建目录的路径build
:是构建目录的名称,CMake 会在当前目录下创建一个名为 build
的目录(如果不存在),并将生成的构建文件(如 Makefile
或其他构建系统所需的文件)存储在该目录中。如果需要冗余方式构建,类似make V=1
,则可以执行命令:
cmake -S . -B build -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON
进入build目录,然后执行make
$ cd build/
$ make
Scanning dependencies of target ko
[ 11%] SHELL_OUTPUT
[ 11%] Built target ko
Scanning dependencies of target my_test
[ 22%] Building C object CMakeFiles/my_test.dir/src/print.c.o
[ 33%] Building C object CMakeFiles/my_test.dir/src/add/add.c.o
[ 44%] Building C object CMakeFiles/my_test.dir/src/sub/sub.c.o
[ 55%] Linking C shared library libmy_test.so
[ 55%] Built target my_test
Scanning dependencies of target app2
[ 66%] Building C object CMakeFiles/app2.dir/apps/app2/main.c.o
[ 77%] Linking C executable app2
[ 77%] Built target app2
Scanning dependencies of target app1
[ 88%] Building C object CMakeFiles/app1.dir/apps/app1/main.c.o
[100%] Linking C executable app1
[100%] Built target app1
查看输出文件:
$ file libmy_test.so
libmy_test.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=430735ea7148d002a148f8d51e0041f2217dd309, not stripped
$ file my_test.ko
my_test.ko: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), BuildID[sha1]=6abe75f9a2bf873f2cb1e8321b88dd5d2f9791c4, not stripped
$ ldd app1
linux-vdso.so.1 => (0x00007ffe0899d000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f2045de9000)
libm.so.6 => /lib64/libm.so.6 (0x00007f2045ae7000)
libmy_test.so => /home/extended-project/build/libmy_test.so (0x00007f20458e5000)
libext.so => /home/extended-project/extern_lib/libext.so (0x00007f20456e3000)
libc.so.6 => /lib64/libc.so.6 (0x00007f2045315000)
/lib64/ld-linux-x86-64.so.2 (0x00007f2046005000)
$ ldd app2
linux-vdso.so.1 => (0x00007ffdc8b92000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f717f6cb000)
libm.so.6 => /lib64/libm.so.6 (0x00007f717f3c9000)
libmy_test.so => /home/extended-project/build/libmy_test.so (0x00007f717f1c7000)
libc.so.6 => /lib64/libc.so.6 (0x00007f717edf9000)
/lib64/ld-linux-x86-64.so.2 (0x00007f717f8e7000)
gcc -Dmy_test_EXPORTS -fPIC -lpthread -lm -Wall -Werror -g -O0 -fstack-protector-strong -I/home/extended-project/include/ -I/home/extended-project/include/add -I/home/extended-project/include/sub -o CMakeFiles/my_test.dir/src/print.c.o -c /home/extended-project/src/print.c
gcc -Dmy_test_EXPORTS -fPIC -lpthread -lm -Wall -Werror -g -O0 -fstack-protector-strong -I/home/extended-project/include/ -I/home/extended-project/include/add -o CMakeFiles/my_test.dir/src/add/add.c.o -c /home/extended-project/src/add/add.c
gcc -Dmy_test_EXPORTS -fPIC -lpthread -lm -Wall -Werror -g -O0 -fstack-protector-strong -I/home/extended-project/include/ -I/home/extended-project/include/sub -o CMakeFiles/my_test.dir/src/sub/sub.c.o -c /home/extended-project/src/sub/sub.c
gcc -I/home/extended-project/include -I/home/extended-project/include/sub -lpthread -lm -Wall -Werror -g -O0 -o CMakeFiles/app2.dir/apps/app2/main.c.o -c /home/extended-project/apps/app2/main.c
gcc CMakeFiles/app2.dir/apps/app2/main.c.o -o app2 -Wl,-rpath,/home/extended-project/build -lpthread -lm libmy_test.so -lpthread -lm
gcc -I/home/extended-project/include -I/home/extended-project/include/add -I/home/extended-project/extern_lib -lpthread -lm -Wall -Werror -g -O0 -o CMakeFiles/app1.dir/apps/app1/main.c.o -c /home/extended-project/apps/app1/main.c
gcc CMakeFiles/app1.dir/apps/app1/main.c.o -o app1 -L/home/extended-project/extern_lib -Wl,-rpath,/home/extended-project/build:/home/extended-project/extern_lib -lpthread -lm libmy_test.so -lext -lpthread -lm
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。