Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Multiple .cpp files

Multiple .cpp files

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++visual-studio
14 Posts 4 Posters 16 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Cedric Moonen

    gamefreak2291 wrote:

    So to get the "main" function of the second .cpp file to run i have to call it within the other one using the same method used for calling the two functions that made the two different colored boxes correct?

    Yes, but the problem is that you have a while loop in both of your functions, which means that you still won't achieve what you want. You need to have only one loop in which you execute your code.

    Cédric Moonen Software developer
    Charting control [v3.0] OpenGL game tutorial in C++

    G Offline
    G Offline
    gamefreak2291
    wrote on last edited by
    #5

    Yeah, that how the issue began, I want to have two continuous loops running. One looping at every possible interval(mouse position), the other looping every second or so(changing the colors of the squares). I was wanting to do this without having to create two separate executable files and running then side by side.

    C 1 Reply Last reply
    0
    • G gamefreak2291

      I'm not sure how using multiple .cpp files to create one .exe works but I'm attempted to using multiples to create just one .exe file. I'm currently using Microsoft Visual Studio 2008 and I've created a project built the project, and ran the file. I've either done something incorrect, or my idea of how this would work is wrong, in which case, my bad.

      #include "DarkGDK.h"

      void drawBlueBox();
      void drawRedBox();
      int x=319;

      void DarkGDK()
      {
      dbSetWindowOff(); // window mode off
      dbMaximiseWindow(); // maximise
      dbSetDisplayMode(1280, 1024, 32);

      while(x=319){
      dbCLS();
      drawBlueBox();
      drawRedBox();
      }
      dbWaitKey();
      

      }

      void drawBlueBox()
      {
      int red = dbRND(255);
      int green = dbRND(225);
      int blue = dbRND(225);

      DWORD blue1 = dbRGB(red, green, blue);
      DWORD black = dbRGB(0, 255, 0);
      
      dbInk(blue1, black);
      dbBox(0, 0, 639, 511);
      

      }

      void drawRedBox()
      {
      int red = dbRND(255);
      int green = dbRND(225);
      int blue = dbRND(225);

      DWORD red1 = dbRGB(red, green, blue);
      DWORD black = dbRGB(0, 255, 0);
      
      dbInk(red1, black);
      dbBox(640, 512, 1280, 1024);
      

      }

      #include "DarkGDK.h"

      int y=0;

      void main()
      {
      int width = dbScreenWidth();
      int height = dbScreenHeight();
      int mouseX = dbMouseX();
      int mouseY = dbMouseY();
      dbPositionMouse(dbScreenWidth()/2, dbScreenHeight()/2);
      DWORD white = dbRGB(255, 255, 255);
      DWORD black = dbRGB(0, 255, 0);

      while(y=0){
      dbInk(white, black);
      dbText(350, 150, "Screen Width: ");
      dbText(460, 150, dbStr(width));
      dbText(120, 350, "Screen Height: ");
      dbText(240, 350, dbStr(height));
      dbText(120, 363, "Mouse Position: (    ,    )");
      dbText(255, 363, dbStr(mouseX));
      dbText(297, 363, dbStr(mouseY));
      dbWait(50);
      }
      

      }

      The first .cpp file makes a full screen window that makes two randomly color changing boxes, while the second .cpp files tracks your mouses movement as you move it across the screen and displays it. The code on either .cpp files is running properly when compiled alone and into two separate .exe files. If someone could explain exactly how one .exe file made with multiple .cpp files would run I think that would also be helpful.

      K Offline
      K Offline
      KarstenK
      wrote on last edited by
      #6

      You got to understand how an exe is build. There some steps. One is that the compiler translatet the h/cpp files into obj-files and than the linker grab the obj files and creates the exe. Please refer for further details: http://msdn.microsoft.com/en-us/library/0k6kkbsd.aspx[^]

      Press F1 for help or google it. Greetings from Germany

      G 1 Reply Last reply
      0
      • G gamefreak2291

        Yeah, that how the issue began, I want to have two continuous loops running. One looping at every possible interval(mouse position), the other looping every second or so(changing the colors of the squares). I was wanting to do this without having to create two separate executable files and running then side by side.

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #7

        I suggest you take a look at some tutorials about game programming. What you need to do is only have one loop to handle the messages (e.g. mouse events) and you will need to keep track some information for each object that is drawn (its state). If you want to change the color of the object every second, then you need to remember also the time at which the color changed previously, and before you draw your object, you check if you have to change the color or not.

        Cédric Moonen Software developer
        Charting control [v3.0] OpenGL game tutorial in C++

        G 1 Reply Last reply
        0
        • K KarstenK

          You got to understand how an exe is build. There some steps. One is that the compiler translatet the h/cpp files into obj-files and than the linker grab the obj files and creates the exe. Please refer for further details: http://msdn.microsoft.com/en-us/library/0k6kkbsd.aspx[^]

          Press F1 for help or google it. Greetings from Germany

          G Offline
          G Offline
          gamefreak2291
          wrote on last edited by
          #8

          I do understand how the executable file is compiled, what I was not understanding is how the file is than actually executed.

          K 1 Reply Last reply
          0
          • C Cedric Moonen

            I suggest you take a look at some tutorials about game programming. What you need to do is only have one loop to handle the messages (e.g. mouse events) and you will need to keep track some information for each object that is drawn (its state). If you want to change the color of the object every second, then you need to remember also the time at which the color changed previously, and before you draw your object, you check if you have to change the color or not.

            Cédric Moonen Software developer
            Charting control [v3.0] OpenGL game tutorial in C++

            G Offline
            G Offline
            gamefreak2291
            wrote on last edited by
            #9

            I was recently given a brand new copy of a Game and Graphics textbook for C++ and I've been putting it to use. But I like to veer off and try things as I go that aren't explained in my reading materials. The book explained how to color sections of the screen, and from there I used the index to find out the other parts and attempted to combine a few functions I had been wanting to do before starting Visual programming. Nevertheless I plan on finishing the book and maybe it'll continue to help out.

            1 Reply Last reply
            0
            • G gamefreak2291

              I do understand how the executable file is compiled, what I was not understanding is how the file is than actually executed.

              K Offline
              K Offline
              KarstenK
              wrote on last edited by
              #10

              Sorry but you dont understand it. :mad: The cpp-file isnt executed. It got compiled and linked in the exe. It is like brewing beer: the parts got processed and the final product hasnt the original parts in it. pleaz: "Read the fine manual"

              Press F1 for help or google it. Greetings from Germany

              G 1 Reply Last reply
              0
              • K KarstenK

                Sorry but you dont understand it. :mad: The cpp-file isnt executed. It got compiled and linked in the exe. It is like brewing beer: the parts got processed and the final product hasnt the original parts in it. pleaz: "Read the fine manual"

                Press F1 for help or google it. Greetings from Germany

                G Offline
                G Offline
                gamefreak2291
                wrote on last edited by
                #11

                Sorry, but you're not helping at all. I do understand it. I understand what the compiler does. I know it processing the c++ code, converts it into machine code, creating object files, and then links them into the executable. The thing I did not understand was how the final product would run if there were multiple sources(these are also known as source files or .cpp files). I was attempting to see if when the final product was executed if the functions from each separate source would run along side one another, or if it worked the same as having multiple functions within one single .cpp file. So instead of judging my understanding of the process, try being helpful like the other fellow.

                E 1 Reply Last reply
                0
                • G gamefreak2291

                  Sorry, but you're not helping at all. I do understand it. I understand what the compiler does. I know it processing the c++ code, converts it into machine code, creating object files, and then links them into the executable. The thing I did not understand was how the final product would run if there were multiple sources(these are also known as source files or .cpp files). I was attempting to see if when the final product was executed if the functions from each separate source would run along side one another, or if it worked the same as having multiple functions within one single .cpp file. So instead of judging my understanding of the process, try being helpful like the other fellow.

                  E Offline
                  E Offline
                  Emilio Garavaglia
                  wrote on last edited by
                  #12

                  gamefreak2291 wrote:

                  I know it processing the c++ code, converts it into machine code, creating object files, and then links them into the executable

                  gamefreak2291 wrote:

                  The thing I did not understand was how the final product would run if there were multiple sources

                  This is a contradiction... You miss some aspects of the first point that makes you not having a proper understanding the second. I try to summarize in brief: - Each cpp file is a set of declaration that can be either object instances (aka "global variables") or function ("sequence of expressions and statements") each having a name. Some of those declaration are "external" other "internal" (by default, functions are "external", tgat means "visible outside the file they are in") - The translation the compiler does on each cpp file produces obj files where code is translated into machine code, and where external names are mapped in a symbol table. - The linker peeks all the obj-s and libraries and resolve the mapped names with their respective references they have. To let this process to succeed, all linked names must be unique (or mangled as such). One of the names (corresponding to the main function)is then mapped in the exe file as the "applicaion entry point" (well, not exacly, the entry point is an internally CRT initializer that calls main at the end...) so that when the operating system loads the application the execution will start from there. If you follow these three steps, there is no reason why your question should take place. The program flows in the way the various functions reciprocally call each other. No matter where they originally came from. There is no "parallelism" in a C++ classic program.

                  2 bugs found. > recompile ... 65534 bugs found. :doh:

                  G 1 Reply Last reply
                  0
                  • E Emilio Garavaglia

                    gamefreak2291 wrote:

                    I know it processing the c++ code, converts it into machine code, creating object files, and then links them into the executable

                    gamefreak2291 wrote:

                    The thing I did not understand was how the final product would run if there were multiple sources

                    This is a contradiction... You miss some aspects of the first point that makes you not having a proper understanding the second. I try to summarize in brief: - Each cpp file is a set of declaration that can be either object instances (aka "global variables") or function ("sequence of expressions and statements") each having a name. Some of those declaration are "external" other "internal" (by default, functions are "external", tgat means "visible outside the file they are in") - The translation the compiler does on each cpp file produces obj files where code is translated into machine code, and where external names are mapped in a symbol table. - The linker peeks all the obj-s and libraries and resolve the mapped names with their respective references they have. To let this process to succeed, all linked names must be unique (or mangled as such). One of the names (corresponding to the main function)is then mapped in the exe file as the "applicaion entry point" (well, not exacly, the entry point is an internally CRT initializer that calls main at the end...) so that when the operating system loads the application the execution will start from there. If you follow these three steps, there is no reason why your question should take place. The program flows in the way the various functions reciprocally call each other. No matter where they originally came from. There is no "parallelism" in a C++ classic program.

                    2 bugs found. > recompile ... 65534 bugs found. :doh:

                    G Offline
                    G Offline
                    gamefreak2291
                    wrote on last edited by
                    #13

                    The question should still exist, however it could have been slightly more direct. I understand how having multiple functions and variables withing a single .cpp work. I know extra functions must be called, from within the main function, to be 'executed'. I, however, did not understand how things would work having multiple .cpp files all with their own "main functions" because I had never been instructed, read about, or tried this before. Cedric Moonen understood exactly what I was doing wrong, made mention of it, and my confusion of how having multiple .cpp files work. I now understand it's used for organizational purposes, and works no differently that having multiple functions within one file. Nevertheless I appreciate the attempt at helping me.

                    E 1 Reply Last reply
                    0
                    • G gamefreak2291

                      The question should still exist, however it could have been slightly more direct. I understand how having multiple functions and variables withing a single .cpp work. I know extra functions must be called, from within the main function, to be 'executed'. I, however, did not understand how things would work having multiple .cpp files all with their own "main functions" because I had never been instructed, read about, or tried this before. Cedric Moonen understood exactly what I was doing wrong, made mention of it, and my confusion of how having multiple .cpp files work. I now understand it's used for organizational purposes, and works no differently that having multiple functions within one file. Nevertheless I appreciate the attempt at helping me.

                      E Offline
                      E Offline
                      Emilio Garavaglia
                      wrote on last edited by
                      #14

                      gamefreak2291 wrote:

                      I, however, did not understand how things would work having multiple .cpp files all with their own "main functions" because ...

                      After I told that in all a program (no matter with how many files) names bust be unique, the question become meaningless: What is the meaning (in mathematical sense) of the words "main functions"? If you link more file having each one a main function, the result is a linker error. You have never been instructed about how to do that, simply because it cannot work. Anyway, it doesn't matter how: the important thing is that you understood what was the problem.

                      2 bugs found. > recompile ... 65534 bugs found. :doh:

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups