4.16日更新:
其实这个问题是CSAPP里讲过的,编译命令里面,前面的项依赖后面的项。所以只要调整-ldrm选项的位置即可:gcc -I/usr/include/drm modeset-plane-test.c -o modeset-plane-test -ldrm
===============================
环境是ubuntu,尝试编译的示例代码来自于这里:
https://github.com/hexiaolong2008/sample-code/tree/master/drm/application/modeset-plane-test
但是编译不停出错:
$ gcc -I/usr/include/drm -ldrm modeset-plane-test.c -o modeset-plane-test
/usr/bin/ld: /tmp/ccog2T3L.o: in function `modeset_create_fb':
modeset-plane-test.c:(.text+0x7b): undefined reference to `drmIoctl'
/usr/bin/ld: modeset-plane-test.c:(.text+0xda): undefined reference to `drmModeAddFB'
/usr/bin/ld: modeset-plane-test.c:(.text+0xf7): undefined reference to `drmIoctl'
/usr/bin/ld: /tmp/ccog2T3L.o: in function `modeset_destroy_fb':
modeset-plane-test.c:(.text+0x19f): undefined reference to `drmModeRmFB'
/usr/bin/ld: modeset-plane-test.c:(.text+0x1d8): undefined reference to `drmIoctl'
/usr/bin/ld: /tmp/ccog2T3L.o: in function `main':
modeset-plane-test.c:(.text+0x234): undefined reference to `drmModeGetResources'
/usr/bin/ld: modeset-plane-test.c:(.text+0x266): undefined reference to `drmSetClientCap'
/usr/bin/ld: modeset-plane-test.c:(.text+0x270): undefined reference to `drmModeGetPlaneResources'
/usr/bin/ld: modeset-plane-test.c:(.text+0x290): undefined reference to `drmModeGetConnector'
/usr/bin/ld: modeset-plane-test.c:(.text+0x2ff): undefined reference to `drmModeSetCrtc'
/usr/bin/ld: modeset-plane-test.c:(.text+0x34e): undefined reference to `drmModeSetPlane'
/usr/bin/ld: modeset-plane-test.c:(.text+0x374): undefined reference to `drmModeFreeConnector'
/usr/bin/ld: modeset-plane-test.c:(.text+0x380): undefined reference to `drmModeFreePlaneResources'
/usr/bin/ld: modeset-plane-test.c:(.text+0x38c): undefined reference to `drmModeFreeResources'
collect2: error: ld returned 1 exit status
看上去好像一切都没有问题啊。后来查到这个网页:
https://github.com/vicamo/drm-utils/commit/c83303780f7d88079a0824c5c93a9d02ad270b0f
里面描述到:
make: specify build recipe for drm-info
Implicit rules fails to build on Ubuntu:
drm-utils$ make
cc -Wall `pkg-config --libs libdrm` drm-info.o -o drm-info
/usr/bin/ld: drm-info.o: in function `getCapability':
drm-info.c:(.text+0x30): undefined reference to `drmGetCap'
...
It seems Ubuntu GCC silently adds --as-needed to the linkage options
at a position before the input files and libraries, which then
effectively ignores -ldrm when expanded.
于是按这个思路修改编译命令:
gcc modeset-plane-test.c `pkg-config --cflags libdrm` `pkg-config --libs libdrm` -o modeset-plane-test
编译顺利通过。
但是运行还是有问题,会core dump。
代码例子来自这里:
https://blog.csdn.net/hexiaolong2009/article/details/84934294
运行到
crtc_id = res->crtcs[0];
这里时出错,可能res是空吧。