Surya
Surya

Reputation: 153

HOW to RUN multiple source files???Help needed please ( C++ CODEBLOCKS)

I have two different .cpp files (linked lists) under the same set of source of one project. I tried running one of the linked list files called "customer", but it's only running the other one called "video". How can I run the "customer" linked list file?

My customer.cpp file is active but it's still running the program for the "video" linked list file.

Basically Im trying to bring two seperate lists of customers and another seperate list with videos.

But when I try executing the program under the customer.cpp tab I thought it was supposed to run that but its running the video.cpp file...am I missing something here?

   #include <iostream>
    using namespace std;

    struct video
    {
      chartitle[40],star1[20],star2[20],star3[20],star4[20],prod[20],dir[20],proco[40];
     int copy;
     video *next;
     };
        video *first = NULL, *current = NULL;
      int optn = 0;

^this is my nodestructure for the video list the video.cpp file

      #include <iostream>
      using namespace std;

       struct customer
      {
       char f_name[20],l_name[20];
        int acc_num;
       customer *next;
        };
        customer *start = NULL, *pointer = NULL;
         int option = 0;

^this is my nodestructure for customer linked list.the customer.cpp file .both of these are in two seperate source files under the same project.

       int main(void)
        {
       first = NULL;
        current = NULL;
        do
       {
        display();
        cout << endl;
        cout << "Choose an option: " << endl;
        cout << "1. Move the current position forward once." << endl;
       cout << "2. Move the current position backwards once." << endl;
       cout << "3. Add a video at the beginning of the list." << endl;
        cout << "4. Add a video at the current position of the list." << endl;
       cout << "5. Add a video at the ending of the list." << endl;
       cout << "6. Delete the first video from the list." << endl;
       cout << "7. Delete the video at current position from the list." << endl;
       cout << "8. Delete the last video from the list." << endl;
       cout << "9. End program." << endl;
       cout << endl << " >> " ;
       cin >> optn;
     switch (optn)
    {
        case 1 : currentfor();
        break;
        case 2 : currentbac();
        break;
        case 3 : addbeginning();
        break;
        case 4 : addmiddle();
        break;
        case 5 : addending();
        break;
        case 6 : deletebegin();
        break;
        case 7 : deletemiddle();
        break;
        case 8 : deleteend();
        break;
    }
}
while (optn != 9);

}

^this is the code where i call all the functions for the video.cpp file.

 int mains(void)
 {
 start = NULL;
 pointer = NULL;
do
  {
    display_menu();
    cout << endl;
    cout << "Choose an option: " << endl;
    cout << "1. Move the current position forward once." << endl;
    cout << "2. Move the current position backwards once." << endl;
    cout << "3. Add a customer at the beginning of the list." << endl;
    cout << "4. Add a customer at the current position of the list." << endl;
    cout << "5. Add a customer at the ending of the list." << endl;
    cout << "6. Delete the first customer from the list." << endl;
    cout << "7. Delete the customer profile at current position from            the         list." << endl;
    cout << "8. Delete the last video from the list." << endl;
    cout << "9. End program." << endl;
    cout << endl << " >> " ;
    cin >> option;
     switch (option)
    {
        case 1 : current_forward();
        break;
        case 2 : current_backward();
        break;
        case 3 : add_beginning();
        break;
        case 4 : add_middle();
        break;
        case 5 : add_ending();
        break;
        case 6 : delete_beginning();
        break;
        case 7 : delete_middle();
        break;
        case 8 : delete_ending();
        break;
    }
}
while (option != 9);

}

^this is the final code where i call all functions for customer.cpp file... when I tried initially with int main(void) for the customer.cpp,the compiler showed an error saying that "main" was declared in both video.cpp and customer.cpp so I tried changing "main" to "mains" then it dint show any error...what did I miss here?

Upvotes: 1

Views: 16880

Answers (3)

ImtiazeA
ImtiazeA

Reputation: 1292

This is not an answer to the question, rather this is only for those who actually need to run multiple .c or .cpp files in Code::Blocks.

Instead of having a project, close the project from "File > Close project", now open your files and you can run those separately and it will work.

The compiler will build the files your the folder of the opened file, so you will get atleast 2 files in the same folder (with extensions "*.exe" and "*.o" for Windows).

Upvotes: 0

Mahesh
Mahesh

Reputation: 34665

I think you are under assumption that each source file in the project requires a main() function. main() is the starting point of execution of an executable. So, the entire executable should have only one main().

Edit 1

Only source files get compiled. Also, there are three stages which source files should pass through ( Preprocessor -> Compiler -> Linker). I will try to give you an idea of how to split. Assume that, we have two source files and a header -

  1. main.cpp
  2. foo.cpp
  3. foo.h

It is a custom to name the file as main.cpp where main() resides. Now -

foo.h: Usually declarations go here.

class foo
{
    int number ;

    public:
    foo( int n );
    int getNumber() const;
};

foo.cpp: Since, foo.cpp is a source file, it gets compiled. Now, to define the definitions of member functions, you need to include it's header. Just with out it, if you define foo member functions, compiler doesn't know what foo is.

#include "foo.h"

foo::foo( int n )
{
    number = n;
}

int foo::getNumber() const
{
    return number;
}

Before the compiler, the preprocessor copies the content of foo.h to the source file foo.cpp. Now, I intend to instantiate foo in my main.cpp. Now, main.cpp doesn't know what foo is. Each source is independent. Knowing to one source file doesn't mean that all the source files know it. So, you should include foo.h in main.cpp too. With out it, if you try to create an object for foo, the source file main.cpp doesn't know what the identifier foo means. So,

#include "foo.h"

int main()
{
    foo obj(10);
    obj.getNumber();

    return 0;
}

g++ main.cpp foo.cpp -o a.out. Now, my a.out is the executable whose starting point of execution is from main() defined in main.cpp. Hope it helps to an extent.

Upvotes: 5

Etienne de Martel
Etienne de Martel

Reputation: 36995

Well.

You can only have one main() function by program. And the reason is that main is the "entry point" for your program (it's the first function that's called by the runtime), so if you have more than one, the runtime won't know where to start. To avoid such problems, the linker will raise an error if a main function is defined more than once.

Now, is there a reason while they're part of the same source tree? Do they share anything? If not, then just compile and link them separately.

Upvotes: 0

Related Questions