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
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.
You can always save your changes manually: Press Ctrl+S or select File | Save All from the main menu.
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
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:
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With