Issue
I tried to write a demo program of drm, but I encountered undefined reference to
problems:
xjt@u16:~/SF6_RV1126/drmDemo/build$ make -j8
Scanning dependencies of target drmDemo
[ 50%] Building C object bin/CMakeFiles/drmDemo.dir/main.c.o
[100%] Linking C executable drmDemo
/home/xjt/SF6_RV1126/RV1126/prebuilts/gcc/linux-x86/arm/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/8.3.0/../../../../arm-linux-gnueabihf/bin/ld: CMakeFiles/drmDemo.dir/main.c.o: in function `modeset_create_fb':
main.c:(.text+0x6c): undefined reference to `drmIoctl'
/home/xjt/SF6_RV1126/RV1126/prebuilts/gcc/linux-x86/arm/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/8.3.0/../../../../arm-linux-gnueabihf/bin/ld: main.c:(.text+0xe0): undefined reference to `drmModeAddFB'
/home/xjt/SF6_RV1126/RV1126/prebuilts/gcc/linux-x86/arm/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/8.3.0/../../../../arm-linux-gnueabihf/bin/ld: main.c:(.text+0x100): undefined reference to `drmIoctl'
/home/xjt/SF6_RV1126/RV1126/prebuilts/gcc/linux-x86/arm/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/8.3.0/../../../../arm-linux-gnueabihf/bin/ld: CMakeFiles/drmDemo.dir/main.c.o: in function `modeset_destroy_fb':
main.c:(.text+0x194): undefined reference to `drmModeRmFB'
/home/xjt/SF6_RV1126/RV1126/prebuilts/gcc/linux-x86/arm/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/8.3.0/../../../../arm-linux-gnueabihf/bin/ld: main.c:(.text+0x1d4): undefined reference to `drmIoctl'
/home/xjt/SF6_RV1126/RV1126/prebuilts/gcc/linux-x86/arm/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/8.3.0/../../../../arm-linux-gnueabihf/bin/ld: CMakeFiles/drmDemo.dir/main.c.o: in function `main':
main.c:(.text+0x214): undefined reference to `drmModeGetResources'
/home/xjt/SF6_RV1126/RV1126/prebuilts/gcc/linux-x86/arm/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/8.3.0/../../../../arm-linux-gnueabihf/bin/ld: main.c:(.text+0x248): undefined reference to `drmModeGetConnector'
/home/xjt/SF6_RV1126/RV1126/prebuilts/gcc/linux-x86/arm/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/8.3.0/../../../../arm-linux-gnueabihf/bin/ld: main.c:(.text+0x2d4): undefined reference to `drmModeSetCrtc'
/home/xjt/SF6_RV1126/RV1126/prebuilts/gcc/linux-x86/arm/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/8.3.0/../../../../arm-linux-gnueabihf/bin/ld: main.c:(.text+0x2f0): undefined reference to `drmModeFreeConnector'
/home/xjt/SF6_RV1126/RV1126/prebuilts/gcc/linux-x86/arm/gcc-arm-8.3-2019.03-x86_64-arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/8.3.0/../../../../arm-linux-gnueabihf/bin/ld: main.c:(.text+0x2f8): undefined reference to `drmModeFreeResources'
collect2: error: ld returned 1 exit status
bin/CMakeFiles/drmDemo.dir/build.make:94: recipe for target 'bin/drmDemo' failed
make[2]: *** [bin/drmDemo] Error 1
CMakeFiles/Makefile2:85: recipe for target 'bin/CMakeFiles/drmDemo.dir/all' failed
make[1]: *** [bin/CMakeFiles/drmDemo.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
my cc tool chain is arm-linux-gnueabihf-
whic is produced from Buildroot. And I use CMake to build my program. Here is my project tree:
.
├── CMakeLists.txt # 1
├── src
│ ├── main.c
│ └── CMakeLists.txt # 2
└── build
in CML1:
PROJECT(RV1126)
CMAKE_MINIMUM_REQUIRED(VERSION 3.5)
SET(CMAKE_C_COMPILER "/home/xjt/SF6_RV1126/RV1126/buildroot/output/rockchip_rv1126_rv1109_dcir/host/bin/arm-linux-gnueabihf-gcc")
ADD_SUBDIRECTORY(src bin)
in CML2:
SET(
SRC_LIST
main.c
)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR/bin})
ADD_EXECUTABLE(drmDemo ${SRC_LIST})
I guess it's a linking problem, how should I modify CMake to link to libdrm?
Solution
As @Tsyvarev said, after ADD_EXECUTABLE()
add LINK_LIBRARIES("....../lib/libdrm.so ")
and TARGET_LINK_LIBRARIES(demo libdrm.so)
. Thanks again @Tsyvarev.
Answered By - Chroluma Answer Checked By - David Marino (WPSolving Volunteer)