使用 ncurses 和 cmake 链接期间未定义的引用

Undefined reference during linking using ncurses and cmake

我试图通过包含以下内容在我的 C++ 程序中使用 ncurses headers:

#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'

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

根据 CMAKE 的文档,您需要为 target_link_libraries 指定文件而不是目录。所以像

target_link_libraries(LearnC curses)

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