C - 使用 CMAKE 构建 xbindkeys:错误 未知类型名称 'Display'



env:ubuntu20.4 cmake3.15

我从以下位置下载xbindkeys:https://www.nongnu.org/xbindkeys/并尝试使用cmake来构建它。我可以通过本机配置(autoconf(成功地构建它,但当我使用cmake时,总是会出现未知类型名称错误;cmakelist.txt:

cmake_minimum_required(VERSION 3.10)
project(xbindkeys LANGUAGES C VERSION 1.8.7)
#for x11
find_package(X11)
if(X11_FOUND)
message(STATUS "X11 found")
else()
message(STATUS "X11 NOT FOUND") 
endif(X11_FOUND)
#find_package(Boost  COMPONENTS log log_setup system thread REQUIRED)
aux_source_directory(. SRCS)
message(STATUS "all src:${SRCS}")
message(STATUS "X11 libries:${X11_LIBRARIES}")
file (GLOB H_Files "./*.h")
include_directories(./ ${X11_INCLUDE_DIR} /usr/include/X11/)
message(STATUS "cmake found x11 dir:${X11_INCLUDE_DIR}")
set(GCC_CXX_FLAGS "${GCC_CXX_FLAGS} -lX11")
add_executable(xbindkeys.app ${SRCS})
link_directories(${X11_LIBRARIES})
target_link_libraries(xbindkeys.app ${X11_LIBRARIES} -lX11)

生成makefile后,运行make。得到的消息如下:

[ 16%] Building C object CMakeFiles/xbindkeys.app.dir/get_key.c.o
[ 33%] Building C object CMakeFiles/xbindkeys.app.dir/grab_key.c.o
In file included from /home/eton/workspace/xbindkeys/grab_key.c:18:
/home/eton/workspace/xbindkeys/grab_key.h:21:23: error: unknown type name 鈥楧isplay鈥?
21 | extern void grab_keys(Display* dpy);
|                       ^~~~~~~
/home/eton/workspace/xbindkeys/grab_key.h:22:29: error: unknown type name 鈥楧isplay鈥?
22 | extern void ungrab_all_keys(Display* dpy);
|                             ^~~~~~~
/home/eton/workspace/xbindkeys/grab_key.h:24:37: error: unknown type name 鈥楧isplay鈥?
24 | extern void get_offending_modifiers(Display* dpy);
|                                     ^~~~~~~
/home/eton/workspace/xbindkeys/grab_key.c: In function 鈥榞rab_keys鈥?
/home/eton/workspace/xbindkeys/grab_key.c:223:16: warning: 鈥榅KeycodeToKeysym鈥?is deprecated [-Wdeprecated-declarations]
223 |                XKeysymToString(XKeycodeToKeysym(dpy, keys[i].key.code, 0)));
|                ^~~~~~~~~~~~~~~
In file included from /home/eton/workspace/xbindkeys/grab_key.c:20:
/usr/include/X11/Xlib.h:1687:15: note: declared here
1687 | extern KeySym XKeycodeToKeysym(
|               ^~~~~~~~~~~~~~~~
make[2]: *** [CMakeFiles/xbindkeys.app.dir/build.make:76: CMakeFiles/xbindkeys.app.dir/grab_key.c.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/xbindkeys.app.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

有人能帮忙吗?因为这个问题结合了C编程语言中的CMake和X11库,所以web中的信息不多。所以我来问。

最终我找到了答案。原因得到如下错误:

grab_key.h:21:23: error: unknown type name ‘Display’

21|extern void grab_keys(显示*dpy(;|^~~~~~~是头文件";grab_key.h";不包含X11标头。

我更改内容如下:

18 #ifndef __GRABKEY_H
19 #define __GRABKEY_H
20 
21 #include <X11/Xlib.h> //add this
22 #include <X11/keysym.h> //add this
23 extern void grab_keys(Display* dpy);
24 extern void ungrab_all_keys(Display* dpy);
25 
26 extern void get_offending_modifiers(Display* dpy);
27 
28 extern unsigned int numlock_mask;
29 extern unsigned int scrolllock_mask;
30 extern unsigned int capslock_mask;
31 
32 #endif /* __GRABKEY_H */

那么cmake生成的makefile就可以成功地创建二进制exe文件。

最新更新