CMake无法链接ncurses。

16

我对CMake完全是个新手。我的CMakeLists.txt非常基础:

cmake_minimum_required(VERSION 2.4.6)
#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

#For the Curses library to load:
SET(CURSES_USE_NCURSES TRUE)

include_directories(
     "src/"
)
add_subdirectory(src)

当我进行编译时,链接器无法找到ncurses命令,并且在make的详细模式中,我看到编译器没有添加-lncurses。我需要在CMakeLists中添加什么才能使它正常工作?


2
不应将EXECUTABLE_OUTPUT_PATH设置为相对于PROJECT_SOURCE_DIR,因为这会导致无法进行正确的外部构建。 - datenwolf
2个回答

39

对于超级新手来说,记住target_link_libraries()需要在add_executable()之后:

cmake_minimum_required(VERSION 2.8) project(main)

find_package(Curses REQUIRED)
include_directories(${CURSES_INCLUDE_DIR})

add_executable(main main.cpp)
target_link_libraries(main ${CURSES_LIBRARIES})

我曾经使用了 Curses_INCLUDE_DIRCurses_LIBRARIES,一旦我将它们大写,它就起作用了。 - phoxd

12

在使用第三方库之前,你应该先找到它!对于 ncurses ,你需要添加 find_package(Curses REQUIRED) ,然后在调用 target_link_libraries()target_include_directories(... ${CURSES_INCLUDE_DIR}) 时使用 ${CURSES_LIBRARIES}


4
谢谢!有效了!对于完全的新手来说,这意味着在编译你的程序时需要加上target_link_libraries(your_exe ${CURSES_LIBRARIES})。 - G-Mos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接