Results 1 to 10 of 10

Thread: How to run multiple C++ programs with individuals main functions within a C++ project

  1. #1
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default How to run multiple C++ programs with individuals main functions within a C++ project

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to run multiple C++ programs with individuals main functions within a C++ pro

    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,
    _

  3. #3
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: How to run multiple C++ programs with individuals main functions within a C++ pro

    Can you tell me how do it through subduers project as you mentioned?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to run multiple C++ programs with individuals main functions within a C++ pro

    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,
    _

  5. #5
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: How to run multiple C++ programs with individuals main functions within a C++ pro

    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!

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to run multiple C++ programs with individuals main functions within a C++ pro

    Quote Originally Posted by Cupidvogel View Post
    1. How do I create a subdirs project?
    New File or Project -> Other Project

    Quote Originally Posted by Cupidvogel View Post
    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

    Qt Code:
    1. SUBDIRS += MyProgram
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  7. #7
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: How to run multiple C++ programs with individuals main functions within a C++ pro

    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?

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to run multiple C++ programs with individuals main functions within a C++ pro

    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,
    _

  9. #9
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: How to run multiple C++ programs with individuals main functions within a C++ pro

    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..

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to run multiple C++ programs with individuals main functions within a C++ pro

    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,
    _

Similar Threads

  1. Refresh the mainwindow / functions called in the main loop
    By valerianst in forum Qt Programming
    Replies: 7
    Last Post: 2nd October 2013, 14:57
  2. QProcess in main() opens multiple instances
    By gojkovicde in forum Newbie
    Replies: 2
    Last Post: 10th December 2012, 07:43
  3. main.cpp in creating new QWidget project
    By iamDAMON in forum Qt Programming
    Replies: 2
    Last Post: 1st October 2012, 20:20
  4. Build two programs in one project in creator
    By Gunnar in forum Qt Tools
    Replies: 1
    Last Post: 13th October 2010, 08:52
  5. Change Main Entry Point in Qt Project File
    By Sanuden in forum Qt Programming
    Replies: 0
    Last Post: 24th February 2010, 18:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.