无法使用CMake和ncurses进行编译

时间:2017-04-07 20:03:47

标签: c++ c git gcc cmake

我知道已经发布了类似的问题,但我无法看到它们与我的案例有何关联。我会在这个问题前面说我对CMake不是很熟悉,所以完全有可能这是一个快速修复,我只是没有看到它!

我正在合作一个项目,昨天我的队友将ncurses库添加到项目中以构建终端GUI。自从ncurses被添加以来,我还没有能够编译我的项目。但是,我确实安装了所有6个ncurses- *软件包,所以它应该与我的机器一起运行。

我从我们的GitHub仓库中删除了最新版本的主分支,它在他的机器上编译并运行完美。然而,它甚至不能在我的作品上编译。

系统
Linux Mint 18.1 Cinnamon
CMake版本3.5.1

我尝试过的事情:
我删除了CMakeCache.txt并重新加载了它 我删除了整个CMake构建目录并重新制作了

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
project(irc)

set(SHARED_FLAGS " -Wall -Wextra -Wshadow -Werror -g -D_POSIX_C_SOURCE=200809L -lncurses")
set(CMAKE_C_FLAGS "-std=c11 ${SHARED_FLAGS}")
set(CMAKE_CXX_FLAGS "-std=c++11 ${SHARED_FLAGS}")

include_directories(include)

add_library(client_core SHARED src/client/irc_client.c)

add_executable(client src/client/irc_client_gui.c)
add_executable(server src/server/irc_server.c)

// SOLUTION - MISSING LINE
target_link_libraries(client ncurses)

install(FILES include/irc_client.h DESTINATION include)
install(FILES include/irc_server.h DESTINATION include)

CMakeOutput:

CMakeFiles/client.dir/src/client/irc_client_gui.c.o: In function `main':
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:17: 
undefined reference to `initscr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:18: 
undefined reference to `stdscr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:18: 
undefined reference to `stdscr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:18: 
undefined reference to `stdscr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:18: 
undefined reference to `stdscr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:19: 
undefined reference to `mvprintw'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:21: 
undefined reference to `stdscr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:21: 
undefined reference to `wgetnstr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:22: 
undefined reference to `LINES'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:22: 
undefined reference to `mvprintw'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:23: 
undefined reference to `stdscr'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:23: 
undefined reference to `wgetch'
/home/chrisjansson/Documents/irc/src/client/irc_client_gui.c:24: 
undefined reference to `endwin'
collect2: error: ld returned 1 exit status
CMakeFiles/client.dir/build.make:94: recipe for target 'client' failed
make[2]: *** [client] Error 1
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/client.dir/all' 
failed
make[1]: *** [CMakeFiles/client.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2

有趣的是,如果我在我的机器上的其他地方复制/粘贴irc_client_gui.c,使用gcc手动编译并运行它,它可以很好地工作。所以问题不在我的机器上,而是CMake尝试编译我的整个项目。有任何想法吗?非常感谢!

1 个答案:

答案 0 :(得分:1)

解决方案是添加如下一行:

target_link_libraries(client ncurses)

这告诉CMake当它链接client目标时,它应该使用-lncurses选项在ncurses库中进行链接。