Poster | Thread |
Robert
| |
Noob C problem Posted on 3-Mar-2018 15:41:53
| | [ #1 ] |
|
|
|
Cult Member |
Joined: 10-Mar-2003 Posts: 879
From: Glasgow | | |
|
| Hi folks,
I'm trying to learn some really simple C but can't seem to get past the very basics.
Trying to follow some simple tutorials, I can't get this to compile:
Quote:
struct Library *OpenLibrary(); struct IntuitionBase *IntuitionBase;
int main() { IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",39); return 0; }
|
The error is Quote:
undefined reference to `OpenLibrary' |
I'm aware this is probably something stupid but would appreciate if anyone can point it out to me.
CheersLast edited by Robert on 03-Mar-2018 at 04:13 PM.
_________________ Robert -- A1XE G4, OS4.1. Peg1 G3, MOS 1.4. Abel Soul - Check out our tunes on Spotify |
|
Status: Offline |
|
|
Kronos
| |
Re: Noob C problem Posted on 3-Mar-2018 15:51:24
| | [ #2 ] |
|
|
|
Elite Member |
Joined: 8-Mar-2003 Posts: 2561
From: Unknown | | |
|
| @Robert
You are defining a function OpenLibrary() (otherwise it would complain at compiler level), but you fail to implement it or tell the linker where to find it.
A lot can be different depending on which compiler you use, how it is invoked on which version of AOS is the target.
Try replacing that line with:
#include >proto/exec.h>
Edit: Forum doesn't like the left-hand version of ">" so turn the 1st around..... Last edited by Kronos on 03-Mar-2018 at 03:54 PM. Last edited by Kronos on 03-Mar-2018 at 03:52 PM.
_________________ - We don't need good ideas, we haven't run out on bad ones yet - blame Canada |
|
Status: Offline |
|
|
Robert
| |
Re: Noob C problem Posted on 3-Mar-2018 16:06:03
| | [ #3 ] |
|
|
|
Cult Member |
Joined: 10-Mar-2003 Posts: 879
From: Glasgow | | |
|
| @Kronos
Quote:
Kronos wrote: @Robert
You are defining a function OpenLibrary() (otherwise it would complain at compiler level), but you fail to implement it or tell the linker where to find it.
A lot can be different depending on which compiler you use, how it is invoked on which version of AOS is the target.
|
Thanks Kronos. The compiler is GCC 4.2.4, it's invoked from shell (e.g. gcc -o test test.c) and the OS is AmigaOS4.1.
Quote:
Try replacing that line with:
#include >proto/exec.h>
Edit: Forum doesn't like the left-hand version of ">" so turn the 1st around..... |
Thanks but still seeing the same error:
undefined reference to `OpenLibrary'Last edited by Robert on 03-Mar-2018 at 04:14 PM.
_________________ Robert -- A1XE G4, OS4.1. Peg1 G3, MOS 1.4. Abel Soul - Check out our tunes on Spotify |
|
Status: Offline |
|
|
Karlos
| |
Re: Noob C problem Posted on 3-Mar-2018 16:38:46
| | [ #4 ] |
|
|
|
Elite Member |
Joined: 24-Aug-2003 Posts: 4402
From: As-sassin-aaate! As-sassin-aaate! Ooh! We forgot the ammunition! | | |
|
| @Robert
Hey up. So, I think a problem you are facing is that Libraries in OS4 work rather differently than they did in 3.x.
Depending on how your compiler is configured, you'll want to use something like declaring:
struct IntuitionBase* IntuitionBase = NULL; struct IntuitionIFace* IIntuition = NULL;
Get the library base:
IntuitionBase = (struct IntuitionBase*)IExec->OpenLibrary(...);
and then obtain the interface for it:
IIntuition = (struct IntuitionIface*) IExec->GetInterface((Library*)IntuitionBase, "main", 1, NULL));
It's a bit more involved. Although I am sure there is some glue logic that allows the code you have written to work as-is. _________________ Doing stupid things for fun... |
|
Status: Offline |
|
|
Karlos
| |
Re: Noob C problem Posted on 3-Mar-2018 16:44:07
| | [ #5 ] |
|
|
|
Elite Member |
Joined: 24-Aug-2003 Posts: 4402
From: As-sassin-aaate! As-sassin-aaate! Ooh! We forgot the ammunition! | | |
|
| I tend to use the following template logic for C++ at least:
template<typename L, typename I> inline bool openMainLibInterface(L*& library, I*& interface, const char* name, uint32 version) { if ( (library = (L*)IExec->OpenLibrary(name, version)) ) { if ( (interface = (I*)IExec->GetInterface((Library*)library, "main", 1, NULL)) ) { return true; } else { std::fprintf(stderr, "Opened %s [%p] but failed to get main\n", name, library); } } else { std::fprintf(stderr, "Failed to open: %s\n", name); } return false; }
template<typename L, typename I> inline void closeMainLibInterface(L*& library, I*& interface) { if (interface) { IExec->DropInterface((Interface*)interface); interface = 0; } if (library) { IExec->CloseLibrary((Library*)library); library = 0; } }
So that I can open/close libraries and their main interfaces easily.
struct IntuitionBase* IntuitionBase = 0; struct IntuitionIFace* IIntuition = 0;
if (openMainLibInterface(IntuitionBase, IIntuition, "intuition.library")) { ... }
Also, I forgot how to post indentible in xoops lol. _________________ Doing stupid things for fun... |
|
Status: Offline |
|
|
bison
| |
Re: Noob C problem Posted on 3-Mar-2018 16:44:48
| | [ #6 ] |
|
|
|
Elite Member |
Joined: 18-Dec-2007 Posts: 2112
From: N-Space | | |
|
| @Robert
Actually, I think that is a linker error. If it were the compiler, it would say something like
Quote:
error: implicit declaration of function 'OpenLibrary' |
You probably need to compile with something like
Quote:
where 'foo' is the name of the library.
I know how to find library names on Linux, not not on AmigaOS 4.x -- perhaps someone else can add this information.
Update: or maybe not. Take a look at this:
http://wiki.amigaos.net/wiki/The_Hacking_Way:_Part_1_-_First_Steps#Myth_.231:_AmigaOS_behaves_like_UNIXLast edited by bison on 03-Mar-2018 at 04:55 PM.
_________________ "Unix is supposed to fix that." -- Jay Miner |
|
Status: Offline |
|
|
Robert
| |
Re: Noob C problem Posted on 3-Mar-2018 16:51:55
| | [ #7 ] |
|
|
|
Cult Member |
Joined: 10-Mar-2003 Posts: 879
From: Glasgow | | |
|
| @Karlos
Hey Karlos! Long time no' speak, etc. Hope you're well, sir.
Anyway, back to the issue at hand, that's helped at least as far as getting rid of the error. Thanks.
_________________ Robert -- A1XE G4, OS4.1. Peg1 G3, MOS 1.4. Abel Soul - Check out our tunes on Spotify |
|
Status: Offline |
|
|
NutsAboutAmiga
| |
Re: Noob C problem Posted on 3-Mar-2018 16:59:02
| | [ #8 ] |
|
|
|
Elite Member |
Joined: 9-Jun-2004 Posts: 12817
From: Norway | | |
|
| @Robert
Quote:
Trying to follow some simple tutorials, I can't get this to compile: |
Well the tutorial is wrong, maybe not so good idea to learn how to code from some one who is clueless. Quote:
struct Library *OpenLibrary(); |
I not sure what your trying to do here? It is 110% wrong.
you need includes on top file.
#include "proto/exec.h" #include "proto/dos.h" #include "proto/IIntuition.h"
should " should be greater or smaller but breaks.
you should compile with:
gcc source.c -o your_program_name -D__USE_INLINE__
"-D__USE_INLINE__" allows you drop "IEXEC->" from OpenLibrary, or else it is.
DOSBase = IExec->OpenLibrary("dos.library",50);
Next you need to Obtain a Interface before you call any function inside the library.. (this only AmigaOS4.1)
See the AmigaOS4.1 SDK autodocs for Exec.library It in the SDK:Documentation/Autodocs directory.
Last edited by NutsAboutAmiga on 03-Mar-2018 at 05:10 PM. Last edited by NutsAboutAmiga on 03-Mar-2018 at 05:08 PM. Last edited by NutsAboutAmiga on 03-Mar-2018 at 05:02 PM. Last edited by NutsAboutAmiga on 03-Mar-2018 at 04:59 PM.
_________________ http://lifeofliveforit.blogspot.no/ Facebook::LiveForIt Software for AmigaOS |
|
Status: Offline |
|
|
Robert
| |
Re: Noob C problem Posted on 3-Mar-2018 17:03:19
| | [ #9 ] |
|
|
|
Cult Member |
Joined: 10-Mar-2003 Posts: 879
From: Glasgow | | |
|
| @NutsAboutAmiga
Quote:
NutsAboutAmiga wrote:
Well the tutorial is wrong, maybe not so good idea to learn how code from some who is clueless. |
It's from here: http://amigadev.elowar.com/read/ADCD_2.1/Libraries_Manual_guide/node0578.html
If anyone knows a decent resource for OS4-specific beginner tutorials, that would be appreciated._________________ Robert -- A1XE G4, OS4.1. Peg1 G3, MOS 1.4. Abel Soul - Check out our tunes on Spotify |
|
Status: Offline |
|
|
NutsAboutAmiga
| |
Re: Noob C problem Posted on 3-Mar-2018 17:10:20
| | [ #10 ] |
|
|
|
Elite Member |
Joined: 9-Jun-2004 Posts: 12817
From: Norway | | |
|
| |
Status: Offline |
|
|
NutsAboutAmiga
| |
Re: Noob C problem Posted on 3-Mar-2018 17:25:28
| | [ #11 ] |
|
|
|
Elite Member |
Joined: 9-Jun-2004 Posts: 12817
From: Norway | | |
|
| @Robert
/* easy.c: a complete example of how to open an Amiga function library in * C. In this case the function library is Intuition. Once the Intuition * function library is open, any Intuition function can be called. This * example uses the DisplayBeep() function of Intuition to flash the * screen With SAS/C (Lattice), compile with lc -L easy.c */
/* Declare the return type of the functions we will use. */ struct Library *OpenLibrary(); /* These Exec library functions can be */ void CloseLibrary(); /* called anytime (Exec is always open). */
void DisplayBeep(); /* Before using this Intuition function, */ /* the Intuition library must be opened */
you should never declare library functions like that, should always use the includes, as this code is old, it need to be updated.
struct IntuitionBase *IntuitionBase; /* Get storage for the library base */ /* The base name MUST be */ /* IntuitionBase */
This one should work fine, same as on AmigaOS4.
int main() { IntuitionBase=(struct IntuitionBase *) OpenLibrary("intuition.library",33L);
if(IntuitionBase) /* Check to see if it actually opened. */ { /* The Intuition library is now open so */
and then you need to open Interface, as this was not needed before AmigaOS4.
DisplayBeep(0L); /* any of its functions may be used. */
DisplayBeep(0L) wont work on RTG screen, you wont see any color change on the screen.
CloseLibrary(IntuitionBase); /* Always close a library if not */ /* in use. */
you need to close the interface after obtaining a interface.
} else /* The library did not open so return an */ { /* error code. The exit() function is */ exit(20); /* not part of the OS, it is part of the */ } /* compiler link library. */ }
_________________ http://lifeofliveforit.blogspot.no/ Facebook::LiveForIt Software for AmigaOS |
|
Status: Offline |
|
|
Robert
| |
Re: Noob C problem Posted on 3-Mar-2018 17:39:47
| | [ #12 ] |
|
|
|
Cult Member |
Joined: 10-Mar-2003 Posts: 879
From: Glasgow | | |
|
| @NutsAboutAmiga
Quote:
Thanks very much, I'll check that out.Last edited by Robert on 03-Mar-2018 at 05:52 PM.
_________________ Robert -- A1XE G4, OS4.1. Peg1 G3, MOS 1.4. Abel Soul - Check out our tunes on Spotify |
|
Status: Offline |
|
|
Karlos
| |
Re: Noob C problem Posted on 3-Mar-2018 20:29:51
| | [ #13 ] |
|
|
|
Elite Member |
Joined: 24-Aug-2003 Posts: 4402
From: As-sassin-aaate! As-sassin-aaate! Ooh! We forgot the ammunition! | | |
|
| @Robert
Not bad mate. One or two minor changes since we last spoke I guess. Married, two children :D
Hope all is well with yourself. Last edited by Karlos on 03-Mar-2018 at 08:30 PM.
_________________ Doing stupid things for fun... |
|
Status: Offline |
|
|
Robert
| |
Re: Noob C problem Posted on 3-Mar-2018 21:23:02
| | [ #14 ] |
|
|
|
Cult Member |
Joined: 10-Mar-2003 Posts: 879
From: Glasgow | | |
|
| @Karlos
Quote:
Karlos wrote: @Robert
Not bad mate. One or two minor changes since we last spoke I guess. Married, two children :D |
Yes, minor changes indeed!
Belated congratulations on all counts.
Quote:
Hope all is well with yourself. |
As well as could be reasonably expected for an auld geek. Finally finding the odd half hour here and there to play with my Amigas again, which has been a pleasant distraction. Got the A1 and Peg1 up and running again. Unfortunately the A1200 remains switched off for now as I have been unable to locate the cable that connects it to the 1084s but it still looks nice sitting there. _________________ Robert -- A1XE G4, OS4.1. Peg1 G3, MOS 1.4. Abel Soul - Check out our tunes on Spotify |
|
Status: Offline |
|
|
Hypex
| |
Re: Noob C problem Posted on 4-Mar-2018 15:11:28
| | [ #15 ] |
|
|
|
Elite Member |
Joined: 6-May-2007 Posts: 11204
From: Greensborough, Australia | | |
|
| @Robert
I don't think that would work on an actual Amiga either. As stated it's missing includes. That's makes a bad example. |
|
Status: Offline |
|
|
broadblues
| |
Re: Noob C problem Posted on 4-Mar-2018 20:02:38
| | [ #16 ] |
|
|
|
Amiga Developer Team |
Joined: 20-Jul-2004 Posts: 4446
From: Portsmouth England | | |
|
| @Hypex
Quote:
don't think that would work on an actual Amiga either. As stated it's missing includes. That's makes a bad example.
|
It doesn't need any includes. All the functions have been declared, so it would build, but it would need linking against a libamiga to get the library call stubs.
It is ofcourse a pretty bad example. @Robert
Here is how you would do it now
#define __USE_INLINE__
#include <proto/exec.h> #include <proto/intuition.h>
struct Library *IntuitionBase; struct IntuitionIFace *IIntuition;
int main(int argc, char *argv[]) { if((IntuitionBase = OpenLibrary("intuition.library",0L))) { #if defined (__amigaos4__) if((IIntuition = (struct IntuitionIFace *)GetInterface(IntuitionBase, "main",1,0))) { #endif DisplayBeep(0L); #if defined (__amigaos4__) DropInterface((struct Interface *)IIntuition); } #endif CloseLibrary(IntuitionBase); } }
Save as test.c and
Link with
gcc -o test text.c -Wall -Werror
Run it and watch your screen bar flash (possibly hear a beep depending on your Sound Prefs).
_________________ BroadBlues On Blues BroadBlues On Amiga Walker Broad |
|
Status: Offline |
|
|
broadblues
| |
Re: Noob C problem Posted on 4-Mar-2018 20:09:09
| | [ #17 ] |
|
|
|
Amiga Developer Team |
Joined: 20-Jul-2004 Posts: 4446
From: Portsmouth England | | |
|
| @NutsAboutAmiga
Quote:
DisplayBeep(0L) wont work on RTG screen, you wont see any color change on the screen.
|
Except that it does. It's patched by Iprefs (I think) since quite some time, to play a sound and or flash the title bar.
_________________ BroadBlues On Blues BroadBlues On Amiga Walker Broad |
|
Status: Offline |
|
|
NutsAboutAmiga
| |
Re: Noob C problem Posted on 4-Mar-2018 21:00:46
| | [ #18 ] |
|
|
|
Elite Member |
Joined: 9-Jun-2004 Posts: 12817
From: Norway | | |
|
| |
Status: Offline |
|
|
Robert
| |
Re: Noob C problem Posted on 4-Mar-2018 22:55:02
| | [ #19 ] |
|
|
|
Cult Member |
Joined: 10-Mar-2003 Posts: 879
From: Glasgow | | |
|
| @broadblues (& all)
These replies are excellent, thanks. I'm already getting a *slightly* better understanding of things.
Cheers.
_________________ Robert -- A1XE G4, OS4.1. Peg1 G3, MOS 1.4. Abel Soul - Check out our tunes on Spotify |
|
Status: Offline |
|
|
broadblues
| |
Re: Noob C problem Posted on 5-Mar-2018 0:49:06
| | [ #20 ] |
|
|
|
Amiga Developer Team |
Joined: 20-Jul-2004 Posts: 4446
From: Portsmouth England | | |
|
| @NutsAboutAmiga
Quote:
I guess that beta or something you have?
I know nothing, not part of inner circle.
|
Hardly beta, it's been that way since 3.1 and I think even 2.04 but I don't have 2..04 to check on.
If you *don't* see flash or hear a beep, chack your sound prefs settings._________________ BroadBlues On Blues BroadBlues On Amiga Walker Broad |
|
Status: Offline |
|
|