Procedure for including libraries in a C program
helixnebula77 (13894) 11 posts |
Hi, I’ve made two threads recently and both of them have kind of centered around this same issue. I am using the official DDE compiler and the shared Makefile system. There are multiple libraries on the system, and more can be installed from !PackMan, but I have not yet figured out how to derive from these libraries what needs to be included in my Makefile to make the compiler see these libraries and allow me to #include them in my code. For example, I have the following in my Makefile right now: CINCLUDES = -IC:,OSLib:,SFLib:,TCPIPLibs: SFLib and OSLib both need me to run a !SetVars obey to make the compiler recognize them. TCIPLibs is included on the system but I never would have known what to put here unless someone on this forum told me (which is exactly what happened) and OSLib and SFLib were described in Steve Fryatt’s tutorial and I was also told what to put there. Now I want to use the SocketWatch library (from !PackMan), and I need to add something to my Makefile for that to work because just running #include “sockwatch.h” doesn’t work. I don’t want to pop into the forum every time I want to use a new library and ask someone what to include, I would like to know how I can find this information myself. How can I do that? |
Steve Fryatt (216) 2105 posts |
Please can you point to exactly what you have installed? The SocketWatch that I know is a module, not a library; there may be some C veneers for it, but they don’t seem to be in the module downloads that I can find, so knowing what you’re looking at would be a help! |
David Pitt (9872) 363 posts |
SocketWatch 0.06, on the autobuilder, builds from a single ObjAsm file. There is no library nor header file. The source was revealed here on the autobuilder with the |
helixnebula77 (13894) 11 posts |
The package available as SocketWatch on !PackMan, and here According to PackMan, it installed here on my system:
I took another look at the descriptions and you’re right, !PackMan and riscos.info do say module instead of library. To be honest I am not sure what the difference is. The descriptions of the package imply that it is supposed to be used by other programs, which is what I assume a library is. I would like to know the difference, and I would like to know how to use it. SocketWatch came up in my last thread, where Alan Williams mentioned it a couple times saying that I could use it (and also that it was easy to use). What I’m wondering is, how can I use it? And if I were to run into another module or library somewhere that looked helpful (eg DeskLib), how could I learn how to find this information without asking for help every time? |
Steve Fryatt (216) 2105 posts |
A library is a collection of code for linking to other code, using the compiler. You’ll usually get a collection of header files and an object file, and when you compile your project the compiler will include the relevant bits of the library’s object file into your executable and fix up all of the function calls so that they get performed using a branch or jump in the “normal” way. A module is a self-contained program that has been built to run on RISC OS. They’re a very RISC OS-y thing, and can either be self-contained applications (eg. StrongED and Zap are both application modules) or background tasks or collections of routines that provide services for others (eg. the Wimp, which drives the whole RISC OS Desktop, or SocketWatch). They often make functionality available to other applications via SWI calls, which is what SocketWatch is doing. What they’re not is a shared library (except that the Shared C Library is, but we won’t go there for now). To use a library, you need to give the compiler two pieces of information: where the header files are stored, and where the .o (or .a in Linux parlance) file is stored. There’s no fixed way of doing this that I’m aware of (as I note in my tutorial), but there’s a standard-ish approach which is to provide an Obey file which can be run to set some system variables up. For example, OSLib has SetVars, which resides in the root of the library folder structure and reads
If you look in the OSLib folder, you’ll see that it contains
So when SetVars sets So when you add
to your Makefile, you’re telling the compiler to search within the OSLib folder for header files. You then write something like
in your source file, and the compiler starts in OSLib, looks in the oslib folder because the In a similar vein, when you add
to your makefile, you’re telling the compiler to link against the file inside the OSLib folder, then down into o, and finally use the OSLib32 file. When you find a new library, you need to look at the documentation (if there is any) to see if it suggests how to reference it to the compiler. If that fails, look for Obey files that might help. If that fails, I’d suggest writing your own Obey file to set up a Path Variable. I use a tool called LibFolder, but I’ve never got around to releasing it as a “thing”. Using modules is different. Once a module is in memory, you can usually access the SWIs that it contains from your code. How you do this depends on the module:
To get the module into memory, you can start by double-clicking on the module before you test your code – but that quickly becomes tedious. The correct way is to If PackMan installs SocketWatch somewhere like !System for you, then you RMEnsure it from there. Otherwise, you’ll probably need to include a copy inside your application directory. I’ve never used SocketWatch, so I’ll let someone else advise on that one. |
Steve Pampling (1551) 8170 posts |
The first thing to do on the system is to use the CLI help. CTRL-F12 with open a task window with a CLI “h. Filer” will list the commands provided by that module and h. socketwatch similarly. |
Steve Fryatt (216) 2105 posts |
Although these will give you the * commands, while for use in a C program you’ll ideally want the SWIs. For those, read the documentation (which there hopefully is). |
helixnebula77 (13894) 11 posts |
Thanks for the detailed response!! This is making a lot more sense to me now. I am also happy to learn there is a help function for the command line. |