Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Creating a DLL with Allegro functions

This thread is locked; no one can reply to it. rss feed Print
Creating a DLL with Allegro functions
RemmyDZ
Member #15,971
May 2015

Hi there,

I am using Allegro for quite a while now.
Recently I stumbled across something I wanted to try out for a long time, DLL files.
So, after trying out a little bit, I wanted to implement Allegro functions in a DLL file.
I know it probably sounds way too far fetched and inefficient, but I'm just doing this to learn a bit more.
So, my problem is as follows.
I created a DLL file (all the settings in my compiler, MSVC++, are set correctly, so no problems there).
I could compile that DLL file without any problems at first, but after including the header file "allegro_native_dialog.h", things went south.
I got the following error messages and warning:

Warning 1 warning LNK4086: entrypoint '_mainCRTStartup' is not __stdcall with 12 bytes of arguments; image may not run C:\Users\Remco\Documents\Visual Studio 2010\Projects\Project_SandBox_Engine\Debug\Project_SandBox_Engine.dll 1 1 Project_SandBox_Engine

Error 2 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup C:\Users\Remco\Documents\Visual Studio 2010\Projects\Project_SandBox_Engine\Project_SandBox_Engine\MSVCRTD.lib(crtexe.obj) Project_SandBox_Engine

Error 3 error LNK1120: 1 unresolved externals C:\Users\Remco\Documents\Visual Studio 2010\Projects\Project_SandBox_Engine\Debug\Project_SandBox_Engine.dll 1 1 Project_SandBox_Engine

Now, without including that specific header, everything works fine, except for the fact that I cannot use the desired function "al_show_native_message_box(...)", which I'd like to implement inside my DLL.

Here's my code for the DLL project:

Header file:

#SelectExpand
1#ifdef COMPILE_ENGINE 2 #define ENGINE_EXPORT __declspec(dllexport) 3#else 4 #define ENGINE_EXPORT __declspec(dllimport) 5#endif 6 7#include<string> 8 9class ENGINE_EXPORT Engine{ 10public: 11 Engine(); 12 void moveObject(float x, float y, float speed); 13 void turnObject(float objectAngle, float angleDelta); 14 void showErrorMessage(int errorNr, std::string message); 15};

CPP file:

#SelectExpand
1#include<iostream> 2#include<allegro5/allegro.h> 3#include<allegro5/allegro_native_dialog.h> 4#include"engine.h" 5 6#pragma comment(lib, "allegro-5.0.10-monolith-md-debug.lib") 7 8Engine::Engine(){ 9} 10 11void Engine::moveObject(float x, float y, float speed){ 12} 13 14void Engine::turnObject(float objectAngle, float angleDelta){ 15 objectAngle += angleDelta; 16} 17 18void Engine::showErrorMessage(int errorNr, std::string message){ 19 al_show_native_message_box(NULL, "ERROR", "ERROR", message.c_str(), MB_OK, NULL); 20}

The project is set as a Dynamic Library (.dll), and I have a global definition set for "COMPILE_ENGINE".

Once again, I know that what I'm trying to to is quite inefficient, but I'd still love to see this work, and I hope to achieve that with your help!

Greetings,
RemmyDZ :)

Elias
Member #358
May 2000

DLLs are very efficient, not sure why you think otherwise. Splitting code off into libraries is a good idea, it's what I do myself.

I don't use MSVC myself, but I think you need to make sure to link to the same MSVC runtime in all your libraries (not in general, but in this case). Maybe try with monolith-mt-debug instead of monolith-md-debug.

--
"Either help out or stop whining" - Evert

RemmyDZ
Member #15,971
May 2015

Thanks for your quick reply!

Well, the reason I thought it'd be inefficient is that Allegro already works with a DLL itself, the monolith-thingy.
So my initial thought was that using that DLL inside another DLL would be quite useless, nevertheless I wanted to try it out.
So it actually is efficient?
Is that because you can reuse the created DLL for other projects for instance?

Anyways, I'll try out your suggestion and see if it works, thanks! 8-)

EDIT:

Allright, I tried using the mt-debug, but it still gave me the same errors and warning.
After trying a few things, I noticed two things.
First, it compiles just fine with only the "allegro.h" header included, but it starts giving errors when including the "allegro_native_dialog.h" header.
Now, I also noticed that, when searching online for my error message, people who are experiencing this error all forgot to write a main function in their CPP file.
I know that a DLL file doesn't need a main function, and maybe it even doesn't function with one (no idea on that one), but, when addind the following code to my CPP file, it compiles just fine:

#SelectExpand
1int main(){ 2}

Will my DLL still work this way, or is there another solution without having to include a main function in my project?

Greetings,
RemmyDZ :)

bamccaig
Member #7,536
July 2006
avatar

That sounds to me like you haven't configured your toolchain properly. Is it actually producing a DLL file when you add a main function or is it just generating an EXE? :-/

Elias
Member #358
May 2000

Can you try using the separate allegro DLLs instead of monolith? Something like this (I don't really know the exact names):

#pragma comment(lib, "allegro-5.0.10-md-debug.lib")
#pragma comment(lib, "allegro-5.0.10-native_dialog-md-debug.lib")
#pragma comment(lib, "allegro-5.0.10-main-md-debug.lib")

--
"Either help out or stop whining" - Evert

RemmyDZ
Member #15,971
May 2015

Thanks for the replies!

It actually produced a .DLL file, but it's still very weird, having a main function and all.
What do you mean by my toolchain exactly?

As for the seperate ALlegro DLLs, I will try that out, thanks!

EDIT:
I found the LIB files, except for the main thing.
So now I have this:

#SelectExpand
1#pragma comment(lib, "allegro-5.0.10-md-debug.lib") 2#pragma comment(lib, "allegro_dialog-5.0.10-md-debug.lib")

Still cannot find the one with "main" in it, maybe it doesn't exist, I don't know.
Anyhow, without having a main function inside my code, it still won't compile and it gives me the same errors and warning as before.

bamccaig
Member #7,536
July 2006
avatar

By "toolchain" I meant the chain of tools used to build the program. Typically there's a C pre-processor, a compiler, and a linker. In Visual Studio, this is all hidden away from you, and instead you would configure them all using Visual Studio's project settings, and that would pass the appropriate options onto the command line tools.

There isn't going to be a main function in Allegro. There shouldn't be. There's only supposed to be one main function ([C++:] at least with any given signature) and that's typically going to be defined by the program author, not the library author. However, there's no reason that your DLL should require a main function...

It sounds like there's an "entry point" function that is supposed to be optional. This says that the run-time might provide one for you to use. If not, I don't know, maybe you should define an empty one? But I don't think it needs to be called main (and I'd assume it shouldn't since it serves a different purpose).

I don't know. Maybe look into that and see if it sheds any light on things.

Elias
Member #358
May 2000

Just to make sure, does your DLL (without that "main" function) work fine as long as you don't use al_show_native_message_box?

And if so, can you use any Allegro functions from the DLL? And is your plan to use Allegro both from the DLL and your main program, or only from the DLL?

--
"Either help out or stop whining" - Evert

RemmyDZ
Member #15,971
May 2015

Once again thanks for the replies!

I actually found out that I didn't do my research well enough.
It turns out that I get the same errors and warning when I compile with just the "allegro.h" header file included.
What I basically did was including that header file and not using any Allegro functions throughout the code.
So, just including any of those Allegro header files gives me those errors.
Including the "allegro_image.h" header file makes it even weirder:

#SelectExpand
11>------ Build started: Project: Project_SandBox_Engine, Configuration: Debug Win32 ------ 21>Build started 14-12-2015 17:07:10. 31>InitializeBuildStatus: 41> Touching "Debug\Project_SandBox_Engine.unsuccessfulbuild". 51>ClCompile: 61> main.cpp 71>c:\program files (x86)\microsoft visual studio 10.0\vc\include\allegro5\allegro_image.h(32): error C2061: syntax error : identifier 'al_init_image_addon' 81>c:\program files (x86)\microsoft visual studio 10.0\vc\include\allegro5\allegro_image.h(32): error C2059: syntax error : ')' 91>c:\program files (x86)\microsoft visual studio 10.0\vc\include\allegro5\allegro_image.h(32): error C2143: syntax error : missing ')' before ';' 101>c:\program files (x86)\microsoft visual studio 10.0\vc\include\allegro5\allegro_image.h(32): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 111>c:\program files (x86)\microsoft visual studio 10.0\vc\include\allegro5\allegro_image.h(33): error C2061: syntax error : identifier 'al_shutdown_image_addon' 121>c:\program files (x86)\microsoft visual studio 10.0\vc\include\allegro5\allegro_image.h(33): error C2059: syntax error : ')' 131>c:\program files (x86)\microsoft visual studio 10.0\vc\include\allegro5\allegro_image.h(33): error C2143: syntax error : missing ')' before ';' 141>c:\program files (x86)\microsoft visual studio 10.0\vc\include\allegro5\allegro_image.h(33): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 151>c:\program files (x86)\microsoft visual studio 10.0\vc\include\allegro5\allegro_image.h(33): error C2733: second C linkage of overloaded function 'AL_FUNC' not allowed 161> c:\program files (x86)\microsoft visual studio 10.0\vc\include\allegro5\allegro_image.h(33) : see declaration of 'AL_FUNC' 171>c:\program files (x86)\microsoft visual studio 10.0\vc\include\allegro5\allegro_image.h(34): error C2065: 'uint32_t' : undeclared identifier 181>c:\program files (x86)\microsoft visual studio 10.0\vc\include\allegro5\allegro_image.h(34): error C2065: 'al_get_allegro_image_version' : undeclared identifier 191>c:\program files (x86)\microsoft visual studio 10.0\vc\include\allegro5\allegro_image.h(34): error C2059: syntax error : ')' 201> 211>Build FAILED. 221> 231>Time Elapsed 00:00:00.44 24========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I tried the DllMain function as suggested, but to no avail.

Inside my MSVC options I tried compiling with both /NOENTRY and without, but as long as there are Allegro header files involved, I'm not getting anything compiled.

EDIT:
Nevermind, when having both "allegro.h" and "allegro_image.h" included, I don't get the errors stated above, but I still keep the 2 errors and 1 warning from earlier.

EDIT #2:

It seems like my problem is solved!
I don't want to jump on the hype train too soon, but my DLL compiles fine so far!
As happy as I am, I'll be going to bed right now, so I haven't tested if the DLL actually works, but here's what I did:

I kept searching on internet for my error, and once I also used the word "Allegro" in my search keys, I came across this post:

#SelectExpand
1 2I was merging the differences between new project that was working properly and our project for hours until I found out that the problem is caused by our misconfiguration of the allegro preprocesor definitions. 3 4Deep down in the allegro library in win/alconfig.h, there are these lines 5 6#ifndef ALLEGRO_NO_MAGIC_MAIN 7 #if defined _MSC_VER && !defined ALLEGRO_LIB_BUILD 8 #pragma comment(linker,"/ENTRY:mainCRTStartup") 9 #endif 10#endif 11We did setup this macro for the allegro library compilation, but the allegro file was also included from the main project, that didn't specify this one. Defining the macro in the main project fixed the problem (obviously). 12 13I didn't really see this comming!

So, I added the following code to my CPP file:

#SelectExpand
1#define ALLEGRO_NO_MAGIC_MAIN

And voila, it worked!
I'll post the results tomorrow, as I'm about to sleep right now.

Cheers!
RemmyDZ :)

Go to: