GP32 Compiling Using A Lib


pea

developer
Joined
Oct 3, 2004
Messages
1,089
Age
46
Location
New Zealand
Website
www.projectitis.com
Hi all,

When I am compiling a lib, and it references functions from another lib (e.g. The ringbuffer or smc functions from Mr.Mirkos SDK lib), does it compile the functions that it accesses from Mr.Mirkos lib directly in to the code?

If so, how do I stop this?

e.g.
Lib A has a function:
Code:
int do_something(){
    return 5*4-3+2;
}

Lib B has a function:
Code:
do_anotherthing(){
    int a;
    a = do_something();
}

How do I get lib B to compile without actually compiling the code for lib A's function in to it? Just having a reference to the function.

This means that the person who uses lib B will also need lib A, but it also means that lib A can be updated, and lib B doesn't need to be recompiled, as long as the prototypes are the same.
 
How do you compile a library?

Well, see, you can't use an external function unless you #include it or whatever.
If you remove the includes and stuff, and just have the prototypes, the compiler will quit and say it can't find the implementation.

However, if you add "extern" or something to the prototype, the compiler will figure it out I think.

This is so libgpmikmod.lib (or whatever) doesn't include half of Mr.Mirko's SDK, right? :p

Edit: http://cplus.about.com/od/beginnerctutoria1/l/aa060402d.htm

"The keyword extern tells the compiler that the variable is defined in another file."

Try that. :)
 
This is so libgpmikmod.lib (or whatever) doesn't include half of Mr.Mirko's SDK, right?

Yeah, how did you guess :) :p

I want to make it so that you need Mr.Mirkos SDK (or functions that replace the ones found in the SDK) but so that the SDK can be updated in the future :)

I knew it had something to do with 'extern'. I'll get on to it...
 
Hey Pea,

The compiler will never include parts of code from other libraries, it will only ever reference them. It is upto the linker to bring all the parts together and generate the final file. That's why you can get linker errors saying things like certain function not being defined, because the compiler thinks they are defined and simply references them, but the linker can't find the code to include into the final file.

BTW: "extern" only tells the compiler the definition is defined in another file, and not to recreate it. Eg:

int a;

will create a variable called 'a', but

extern int a;

will tell the compiler that the variable "a" has already been defined in another file, and it should not attempt to create it again. If in fact it's not in another file, you'll get a linker error at the end saying "undefined symbol" or such like. It's certainly useful for referencing variables and functions from other libraries, but most people miss it out for function declarations as they don't "create" anything (they are just a prototype).

eg:
void playmod (void);
extern void playmod (void);

to a lot of compilers parses to the exact same thing (as there's no storage/etc to be allocated), whereas the above definition of "int a" and "extern int a" are completely different - the first actually allocates storage for the variable.
 
Thanks Squidge.

So that means that my 'libmikmod.a' contains only references to Mr.Mirkos functions, and not the code themselves? That's great news.

EDIT: I have indeed had those linker errors in the past. I guess its always good to be sure :) Thanks
 
Back
Top