Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ extern usage between dll and application

Tags:

c++

extern

dll

Please forgive me if this is a stupid question but I am following a book and it isn't working. I have an game engine, which lives in a dll. It has a couple of external functions to be used in the actual game application:

extern void game_preload();

This is all perfect and dandy but when I go to build the all I get an error:

Undefined reference to `game_preload()'

Why is this? how can I get it to build out the dll so I can implement those functions in the application?

The book I am following is called "Advanced 2D game development" and it doesn't mention there being an issue.

EDIT: I read the book wrong and I was supposed to make a static library rather than a dynamic one. But may I ask is there anything similar to the "extern" usage for dynamic libraries?

like image 829
DavidColson Avatar asked Nov 25 '22 20:11

DavidColson


1 Answers

You'll have to link the static or dynamic library (for MSVC this is a file with the "lib" extension, for GCC this is either "a", "o" or "dll" depending on your system, etc.). The extern keyword is meant to be used to tell the compiler some variable can be found in another compilation unit (like a forward declaration, but for variables). Function prototypes are always externanyway, so it can be omitted there.

There's also the special case of using extern "C" to explicitly tell the compiler to handle some code as C rather than C++ (e.g. to link to C style functions inside C++ code).

Edit: Creating and using a static library is even easier to do. You can read up on this including some examples here. However I think it should be explained in the book at some part, unless you're supposed to not use any library so far (as mentioned in one of the comments).

like image 118
Mario Avatar answered Jan 10 '23 15:01

Mario