CLion's auto-generated .h/.cpp files error with 'undefined reference to' function

Answered

In my main.cpp's folder, I do New>C/C++ Source File. I type in a name, check ‘Create an associated header’, and click ok. CMakeLists.txt is updated accordingly.

I create a simple dummy function, doStuff(), inside both the .cpp and .h files.  CLion/Clang autocompletes my #include that contains the new .h file, so it clearly sees the header file.  CLion autocompletes my typing of doStuff() in main.cpp, so it clearly sees that as well.  

CMakeLists.txt is automatically updated with the new .h and .cpp files: add_executable(firstdemo main.cpp test.cpp test.h)

When I build the project, I get:

main.cpp: undefined reference to ‘doStuff()’

 

This is just pathetic.

0
3 comments

The solution is that CLion doesn't actually run or compile the entire project by default, which makes absolutely no sense, but hey, when did paid software actually work intuitively or care about what its customers want?  For some reason, the default is to only compile and run main.cpp by itself.  Instead of using the default build and run icons in the top right of CLion, you need to go to the ‘Build’ menu up top and build the entire project instead of building main.cpp by itself. Afterwards, you then need to go to the ‘Run’ menu and run the entire project instead of the default of running main.cpp.  If you do this, the code inside the .h and .cpp files will build and run successfully.

Hopefully this will help someone in the future when they're searching why CLion doesn't handle header files.  It's very sad that I had to burn an entire day on this nonsense with Jetbrains giving the most useless response to my problem (pointing me to a CMake tutorial that had nothing to do with the problem).  They do not deserve the money I'm paying them to make such a terrible initial experience for programmers.  Fix this!!!

1

Hello!

Instead of using the default build and run icons in the top right of CLion

Those icons launch build/run/debug processes for the particular run/debug configuration selected in the toolbar switcher next to them:

If a run/debug configuration of the type CMake Application with your “firstdemo” target is selected there, then CLion will build/run/debug this configuration when you use those icons.

It seems that some other configuration was selected in the switcher when you tried to use those build/run/debug icons. Maybe it was a run/debug configuration of the type C/C++ File created for the main.cpp file.

0

Thank you Anna.  You seem to be correct that the run/debug configuration was set for main.cpp instead of the project.  I didn't know about this feature, so somehow the starting configuration pointed exclusively to main.cpp on its own instead of the entire project. That's not going to make sense to CLion newcomers.

Also, the information you provided needs to be added to the help files below since everyone looks at CMake issues when they have these kinds of problems (including a Jetbrain rep who pointed me to CMake as well):

https://www.jetbrains.com/help/clion/managing-cmake-project-files.html

https://www.jetbrains.com/help/clion/creating-files-from-templates.html

 

0

Please sign in to leave a comment.