Search code examples
makefileclion

Using local makefile for CLion instead of CMake


Is there a way to configure CLion to use a local makefile to compile code, rather than CMake? I can't seem to find the way to do it from the build options.


Solution

  • Update: If you are using CLion 2020.2, then it already supports Makefiles. If you are using an older version, read on.


    Even though currently only CMake is supported, you can instruct CMake to call make with your custom Makefile. Edit your CMakeLists.txt adding one of these two commands:

    When you tell CLion to run your program, it will try to find an executable with the same name of the target in the directory pointed by PROJECT_BINARY_DIR. So as long as your make generates the file where CLion expects, there will be no problem.

    Here is a working example:

    Tell CLion to pass its $(PROJECT_BINARY_DIR) to make

    This is the sample CMakeLists.txt:

    cmake_minimum_required(VERSION 2.8.4)
    project(mytest)
    
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
    
    add_custom_target(mytest COMMAND make -C ${mytest_SOURCE_DIR}
                             CLION_EXE_DIR=${PROJECT_BINARY_DIR})
    

    Tell make to generate the executable in CLion's directory

    This is the sample Makefile:

    all:
        echo Compiling $(CLION_EXE_DIR)/$@ ...
        g++ mytest.cpp -o $(CLION_EXE_DIR)/mytest
    

    That is all, you may also want to change your program's working directory so it executes as it is when you run make from inside your directory. For this edit: Run -> Edit Configurations ... -> mytest -> Working directory