Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make CLion insert generated code.... in .cpp files

Tags:

c++

clion

Generating code in CLion always result in having the methods implemented in the header files, I've always been taught that they should go in .cpp files, how can I change that behavior and is it even possible ?

Example :

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

The CMake file is as follow:

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 clion, I haven't 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 while being in test.hpp OR test.cpp only changes the 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
like image 501
Quentin Sommer Avatar asked Jan 16 '16 11:01

Quentin Sommer


People also ask

How do I add a source file to CPP?

Add a new source fileIn Solution Explorer, right-click the Source Files folder, point to Add, and then click New Item. In the Code node, click C++ File (. cpp), type a name for the file, and then click Add.

How do I save a CPP file in CLion?

You can always save your changes manually: Press Ctrl+S or select File | Save All from the main menu.


2 Answers

Ok, I have an actual solution for you. I had the same problem where I could do alt + enter and I could only auto generate per method. You can work around this via using alt + insert. This brings up the generation menu in Clion. From here select generate definitions which will bring up a menu where you can either just select all the definitions or select a select few you actually want to generate.

Clion is smart enough to know if you've already generated a definition so you don't have to worry about duplicate definitions here. I've found that with QT however that certain classes have Meta Object Compiler overrides that will show up here, so I make sure not to select those when creating definitions, but for most normal use cases selecting every element in the generate definitions list will just generate things you've actually defined in the header.

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

EDIT: Note if you actually want the behavior of the original author, you can select the "generate in place" option once you get to the generate definitions function selection screen

like image 86
Krupip Avatar answered Oct 15 '22 05:10

Krupip


CLion actually declares the methods in the header file and defines them in the .cpp file provided it 'knows' that both files are related. I noticed that when creating new source files, you have to reload cmake (CMake toolbar > "Reload CMake Project" icon) for the IDE to take the .cpp file into account, especially if you use file(GLOB xxx) to list source files.

Please also check that:

  • the .cpp file is listed as a source for a binary built with cmake
  • it properly includes its related header
  • the source and header files have the same basename

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

like image 35
etienne Avatar answered Oct 15 '22 07:10

etienne