PDA

View Full Version : Opening libraries in c++


Desler
05-02-2003, 05:23 AM
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 :-) )

pjhutch
05-02-2003, 06:36 AM
You need to have the Amiga Include files for exec, dos, intuition, graphics, gadtools etc to use Amiga Libraries
e.g.
#include <exec.h>
#include <dos.h>
.....
struct DosBase *DosBase;

DosBase = (struct DosBase*)OpenLibrary("dos.library, 39)
......
if (DosBase) CloseLibrary(DosBase);
....

Desler
05-02-2003, 01:03 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 <intuition/intuition.h>

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

Piru
05-02-2003, 01:54 PM
You didn't include <proto/exec.h>.

// example

#include <exec/types.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>

#include <proto/exec.h>
#include <proto/intuition.h>

struct IntuitionBase *IntuitionBase;

int main(void)
{
IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 37);
if (IntuitionBase)
{
// ...
CloseLibrary((struct Library *) IntuitionBase);
}
return 0;
}

Desler
05-02-2003, 03:42 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 :-(

Piru
05-02-2003, 03:50 PM
It built just fine with gcc 2.95.3. Anyhow try:

IntuitionBase = (struct IntuitionBase *) OpenLibrary((CONST_STRPTR) "intuition.library", 37);

Shadow
05-02-2003, 06:15 PM
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:
10: initialization to 'UBYTE *' from 'const char *' discards qualifiers
can be fixed with something like this:
UBYTE *p = reinterpret_cast<UBYTE *>(const_cast<char *>(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 <exec/types.h>
#include <intuition/intuition.h>
#include <clib/exec_protos.h>

#include <iostream>
using namespace std;

struct IntutionBase *intuibase = NULL;

int main()
{
intuibase = static_cast<struct IntuitionBase *>(OpenLibrary("intuition.library", 0));

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

This should compile with g++ or StormC4 + stlport

Piru
05-02-2003, 06:26 PM
#include <clib/exec_protos.h>
Wrong. Never ever include clib directly. Always use <proto/libname.h>, in this case <proto/exec.h>.

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?)

Shadow
05-03-2003, 02:31 AM
Wrong. Never ever include clib directly. Always use <proto/libname.h>, in this case <proto/exec.h>.

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


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.