![]() |
|
Creating a DLL with Allegro functions |
RemmyDZ
Member #15,971
May 2015
|
Hi there, I am using Allegro for quite a while now. 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: 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: 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, |
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. -- |
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. Anyways, I'll try out your suggestion and see if it works, thanks! EDIT: Allright, I tried using the mt-debug, but it still gave me the same errors and warning. 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, |
bamccaig
Member #7,536
July 2006
![]() |
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? -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
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")
-- |
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. As for the seperate ALlegro DLLs, I will try that out, thanks! EDIT: 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. |
bamccaig
Member #7,536
July 2006
![]() |
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. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
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? -- |
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. 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: EDIT #2: It seems like my problem is solved! 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: 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: 1#define ALLEGRO_NO_MAGIC_MAIN
And voila, it worked! Cheers! |
|