使用ncurses和cmake进行链接时未定义的引用

时间:2016-11-20 23:59:44

标签: c++ linker cmake ncurses clion

我正在尝试在我的C ++ - 程序中使用ncurses,包括以下标题:

#include <curses.h>
#include <menu.h>
#include <stdlib.h>

我正在使用CLion IDE,这是我的CMakeList.txt:

cmake_minimum_required(VERSION 3.6)
project(LearnC)

set(CURSES_USE_NCURSES TRUE)
find_package(Curses REQUIRED)
include_directories(${CURSES_INCLUDE_DIR})

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)
add_executable(LearnC ${SOURCE_FILES})
target_link_libraries(LearnC ${CURSES_INCLUDE_DIR})

编译顺利但是在链接时我收到了这些错误:

[ 20%] Linking CXX executable LearnC
CMakeFiles/LearnC.dir/main.cpp.o: In function `main':
../LearnC/main.cpp:20: undefined reference to `initscr'
../LearnC/main.cpp:22: undefined reference to `clear'
../LearnC/main.cpp:23: undefined reference to `noecho'
../LearnC/main.cpp:24: undefined reference to `curs_set'
../LearnC/main.cpp:25: undefined reference to `cbreak'
../LearnC/main.cpp:26: undefined reference to `nl'
../LearnC/main.cpp:27: undefined reference to `stdscr'
../LearnC/main.cpp:27: undefined reference to `keypad'

我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:4)

从CMAKE的文档中,您需要为target_link_libraries指定文件而不是目录。像

这样的东西
target_link_libraries(LearnC curses)

https://cmake.org/cmake/help/v2.8.12/cmake.html#command%3atarget_link_libraries