Amiga.org

Operating System Specific Discussions => Amiga OS => Amiga OS -- Development => Topic started by: Desler on May 02, 2003, 12:23:46 PM

Title: Opening libraries in c++
Post by: Desler on May 02, 2003, 12:23:46 PM
Recently I have thrown myself at the mercy of programming c++ on the amiga.
However lately I have come across this problem that I simply cannot open and use ANY library  and I cannot find any examples on the net how to do it.

Is there anyone outthere able to do this that would be so kind to give an example in c++. It would be much appreciated :-)

Im using gcc and my main purpose is to open intuition and use for opening a window.
(Uhh I hope this is not a real dumb question  :-) )
Title: Re: Opening libraries in c++
Post by: pjhutch on May 02, 2003, 01:36:38 PM
You need to have the Amiga Include files for exec, dos, intuition, graphics, gadtools etc to use Amiga Libraries
e.g.
#include
#include
.....
struct DosBase *DosBase;

DosBase = (struct DosBase*)OpenLibrary("dos.library, 39)
......
if (DosBase) CloseLibrary(DosBase);
....
Title: Re: Opening libraries in c++
Post by: Desler on May 02, 2003, 08:03:24 PM
No if only it was that easy. This is my problem excactly. It works fine under c but when changing to c++ you get different error messages.
I tried this piece of code:

#include

struct intuitionBase *IntuitionBase;
IntuitionBase = (struct IntuitionBase*)OpenLibrary("intuition.library", 39);

if (IntuitionBase) CloseLibrary(IntuitionBase);

void main()
{
}


and I got

ANSI C++ forbids declaration `IntuitionBase' with no type
4: conflicting types for `int IntuitionBase'
3: previous declaration as `struct intuitionBase * IntuitionBase'
4: implicit declaration of function `int OpenLibrary(...)'
4: initialization to `int' from `IntuitionBase *' lacks a cast
6: parse error before `if'

 :-?
Is there anyone who can tell me how to make this work
Title: Re: Opening libraries in c++
Post by: Piru on May 02, 2003, 08:54:37 PM
You didn't include .

// example

#include
#include
#include

#include
#include

struct IntuitionBase *IntuitionBase;

int main(void)
{
  IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 37);
  if (IntuitionBase)
  {
    // ...
    CloseLibrary((struct Library *) IntuitionBase);
  }
  return 0;
}
Title: Re: Opening libraries in c++
Post by: Desler on May 02, 2003, 10:42:16 PM
Thanks guys. I really appreciate your help. But its still not there. When I run the code I got from Piru Ill get this errorcode
10: initialization to `UBYTE *' from `const char *' discards qualifiers    

I know this is really rookie problems, but I really would like to know what I do wrong  :-(
Title: Re: Opening libraries in c++
Post by: Piru on May 02, 2003, 10:50:08 PM
It built just fine with gcc 2.95.3. Anyhow try:

IntuitionBase = (struct IntuitionBase *) OpenLibrary((CONST_STRPTR) "intuition.library", 37);
Title: Re: Opening libraries in c++
Post by: Shadow on May 03, 2003, 01:15:30 AM
From what I can see of your code I can see you spelled IntuitionBase with at small "i" and not at capital "I".

The other error you get:
Quote
10: initialization to 'UBYTE *' from 'const char *' discards qualifiers

can be fixed with something like this:
UBYTE *p = reinterpret_cast(const_cast(o));

where "o" has been defined as: const char *o somewhere.

This first removes the const qualifier from "o" and then casts it to a UBYTE pointer

And it looks like you are missing some includes.

Try this:

#include
#include
#include

#include
using namespace std;

struct IntutionBase *intuibase = NULL;

int main()
{
  intuibase = static_cast(OpenLibrary("intuition.library", 0));

  if(intuibase != NULL)
  {
    cout << "Yeah" << endl;
    CloseLibrary(static_cast(intuibase));
  }
  else
  {
    cout << "darn it" << endl;
  }
}

This should compile with g++ or StormC4 + stlport
Title: Re: Opening libraries in c++
Post by: Piru on May 03, 2003, 01:26:48 AM
Quote
#include

Wrong. Never ever include clib directly. Always use , in this case .

Quote
struct IntutionBase *intuibase = NULL;

That's IntuitionBase, or else he can't call any intuition functions (that's usually the reason for opening the library right?)
Title: Re: Opening libraries in c++
Post by: Shadow on May 03, 2003, 09:31:00 AM
Quote

Wrong. Never ever include clib directly. Always use , in this case .


Really?? I never heard that before. I am possitive I read somewhere allways to use clib/. But I could be wrong.

Quote

Quote
struct IntutionBase *intuibase = NULL;

That's IntuitionBase, or else he can't call any intuition functions (that's usually the reason for opening the library right?)


Ahh, I was actually very carefull not to make any mistakes, guess I missed that one.  sorry.