How to add C++ compiler in `kdevelop`?

I’ve searched online for how to, but I didn’t notice any documentation. I understand how to add python3 as an interpreter, but I’m new to compiled languages, and the “Binary application” choice confuses me. Furthermore, I’m not even certain whether it’s the correct one. (Additionally, which choice in the menu bar compiles it? – “Debug launch”?)

I want this so that I need not have to type g++ -std=gnu++20 -o ./output ./source.cpp in the konsole KPart panel anymore.

KDevelop is supposed to preconfigure things up for you as long as you have (a) the required build tools and (b) your CMakeLists.txt set up to generate a target correctly.

Then you should see a fourth option in that combobox that has the name of the executable target. That’s the right one. If it doesn’t appear, it is possible that there is something wrong in your CMakeLists.txt.

Screenshot_20230222_001208

1 Like

Thank you!

I hadn’t created a CMakeLists.txt file. I apologize; this is my first time using C++. I tried to verify that this wasn’t a KDE-related problem, but Google provided no useful answers for my queries. The W3C tutorial didn’t mention such a file.

@Herzenschein, do you know of any resources or can point to me to a basic CMakeLists.txt file already on my KDE installation? I ask solely because either I’m useless at using search engines, or the basic developer documentation for this topic needs some serious SEO.


Would be good if this instance of Discourse used whatever plugin or feature allows marking a response as the answer.

can point to me to a basic CMakeLists.txt file already on my KDE installation?

I’m pretty sure it won’t be there in your installation. https://develop.kde.org/docs/use/kirigami/introduction-getting_started/ has explanation of somelines used in CMakeLists.txt

In your case, following should be enough i believe

project(HelloWorld)
set(CMAKE_CXX_STANDARD 20)
add_executable(output source.cpp)
1 Like

There’s currently no super satisfying tutorial that is both modern and explains the core functionality well IMO. There’s one on the way, but it will take a while to be finished.

This is the absolute minimum for a pure C++ project, following today’s best practices:

cmake_minimum_required(VERSION 3.15)
project(yourprojectname)
add_executable(maintargetexecutable)
target_sources(maintargetexecutable
    PRIVATE
    main.cpp
    class.cpp class.h
    otherclass.cpp otherclass.h
)
# No need to link any libraries (a.k.a. -l flag in g++/clang++)

You can also just pass the files directly through add_executable(), though it’s older and less flexible.

And this for Qt (and any other external libraries, like those from KDE Frameworks):

cmake_minimum_required(VERSION 3.15)
project(yourprojectname)
find_package(Qt5
    REQUIRED COMPONENTS
    Widgets # If you need QtWidgets
    Quick # If you need QtQuick
)

# Constructs the executable
add_executable(maintargetexecutable)
# Adds source files to the executable
target_sources(maintargetexecutable
    PRIVATE
    main.cpp
    class.cpp class.h
    otherclass.cpp otherclass.h
)
# We need linking here to actually use the Qt API
target_link_libraries(maintargetexecutable
    PRIVATE
    Qt5::Widgets Qt5::Quick
)

Both of these only compile things. If you need to install your app, you can use something like:

install(
    TARGETS maintargetexecutable
    # a.k.a. /usr/local/bin by default,
    # this can then be changed with CMAKE_INSTALL_PREFIX
    # in the project's settings
    DESTINATION ${CMAKE_INSTALL_BINDIR}
)

To set the C++ standard to 20, use the project’s settings (CMake options) and add -DCMAKE_CXX_STANDARD=20 to the extra arguments. You could probably change the environment settings to that as well.

If you really need to enforce the C++ standard in the CMakeLists.txt file, use target_compile_features(maintargetexecutable PRIVATE cxx_std_20). Using set() is the old way.

2 Likes

Turns out that all that I needed to do was:

Add > , per https://discuss.kde.org/t/185/2?u=rokejulianlockhart

Yeah this creates the cmakefile for you :slight_smile:

1 Like