Link ncurses in CLion CMake
Asked Answered
U

3

8

I'm currently playing around with ncurses. Ncurses is a library I installed, NOT my own file. I already did some stuff but using an IDE is much easier so I decided to use CLion (I'm on Linux so can't use Visual Studio). I got the following CMakeLists.txt:

cmake_minimum_required(VERSION 3.6)
project(ncurses)

set(CMAKE_C_STANDARD "${CMAKE_C_FLAGS} -Wall -Werror -lpdcurses")

set(SOURCE_FILES main.cpp ncurses.h)
add_executable(ncurses ${SOURCE_FILES})

My project is called ncurses I don't know if that'd matter.

I got the following main.cpp

#include <ncurses.h>

int main() {
    initscr();
    printw("Hello");
    refresh();
    getch();
    endwin();
    return 0;
}

However, I get the following errors:

/opt/clion/bin/cmake/bin/cmake --build /home/josh/ClionProjects/ncurses /cmake-build-debug --target all -- -j 4
make[2]: *** No rule to make target 'CMakeFiles/ncurses.dir/build'.  Stop.
make[1]: *** [CMakeFiles/Makefile2:68: CMakeFiles/ncurses.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

I don't get what the problem is. I tried -lncurses except of lpdcurses but that doesn't work either. It only gives an error when building but not in the IDEA itself.

Unionist answered 7/12, 2016 at 12:34 Comment(0)
C
4

in your CMakeLists.txt

just add :

set(CMAKE_CXX_FLAGS "-lncurses")
Chimaera answered 12/1, 2018 at 19:11 Comment(0)
S
4

For me solutions above did not work. However appending the last 4 lines to the code block below worked for me.

(Linux mint 20, Clion 2020.3)


CMakeLists.txt

cmake_minimum_required(VERSION 3.17)
project(<YOUR_PROJECT>)

set(CMAKE_CXX_STANDARD 14)

find_package(Curses REQUIRED)
include_directories(${CURSES_INCLUDE_DIR})
add_executable(<YOUR_PROJECT> main.cpp)
target_link_libraries(<YOUR_PROJECT> ${CURSES_LIBRARIES})

Change <YOUR_PROJECT> to your actual project name.

Skilled answered 11/5, 2021 at 12:18 Comment(0)
S
3

To tell the compiler to link a library using CMake, you should use the target_link_libraries() function.

Add this in your CMakeLists.txt:

target_link_libraries(${PROJECT_NAME} ncurses)

However, the error code that you have doesn't seem to be caused by linking. Try adding this line: set(CMAKE_CXX_STANDARD 17) before your add_executable(). Replace 17 by whatever C++ version you want. I'm pretty sure that it wont change anything but heh, worth a try. Also, don't forget to reload cmake project and reset the cache.

Snapshot answered 6/8, 2018 at 22:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.