Pelles C forum

C language => Beginner questions => Topic started by: opaque on April 02, 2020, 10:43:40 AM

Title: Build a dll without __declspec(dllexport)?
Post by: opaque on April 02, 2020, 10:43:40 AM
I forgot to add __declspec(dllexport) and write the code for the dll just like normal program. Does Pelles C support build a dll and automatically exported all functions and types from it? MinGW has this option.

http://www.mingw.org/wiki/sampledll

Quote
Building and using a DLL without the dllexport/dllimport attibutes

If you pass the -no-undefined and --enable-runtime-pseudo-reloc options to the linker, you don't have to add dllimport or dllexport attributes to the source code that the DLL is made with ; all functions are imported/exported automatically by default, just like in unices.

I have successfully build a dll from my code with MinGW and program linked to it worked properly.
Title: Re: Build a dll without __declspec(dllexport)?
Post by: opaque on April 02, 2020, 10:45:42 AM
When I posted this thread it's error with "Call to undefined method Akismet::setAuthor()". Fortunately, the thread was posted successfully.
Title: Re: Build a dll without __declspec(dllexport)?
Post by: frankie on April 02, 2020, 12:02:13 PM
PellesC doesn't have the GCC option.
If you don't specify any exported symbol the export table in the PE file will be empty.
If you don't want modify all your DLL code you can build a DEF file with functions you want to export and relink the DLL objects adding the DEF file.
For better instructions please refer to the help for polink option /DEF:filename
Title: Re: Build a dll without __declspec(dllexport)?
Post by: opaque on April 02, 2020, 05:33:23 PM
PellesC doesn't have the GCC option.
If you don't specify any exported symbol the export table in the PE file will be empty.
If you don't want modify all your DLL code you can build a DEF file with functions you want to export and relink the DLL objects adding the DEF file.
For better instructions please refer to the help for polink option /DEF:filename
If my code doesn't specify it does Pelles C use CDECL or STDCALL by default? I have to know this to prepare the .def file.
Title: Re: Build a dll without __declspec(dllexport)?
Post by: frankie on April 02, 2020, 10:26:13 PM
If my code doesn't specify it does Pelles C use CDECL or STDCALL by default? I have to know this to prepare the .def file.
This is not related to the export qualifier __declspec(dllexport). This extended qualifier simply puts the address of your function in the export table of the PE executable independently from the calling convention.
You can specify the calling convention for 32bit executables on function by function basis, using the qualifiers __stdcall or __cdecl, or globally defining the default calling convention. For 64bits executables the calling convention is fixed and is the MS 64bits ABI.
The default calling convention can be set on the compiler command line with the switches /Gr and /Gz, or setting it in the IDE project options, compiler tab.
You must specify in an header file the prototype of your exported functions, eventually adding the calling convention, __stdcall or __cdecl, and include it in the modules of the executable that loads the DLL.
Title: Re: Build a dll without __declspec(dllexport)?
Post by: opaque on April 04, 2020, 09:27:41 AM
If my code doesn't specify it does Pelles C use CDECL or STDCALL by default? I have to know this to prepare the .def file.
This is not related to the export qualifier __declspec(dllexport). This extended qualifier simply puts the address of your function in the export table of the PE executable independently from the calling convention.
You can specify the calling convention for 32bit executables on function by function basis, using the qualifiers __stdcall or __cdecl, or globally defining the default calling convention. For 64bits executables the calling convention is fixed and is the MS 64bits ABI.
The default calling convention can be set on the compiler command line with the switches /Gr and /Gz, or setting it in the IDE project options, compiler tab.
You must specify in an header file the prototype of your exported functions, eventually adding the calling convention, __stdcall or __cdecl, and include it in the modules of the executable that loads the DLL.

Because I don't write the .def file myself. I got it generated from MinGW when compiling the dll with MinGW. The generated .def file is decorated with @ so I think it's STDCALL.

Yes, it's a 64 bit DLL. But I also want to build for 32 bit.

IMHO, being able to build a dll without dllexport/dllimport is very useful. I think Pelles C should support it.
Title: Re: Build a dll without __declspec(dllexport)?
Post by: frankie on April 04, 2020, 04:40:22 PM
Because I don't write the .def file myself. I got it generated from MinGW when compiling the dll with MinGW. The generated .def file is decorated with @ so I think it's STDCALL.
Yes, it's a 64 bit DLL. But I also want to build for 32 bit.
IMHO, being able to build a dll without dllexport/dllimport is very useful. I think Pelles C should support it.
I see a lot of confusion here, and honestly don't understand your point.
Lets consider each options.
Code: [Select]
#ifdef _WIN32    //If compiling in MS environment define export specifier
#define EXPORT __cdecl(dllexport)
#else
#define EXPORT    //For all other OS ignore it
#endif

int EXPORT foo(void)
{
    ...
}

In the end if you like more the gcc behavior use it, it is a gcc compiler private extension, almost never available on compilers specific for MS OS.
I hope to have made it more clear now.