168

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.

hlin117
  • 20,764
  • 31
  • 72
  • 93

6 Answers6

158

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

starball
  • 20,030
  • 7
  • 43
  • 238
Daniel
  • 21,933
  • 14
  • 72
  • 101
  • I haven't tried your above suggestion yet, but would I be able to execute my C/C++ programs within CLion if I do so? (This would make the debugging process a ton easier.) – hlin117 Jan 10 '15 at 04:06
  • 3
    I verified that @Mondkin's solution works. If you're having trouble building the project after modifying the `Makefile` and `CMakeLists.txt`, try modifying a `*.cpp` file. This would have CLion build everything from scratch. – hlin117 Jan 13 '15 at 00:10
  • This works, but when I tried to add "make clean" to the configuration it fails. – 71GA Feb 27 '15 at 12:35
  • I think you should open a new question with the details of your problem. – Daniel Feb 27 '15 at 16:43
  • Does CLion set a variable when it "cleans" a project? can we pass an arg to clean ? – Yuriy Gettya Mar 03 '15 at 22:28
  • I don't think so, as if you look at the file "cmake_clean.cmake" inside your build directory, you will see that all it does is delete some files CMake created. Depending on what you want to accomplish, you may consider creating a custom "clean" target that does whatever you want. – Daniel Mar 04 '15 at 02:45
  • 1
    FYI In order to get this to work, at least on some projects, (e.g. redis), you have to add an 'ALL', making it: add_custom_target(mytest ALL COMMAND make -C ${mytest_SOURCE_DIR} CLION_EXE_DIR=${PROJECT_BINARY_DIR}) – Decker May 27 '16 at 16:48
  • Nice find! Now we don't need to maintain an extra CMakeFiles.txt just to appease Clion. For those who need to build from the command line (not just from within clion), note that CLION_EXE_DIR is not defined in that case, so just define your own Make variable (`OBJDIR=./cmake-build-debug/`) and generate your exe there `$(CC) ... -o $(OBJDIR)/$@` – Brent Faust Mar 28 '17 at 01:18
  • 1
    what should I change to make it working with gcc for C – Romain-p Apr 10 '17 at 20:10
  • 1
    CLion has just added support for Makefiles! – eri0o Jun 20 '20 at 21:13
36

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.

iled
  • 2,142
  • 3
  • 31
  • 43
  • 2
    Just a headsup, if you go this route, make sure you go into settings->make and make sure you point the plugin to the install directory of make. By default it looks in /usr/bin/make – Taylor Liss Nov 05 '17 at 22:00
  • 5
    I tried your method, but this gives only the opportunity to build your project, right? I can't run an application which was built with make with this plugin, or do I miss something? I have a project which has a quite complex Makefile and I don't want to write a CMake file for it but I would like to debug it using CLion ... – blacktemplar Dec 20 '17 at 09:33
  • @blacktemplar I don't know if you can use all debugging tools from CLion, but I should be able to compile and debug a binary file created with the above method. I just played a bit with it and I was able to use the step control buttons for instance. – iled Dec 20 '17 at 21:10
  • 2
    For more complex configurations this is so much easier than the accepted answer. Editing the makefile just to run that makefile is madness! – Kevin Sep 16 '18 at 11:08
  • 1
    How did you run the generated binary in clion? I create a new configuration for Application , then target: All targets , Executable: name of the binary, yet I get this error: "Cannot run `binary` on " – Kennet Celeste Oct 24 '18 at 14:15
  • @KennetCeleste Sorry, I can't remember the exact configuration I had. This was one year ago and I'm not using that same setup for a while now. I think I used to run the generated binary by pushing the "Run" button (green play-arrow). I'm not sure, but I think I had specified which binary to run in the run configuration window. – iled Oct 26 '18 at 22:43
  • @KennetCeleste I believe you have to use a specific target like the one found on the add_executable(yourTarget ) in your CMakeLists.txt instead of all targets. – kmk09k Dec 01 '18 at 07:57
  • @KennetCeleste Did you ever figure this out? – user2635088 Jan 14 '19 at 17:56
  • @user2635088 Based on [this comment](https://github.com/kropp/intellij-makefile/issues/79#issuecomment-423320237), I assume it's impossible. – Duncan Jones Feb 12 '19 at 14:16
9

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

scopchanov
  • 7,966
  • 10
  • 40
  • 68
Fengggli
  • 123
  • 1
  • 10
  • 1
    Nice the Jetbrains is working the problem, but as of 16 Nov 2018 its not useful because you can't debug the generated executable. – gerardw Nov 16 '18 at 14:47
  • @gerardw I guess you can modify the CFLAGS in the original makefile, so that the generated compile_commands.json will have those debug symbols added. – Fengggli Nov 16 '20 at 16:55
5

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.

13013SwagR
  • 433
  • 4
  • 13
  • 1
    My configuration as you described it says "Nothing to build on" even though the executable is set and exists. – donkey Feb 06 '19 at 23:41
  • That is right. You should build manually then run (without building). Note that newest version of CLion seems to better support MakeFiles. This solution still works, but I would consider it as **Deprecated** – 13013SwagR Feb 10 '19 at 15:42
  • What would you suggest for the latest version? I have the makefile plugin but it doesn't handle compilation/debugging. – donkey Feb 11 '19 at 01:07
  • You should be able to compile and I am guessing debug as well with the Makefile plugin. See the [Clion Doc.](https://www.jetbrains.com/help/clion/2019.1/using-file-watchers.html#CompDBExample). Though, I suggest you use CMake which is fully supported and avoids all the trouble. You could also set it up (I think) with the remote debug Template or you use GDB directly in the console. – 13013SwagR Feb 12 '19 at 03:21
1

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/

Gauthier
  • 538
  • 6
  • 12
1

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...

Uraza
  • 556
  • 3
  • 17
  • This worked for me. I was able to get CLion to recognize my source files as "active" by including them all in my `set(SOURCE_FILES all_files_here.c)` and then adding `add_executable(foobar_cmake ${SOURCE_FILES})` in addition to the custom target. Then just make sure CLion is set to build/run your custom target and not the _cmake one. – Kevin Mark Jun 02 '17 at 23:10