15

I am a total noob concerning CMake. My CMakeLists.txt is really basic:

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)

When I make, the linker does not find the ncurses commands and in the verbose mode of make, I see that the compiler did not add the -lncurses. What do I have to add to the CMakeLists to make it work?

Andy Zhang
  • 198
  • 4
  • 21
G-Mos
  • 173
  • 1
  • 2
  • 6
  • 2
    You should not set EXECUTABLE_OUTPUT_PATH relative to PROJECT_SOURCE_DIR as this makes it impossible to perform proper out-of-tree builds. – datenwolf Nov 01 '14 at 21:59

2 Answers2

37

For the super noob, remember target_link_libraries() needs to be below 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})
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
wafflecat
  • 1,144
  • 10
  • 15
  • I was using `Curses_INCLUDE_DIR` and `Curses_LIBRARIES` once I uppercased them, it worked – phoxd Jan 06 '20 at 20:35
12

before use some third party libs, you ought to find it! in case of ncurses you need to add find_package(Curses REQUIRED) and then use ${CURSES_LIBRARIES} in a call to target_link_libraries() and target_include_directories(... ${CURSES_INCLUDE_DIR}).

zaufi
  • 6,811
  • 26
  • 34
  • 4
    Thank you! This worked! For complete noobs it is target_link_libraries(your_exe ${CURSES_LIBRARIES}) – G-Mos Nov 06 '14 at 12:33