dissecting a cpp/dll file

Hi guys,

just messing around with creating dll's I am doing it with codeblocks which is unusual but still valid,

first lets start off with the header file,the pre-processor directives seem to be giving me trouble

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 #ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>

/*  To use this exported function of dll, include this header
 *  in your project.
 */

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


#ifdef __cplusplus
extern "C"
{
#endif

void DLL_EXPORT SomeFunction(const LPCSTR sometext);

#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__ 


#ifndef __MAIN_H__
#define __MAIN_H__ - these are pretty simple these are just header guards

#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif - I don't know at all what's going on here,if defined BUILD_DLL?

#ifdef __cplusplus
extern "C"
{
#endif

void DLL_EXPORT SomeFunction(const LPCSTR sometext);

#ifdef __cplusplus
}
#endif - also here I have no clue what is going on

thanks

#define __MAIN_H__ - these are pretty simple these are just header guards


Yes simple, but you're using a reserved identifier. Anything that starts with two underscores, or one underscore followed by an upper case letter is reserved for the implementation in any scope, and using a single leading underscore is also reserved for the implementation, if used within the global scope. Also remember that you must use distinct names in every file and MAIN_H may not be the best "name" for these header guards since it is a fairly common "name".

#endif - I don't know at all what's going on here,if defined BUILD_DLL?

Every #if must have a corresponding #endif.

I am doing it with codeblocks which is unusual


Why would you think using codeblocks to create a .dll or a static lib unusual?

thanks jlb



Anything that starts with two underscores, or one underscore followed by an upper case letter is reserved for the implementation in any scope, and using a single leading underscore is also reserved for the implementation, if used within the global scope. Also remember that you must use distinct names in every file and MAIN_H may not be the best "name" for these header guards since it is a fairly common "name".



what is meant by reserved for the implementation? (in any scope, and global scope)


I have always noticed that most people create a static or dynamic library using visual studio


thanks
I have always noticed that most people create a static or dynamic library using visual studio

What about people that don't use visual studio, or gasp Windows?

what is meant by reserved for the implementation?

Exactly what it says it is reserved for the implementation, meaning you can't use that naming convention unless you are writing the compiler.

(in any scope, and global scope)

I assume you know how scope works, but as a refresher "any scope" means any where "global scope" means outside any functions, classes, structures or any other limited scope.

Visual Studio means you're using Microsoft's build system/compiler.
Code Blocks (by default, can be re-configured) means that you're using MinGW (GNU)'s build system/compiler. Which you want to use is just down to taste and requirements of your project -- preferably, a robust solution would be able to build on multiple systems and configurations, but this is not always reality.

what is meant by reserved for the implementation
Basically, it means that that identifier (macro, class name, variable name, etc.) could already be defined or in use by internal procedures. For example, "__cplusplus" is a macro. Defining your own macro called __cplusplus could cause problems with libraries and header files.

Example of use of __cplusplus (in C++ header file cstdint):
1
2
3
#if __cplusplus < 201103L
# include <bits/c++0x_warning.h>
#else 


https://stackoverflow.com/questions/37325293/meaning-of-reserved-for-the-implementation

thanks guys that makes sense,

so where are the DLL_EXPORT __declspec(dllexport)

1
2
3
4
5
6
7

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


where is BUILD_DLL defined?? is it defined within the compiler?

also as you mentioned we can use __cplusplus to include certain headers,but where is __cplusplus actually defined?

thanks guys
also

1
2
3
4
ifdef __cplusplus
extern "C"
{
#endif 


also why do we need the extern "C" in this particular case? I know extern "C" is used to stop function name mangling but why would we need it in this case?

thanks

also why do we need the extern "C" in this particular case? I know extern "C" is used to stop function name mangling but why would we need it in this case?


Do you know that every compiler can mangle names differently and that even the same compiler can mangle name differently depending on the version of the compiler?

Also that macro tells the compiler to use C calling semantics to allow C functions to call this function.
thanks jlb,

probably a stupid question,but is the extern "C" essential? could I still make a DLL without it?
Why would you want to? It would only, at best, work with programs written with your particular compiler with everything compiled with the exact settings. A dll is meant to house code that can be shared by many different programs that were made by many different compilers. If, you don't care about sharing you might as well statically link instead of dynamic linking.

extremely good point,

but it does come at one small cost right? that cost that function overloading will not be possible ?
Yes. Also, you cannot use non-C features at the interface, such as C++ classes, references, templates, etc.
thanks guys =)
Topic archived. No new replies allowed.