Using local makefile for CLion instead of CMake

后端 未结 6 703
醉酒成梦
醉酒成梦 2020-12-04 04:49

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.

相关标签:
6条回答
  • 2020-12-04 05:04

    While this is one of the most voted feature requests, there is one plugin available, by Victor Kropp, that adds support to makefiles:

    Makefile support plugin for IntelliJ IDEA

    Install

    You can install directly from the official repository:

    Settings > Plugins > search for makefile > Search in repositories > Install > Restart

    Use

    There are at least three different ways to run:

    1. Right click on a makefile and select Run
    2. Have the makefile open in the editor, put the cursor over one target (anywhere on the line), hit alt + enter, then select make target
    3. Hit ctrl/cmd + shift + F10 on a target (although this one didn't work for me on a mac).

    It opens a pane named Run target with the output.

    0 讨论(0)
  • 2020-12-04 05:05

    Currently, only CMake is supported by CLion. Others build systems will be added in the future, but currently, you can only use CMake.

    An importer tool has been implemented to help you to use CMake.

    Edit:

    Source : http://blog.jetbrains.com/clion/2014/09/clion-answers-frequently-asked-questions/

    0 讨论(0)
  • 2020-12-04 05:15

    I am not very familiar with CMake and could not use Mondkin's solution directly.

    Here is what I came up with in my CMakeLists.txt using the latest version of CLion (1.2.4) and MinGW on Windows (I guess you will just need to replace all: g++ mytest.cpp -o bin/mytest by make if you are not using the same setup):

    cmake_minimum_required(VERSION 3.3)
    project(mytest)
    
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
    
    add_custom_target(mytest ALL COMMAND mingw32-make WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
    

    And the custom Makefile is like this (it is located at the root of my project and generates the executable in a bin directory):

    all:
        g++ mytest.cpp -o bin/mytest
    

    I am able to build the executable and errors in the log window are clickable.

    Hints in the IDE are quite limited through, which is a big limitation compared to pure CMake projects...

    0 讨论(0)
  • 2020-12-04 05:17

    Newest version has better support literally for any generated Makefiles, through the compiledb

    Three steps:

    1. install compiledb

      pip install compiledb
      
    2. run a dry make

      compiledb -n make 
      

      (do the autogen, configure if needed)

    3. there will be a compile_commands.json file generated open the project and you will see CLion will load info from the json file. If you your CLion still try to find CMakeLists.txt and cannot read compile_commands.json, try to remove the entire folder, re-download the source files, and redo step 1,2,3

    Orignal post: Working with Makefiles in CLion using Compilation DB

    0 讨论(0)
  • 2020-12-04 05:19

    To totally avoid using CMAKE, you can simply:

    1. Build your project as you normally with Make through the terminal.

    2. Change your CLion configurations, go to (in top bar) :
      Run -> Edit Configurations -> yourProjectFolder

    3. Change the Executable to the one generated with Make

    4. Change the Working directory to the folder holding your executable (if needed)

    5. Remove the Build task in the Before launch:Activate tool window box

    And you're all set! You can now use the debug button after your manual build.

    0 讨论(0)
  • 2020-12-04 05:23

    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:

    • add_custom_target
    • add_custom_command

    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

    0 讨论(0)
提交回复
热议问题