PDA

View Full Version : How to run multiple C++ programs with individuals main functions within a C++ project



Cupidvogel
30th May 2014, 21:32
Hi, in QT, whenever I create any C++ project, it contains a main.cpp file by default. If I run it, it works fine. However, when I try to add another C++ file to the project with its corresponding main function, and after selecting that file name in the explore pane I click the Run button, it shows a build error.

I understand that in a project, there is supposed to be one main file with the main function in it while the others are invoked internally. However, for this project, I want to run individual files separately. How do I do it?

anda_skoa
31st May 2014, 11:12
Each program can only have one main() function.

If your project contains multiple programs, then you need a different top level project type, e.g subdirs project

Cheers,
_

Cupidvogel
31st May 2014, 13:36
Can you tell me how do it through subduers project as you mentioned?

anda_skoa
31st May 2014, 14:39
You create a new subdirs project.
You move your program project folders into the subdir project's folder.
You add the name of these sub folders to the SUBDIRS variable in the subdir project's .pro file

Cheers,
_

Cupidvogel
31st May 2014, 17:08
1. How do I create a subdirs project?

2. What do you mean by program's project folders? By default the project contains a pro file and a source folder under which I am creating the files.

Sorry if I am being naive. I am totally new to QT!

anda_skoa
31st May 2014, 17:59
1. How do I create a subdirs project?

New File or Project -> Other Project



2. What do you mean by program's project folders? By default the project contains a pro file and a source folder under which I am creating the files.

When you create a project it usually creates a folder with the same name. This folder then contains the .pro file and potentially other files.

Lets say you have a project called "MyProgram", then you should have a folder "MyProgram" somewhere.

You create a subdirs project, e.g. "MyMetaProject" and move "MyProgram" into that folder.
Then you edit MyMetaProject.pro to contain



SUBDIRS += MyProgram


Cheers,
_

Cupidvogel
31st May 2014, 20:37
I created a new subdirs project. It currently contains only a .pro file and nothing else. Right clicking it does not show any option for creating new file or new folder, only New Subproject. In the previous project HelloWorld, the Sources folder contained two files - main.cpp and main2.cpp. How do I copy them here?

anda_skoa
31st May 2014, 21:53
I assumed that whatever operating system you are using to run QtCreator has a file manager program that can move folders.
Is that not the case?

Cheers,
_

Cupidvogel
31st May 2014, 22:16
Not sure what you meant. I mean, yes, I know where these projects are located in the file system, so I can manually copy them from here to there. But I don't think that way they will show up in QT file explorer pane on the left. And I using OSX Mavericks..

anda_skoa
31st May 2014, 22:52
Ah, OSX user, lets try this, slowly, step by step then:

1) You have a project "MyProgram", located in a folder named "MyProgram". It contains, among other things, a MyProgram.pro file
2) You create a subdirs project named "MyMetaProject", which creates a folder named "MyMetaProject"
3) You use your operating system file manager, whatever that is, to move MyProgram into MyMetaProject. MyMetaProject will then have a MyMetaProject.pro file and a MyProgram sub folder
4) You open the MyMetaProject.pro file and add MyProgram to the SUBDIRS variable
5) You save the change
6) You watch a MyProgram folder appear in the project tree view

Cheers,
_