Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link ncurses in CLion CMake

Tags:

cmake

clion

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.

like image 986
Joshua Bakker Avatar asked Dec 07 '16 12:12

Joshua Bakker


3 Answers

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.

like image 129
Kakise Avatar answered Sep 24 '22 15:09

Kakise


in your CMakeLists.txt

just add :

set(CMAKE_CXX_FLAGS "-lncurses")
like image 45
VelocityPulse Avatar answered Sep 25 '22 15:09

VelocityPulse


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.

like image 44
oriku Avatar answered Sep 22 '22 15:09

oriku