c++ – How a main function is being called in a c/cpp project – Education Career Blog

Good day good-looking computer people,

I might be asking a bit too much, but here it goes.

I’m trying to do a bit of reserve engineering on this sound library. Looking at the main.cpp file (which I have posted below) it has two methods, setup and play. I’m a bit confused as to how this is working:

  1. When you run the Xcode project, shouldn’t there be a main function that is the first method called? I don’t see that here.

  2. The play function is being called (because I hear music), but it must be from elsewhere since in needs the argument output. Where could it be being called from?

To try to be a bit more specific, this is my question:

  1. If the program isn’t starting from a main method in the main.cpp file, where else could it be starting from ?

#include "maximilian.h"

double outputs2,moreoutputs2; //some track outputs
double filtered, ramped, filtered2; 
osc buffertest,ramp;
mix mymix,bobbins;//some panning busses
double env4={200,0,0,50};//the kick drum pitch envelope data
double env26={10000,0,9000,5,0,5};//the hi hat pitch envelope dat
envelope b,f;//two envelopers
sample beats;


extern int channels=2;//stereo-must be supported by hardware
extern int buffersize=256;//should be fine for most things
extern int samplerate=44100;//SR must be supported by card. It's always the default

void setup() {//some inits
    b.amplitude=env0;//starting value for envelope b
    f.amplitude=env20;//same for f
    beats.load("/Users/ericbrotto/Desktop/beats.wav");//put a path to a soundfile here.           Wav format only.
    printf("Summary:\n%s", beats.getSummary());//get info on samples if you like
 }

 void play(double *output) {//this is where the magic happens. Very slow magic.
    filtered2=beats.play(1*(1./34), 0, beats.length()); 
    bobbins.stereo(filtered2, moreoutputs, 0.5);//invert the pan
    output0=outputs0+moreoutputs0;//stick it in the out!!
    output1=outputs1+moreoutputs1;
 }

Thanks!

,

If the program isn’t starting from a
main method in the main.cpp file,
where else could it be starting from ?

From a main method in a different source file or pre-compiled library, probably, or definitely.

,

A main method is required and must have the name main unless you tell the compiler otherwise. It is possible that the main method gets included through a header or is implemented by a macro.

,

in the directory where you unpacked the library –

find . | xargs egrep "main"

It has to be there somewhere… 😉

,

  1. You’re dealing with a library, not a standalone program. It doesn’t have a main function, since it won’t run on its own. When a developer uses a library, such as this, his application calls out to the library’s functions (such as play).

  2. I don’t entirely understand from what you hear music. What have you compiled? An example program? In that case, that example program probably does have a main function and at some point calls the library’s play.

,

main is either getting defined in the the maximillian.h, something it includes, or it is already compiled into the maximillian library.

,

Put a breakpoints on the front of each function, run the app, and examine call stack when breakpoint is hit.

,

what i understand from it is the cpp file for the implementation of this library’s header it is not a complete program but when you use this lib in your program you have to do is

1) use its setup method to setup or load the file u want to play

2) you have to call play to play the sound

that it about this/.

,

For your question

If the program isn’t starting from a main method in the main.cpp file,
where else could it be starting from
?

Global objects and static members of classes will be initialized before call to main() function.

For below code

class Test
{
public:

};

Test* fun()
{
    return new Test;
}

Test *p = fun();

int main()
{
}

For initializing pointer p, fun() will be called before main() function.

,

Line 491 of maximillian.cpp

Leave a Comment