No Int main()

General questions regarding the usage of CodeLite
philjynx
CodeLite Enthusiast
Posts: 27
Joined: Thu Mar 22, 2018 3:57 pm
Genuine User: Yes
IDE Question: c++
Contact:

No Int main()

Post by philjynx » Sun Mar 25, 2018 9:58 pm

I'm trying to implement an rtMidi callback, but cannot figure out where to put the code.
I do not know enough about C++ to figure this out and no amount of interweb searching has helped. Here's the problem, Codelite has generated this (with some additions from me).

Code: Select all

// Define the MainApp

class MainApp : public wxApp
{
public:
    
    MainApp()
    {
    }
    virtual ~MainApp()
    {
    }

    wxSingleInstanceChecker* m_checker;
   
    virtual bool OnInit()
    {
        // Add the common image handlers
        //midiin = 0;
        m_checker = new wxSingleInstanceChecker;

        if(m_checker->IsAnotherRunning()) {
            delete m_checker; // OnExit() won't be called if we return false
            m_checker = NULL;
            return false;
        }
        wxImage::AddHandler(new wxPNGHandler);
        wxImage::AddHandler(new wxJPEGHandler);

        MainFrame* mainFrame = new MainFrame(NULL);
        SetTopWindow(mainFrame);
        mainFrame->Show(true);
        return true;
    }
    virtual int OnExit()
    {
        delete m_checker;
        m_checker = NULL;
        return 0;
    }
};

DECLARE_APP(MainApp)
IMPLEMENT_APP(MainApp)
Here's a sample of how to implement that callback (from the authors of rtMidi)

Code: Select all

// cmidiin.cpp
#include <iostream>
#include <cstdlib>
#include "RtMidi.h"
void mycallback( double deltatime, std::vector< unsigned char > *message, void *userData )
{
  unsigned int nBytes = message->size();
  for ( unsigned int i=0; i<nBytes; i++ )
    std::cout << "Byte " << i << " = " << (int)message->at(i) << ", ";
  if ( nBytes > 0 )
    std::cout << "stamp = " << deltatime << std::endl;
}
int main()
{
  RtMidiIn *midiin = new RtMidiIn();
  // Check available ports.
  unsigned int nPorts = midiin->getPortCount();
  if ( nPorts == 0 ) {
    std::cout << "No ports available!\n";
    goto cleanup;
  }
  midiin->openPort( 0 );
  // Set our callback function.  This should be done immediately after
  // opening the port to avoid having incoming messages written to the
  // queue.
  midiin->setCallback( &mycallback );
  // Don't ignore sysex, timing, or active sensing messages.
  midiin->ignoreTypes( false, false, false );
  std::cout << "\nReading MIDI input ... press <enter> to quit.\n";
  char input;
  std::cin.get(input);
  // Clean up
 cleanup:
  delete midiin;
  return 0;
}
Note that

Code: Select all

void mycallback( double deltatime, std::vector< unsigned char > *message, void *userData )
above main.
I have tried putting that at the top of my file, it compiles fine, but causes (I think) a segment fault?

Any clues as to how I should emulate the sample layout given that there is no main in the generated file?
I imagine this is not a codelite issue at all, but I'm lumbered with what it has generated and do not know how to get round this.
Thanks.

DavidGH
CodeLite Plugin
Posts: 607
Joined: Wed Sep 03, 2008 7:26 pm
Contact:

Re: No Int main()

Post by DavidGH » Sun Mar 25, 2018 10:41 pm

Hi,
Any clues as to how I should emulate the sample layout given that there is no main in the generated file?
There is a main() but it's hidden in the IMPLEMENT_APP() macro. You could avoid that and write your own but I doubt if it'll be necessary.

Generally, the place to put things that would otherwise be in main() is in MainApp::OnInit, or else the frame constructor.
I have tried putting that at the top of my file, it compiles fine, but causes (I think) a segment fault?
Perhaps that allowed it to be used too soon, before things were instantiated? But that's a guess; I've not even heard of rtMidi.
I imagine this is not a codelite issue at all
Indeed. wxForum would have been a better choice, and you'd be more likely to get a knowlegeable answer.

Regards,

David

philjynx
CodeLite Enthusiast
Posts: 27
Joined: Thu Mar 22, 2018 3:57 pm
Genuine User: Yes
IDE Question: c++
Contact:

Re: No Int main()

Post by philjynx » Sun Mar 25, 2018 11:01 pm

Generally, the place to put things that would otherwise be in main() is in MainApp::OnInit, or else the frame constructor.
That's the thing, the callback should not be in main, it should precede it.
I've posted on wxWidgets as you suggest.
Thanks
Phil.

Post Reply