Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile and run one random single C++ file in QtCreator (Linux Ubuntu)? [closed]

I'm new to Linux and QtCreator. I have many cpp files with different names that all have main functions in them. But I do not know how to run these cpp files one by one. I also need configuring parameters when running them. I assume Qt Creator doesn't allow compiling and running single cpp files with random names. But in here: https://bugreports.qt.io/browse/QTCREATORBUG-106, it said this problem is solved. But I still can not find out how to do it.

For a C++ project created via Qt Creator, I found I just need to click the green arrow then the main.cpp under the Sources folder will be compiled and run. But the project I am currently using is a C++ project I downloaded from the Internet (GraphChi). The structure of the files and folders are totally different with the ones created via Gt Creator. There's no cpp file "main.cpp", but there're many single cpp files with names of algorithms, each of which has a main function in it.

I just don't know how to compile them one by one or together, and run anyone of them.

like image 785
CyberPlayerOne Avatar asked Jan 12 '23 07:01

CyberPlayerOne


2 Answers

In Qt creator you are supposed to create a project, which has one cpp file containing the main function. It is not meant to compile single files (though you can by creating a project and copying the code in the main file of the project.

For you I would recommend using gcc from terminal. Open the terminal in the directory containing the file and type:

g++ filename.cpp

You can also use Geany (A simple but very nice IDE where you don't need to create a project. Open the file and compile it with a click.

like image 117
adnan kamili Avatar answered Jan 31 '23 00:01

adnan kamili


Qt Creator's usefullness comes from reasonable code completion, debugging, specification of build and execution environment, easy way to switch compilers, etc. This is all handy even if your code is just a single C++ file not using Qt!

The simplest way is to create a .pro file manually, and open it in Creator. It’s hard to beat three lines worth of a human readable “setup”.

CONFIG -= qt
CONFIG += console c++17
SOURCES = main.cpp
like image 20
Kuba hasn't forgotten Monica Avatar answered Jan 31 '23 01:01

Kuba hasn't forgotten Monica