How to make CLIIN generated code .... in .cpp files

Generating code in CLion always leads to the fact that the methods implemented in the header files have always taught me that they should be included in .cpp files, how can I change this behavior and is this possible?

Example:

In a project containing main.cpp and a test class (test.hpp and test.cpp).

The CMake file is as follows:

cmake_minimum_required(VERSION 3.3) project(testClion) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") set(SOURCE_FILES main.cpp test.cpp test.hpp) add_executable(testClion ${SOURCE_FILES}) 

(note that this is the default file provided by the client, I have not changed anything)

test.hpp

 #ifndef TESTCLION_TEST_HPP #define TESTCLION_TEST_HPP class test { protected: int test; }; #endif //TESTCLION_TEST_HPP 

test.cpp

 #include "test.hpp" 

Pressing ALT + INSERT and generating getters / setters during testing. hpp OR test.cpp only modifies test.hpp:

test.hpp

 #ifndef TESTCLION_TEST_HPP #define TESTCLION_TEST_HPP class test { public: int getTest() const { return test; } void setTest(int test) { test::test = test; } protected: int test; }; #endif //TESTCLION_TEST_HPP 
+6
source share
3 answers

Ok, I have a real solution for you. I had the same problem that I could do alt + enter , and I could only auto-generate each method. You can get around this using alt + insert . This brings up the generation menu in Clion. From here, select generate definitions , in which a menu appears in which you can either select all definitions or select several favorites that you want to create.

Clion is smart enough to know you already created a definition, so you don't have to worry about duplicate definitions here. I found that with QT, however, some classes have Meta Object compiler overrides that will be displayed here, so I won’t select them when creating definitions, but for most use cases, when each item in the generate definitions list just create things. which you actually defined in the title.

Note that you can also right-click on the name of your class and go to Generate... and you will be given the same options.

EDIT: note, if you really want the original author’s behavior, you can select the “generate in-place” option as soon as you go to the screen for selecting the definition generation function

+1
source

CLion actually declares the methods in the header file and defines them in the .cpp file, provided that it “knows” that both files are related. I noticed that when creating new source files, you need to reload cmake (CMake toolbar> "Restart CMake project") so that the IDE takes into account the .cpp file, especially if you use file(GLOB xxx) to display the source files.

Also make sure that:

  • .cpp file specified as source for binary created with cmake
  • it correctly includes the associated header
  • source and header files have the same base name

Note. CLion provides an easy way to move a method implementation to the source file, if necessary. Place the cursor in the method in the header file, press Alt + Enter and press Move function definition to source file .

+3
source

I had the same problem when I first started using CLion. I was able to determine where to implement the methods using the Actions menu (yellow globe). I was confused trying to use the wrong hotkey.

Currently in version 2016.1.2:

  • Place the cursor on the method.
  • Open the Intention Actions menu by hovering nearby and clicking on the globe or by pressing a hot key
    • Windows by default: alt + enter
    • OS X Default: option + enter
  • Select the appropriate action.
    • Generate definition for function 'someFunction' to add a definition to the source file.
    • Create new function 'someFunction()' to add the definition to the header file (where the implementation is already done in the source).

enter image description here

+1
source

All Articles