News:

The new Release 25.03 is out! You can download binaries for Windows and many major Linux distros here .

Main Menu

Trying to create a DLL from Enet source code

Started by slenkar, May 18, 2007, 03:09:08 PM

Previous topic - Next topic

slenkar

Hi Im trying to create a DLL from a library called Enet:
http://enet.bespin.org/

I got the source and compiled it into a static library,

then I created a new project:



// dll.h
#ifdef __cplusplus
#define EXPORT extern "C" __declspec (dllexport)
#else
#define EXPORT __declspec (dllexport)
#endif

EXPORT int CALLBACK mymagicfunction(void);



#include <windows.h>
#include "dll.h"

EXPORT int CALLBACK mymagicfunction(void)
{
       enet_initialize();
}


I told the IDE to use libenet.a as a linker. (the static library that I compiled)

the error message was:
ld.exe cannot find -lEnet

stahta01

Turn on Compiler Logging and post the part of "Build Log" that contains the "ld.exe cannot find -lEnet" error and the 3 or 4 command lines right before it.

Steps to turn on Compiler Logging:
"Settings" -> "compiler debugger"
Change "Compiler Settings" to "Other Settings"
Set "Compiler Logging" to "Full Command Line"
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

slenkar

thanks for helping:
Quote
Project   : DLL Sample
Compiler  : GNU GCC Compiler (called directly)
Directory : C:\download\enet-1.0\enet-1.0\
--------------------------------------------------------------------------------
Switching to target: default
mingw32-g++.exe -shared  -Wl,--out-implib=libEnet.a -Wl,--dll    -LC:\Dev-Cpp\lib .objs\main.o   -o Enet.dll      -lEnet
C:\Dev-Cpp\bin\..\lib\gcc\mingw32\3.4.2\..\..\..\..\mingw32\bin\ld.exe: cannot find -lEnet
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 1 seconds)
1 errors, 0 warnings

stahta01

#3
Where is Enet related to the project cbp file?

You need to add it to the search path in CB.

"Project" -> "Build Options"
Change to "Search Diretories"
Add the folder holding Enet library to linker tab.

Tim S
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

slenkar

that worked THANKS


but now it is saying 'undefined reference to enet_initialize()'

I know for a fact that function/command is in the Enet source code.

Is just including the static library enough?

stahta01

#5
Quote from: slenkar on May 18, 2007, 10:39:57 PM
that worked THANKS


but now it is saying 'undefined reference to enet_initialize()'

I know for a fact that function/command is in the Enet source code.

Is just including the static library enough?

This is a little to complex for me to answer, I am a newbie to c++ and have forgotten most of my C language knowledge.
But, I think including static library should be enough.

I am downloading 1.0 version of enet to see if I can see something.

Tim S
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

stahta01

Are you building the debug or the release version of the projects?

The release compiled for me, but the debug version did not.

Tim S
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

stahta01

The below code worked for me in release, but not in debug.


#include <windows.h>

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT
#endif

#include "enet/enet.h"
int DLL_EXPORT CALLBACK mymagicfunction(void);


int DLL_EXPORT CALLBACK mymagicfunction(void)
{
       enet_initialize();
}
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

stahta01

#8
As two files below. Works in release not in debug. A lot of the time debug needs windows libraries not on my system to work, it could work on yours.

Remember to define BUILD_DLL in the project building the DLL.

Tim S


// dll.h

#ifndef DLL_H_INCLUDED
#define DLL_H_INCLUDED

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT
#endif

#ifdef __cplusplus
extern "C" {
#endif
int DLL_EXPORT CALLBACK mymagicfunction(void);
#ifdef __cplusplus
}
#endif

#endif // DLL_H_INCLUDED




#include <windows.h>
#include "dll.h"
#include "enet/enet.h"

int DLL_EXPORT CALLBACK mymagicfunction(void)
{
       enet_initialize();
}

C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

slenkar

#9
thanks
how do you put it in release mode?

and which files do I add to the project?

stahta01

#10
Quote from: slenkar on May 19, 2007, 12:07:20 AM
thanks
how do you put it in release mode?

and which files do I add to the project?

On the compiler toolbar, change "Build Target" to "Release"

Note: I used the GUI to start the DLL Project.

File -> New -> Project -> "Dynamic Link Library"
It defaults to two targets "Debug" and "Release"
I edited the single file main.cpp like my sample above.

Note, if I leave out the include of "enet/enet.h" I get an error like yours. But, I needed to the other changes to get it to work.

Tim S
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

slenkar

I can only seem to get

default
or
all

as choices for the build target

stahta01

#12
Did you build the Enet static library as debug or as release?

Debug in minGW GCC normally means options "-g" and "-D_DEBUG" are used.

Release in minGW GCC normally means option "-DNDEBUG" is used; sometimes optimize options like "-O2" are only used with release.

Note, I just found what looks like bad setting on building the debug version of Enet static library. Will retry it again.

Did you build Enet static library using Code::Blocks?

Did you build Enet static library using minGW GCC?

Tim S
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

stahta01

Found out that the Release build was failing also, Code::Blocks just was not displaying the error in the message window.
Will post more if I get it to work.

Tim S
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]

stahta01

#14
I think I got it to work.

You need to add libraries

enet, wsock32, winmm, ws2_32

Uploaded project to http://www.savefile.com/files/733788
The rest of my CodeBlocks stuff is at http://www.savefile.com/projects/1039215


File layout top is the folder that contains enet.dsp; only some files shown, but it should be enough to confirm where to extract to.

/top/enet.dsp
/top/enet.cbp
/top/enet.workspace
/top/testdll/testdll.cbp

Tim S
C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]