Ho to create one single dll file for entire project in 'C'

I have a project with multiple .c files and one resource.h.
Now my main.c looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
///** INDEX_CENTER **///
//-------------------//

// ** HEADERS ** //
#include "resource.h"

// ** MAIN FUNCTION  ** //
int main()
{
    fullScreen();
    hideCursor();
    menu();
}


The first 2 function are located in the same auxiliar.c file and that's ok I can create a .dll to load them in my main , but the problems comes with the 3rd function which is located in another file called menus.c
Now this menus.c has other functions too located in other files like :
ReadDeleat.c and and they are all connected to each other as I have other .c files that use these functions.
My only connection between all of these .c files is the resource.h which includes the headers the functions prototype other definitions and structs as well.
My question is How to create one single Dll for the main.c ?

this is my header file for the main.c:

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#define IDI_MAIN 1 // --> The Icon for the executable.

// ** Headers ** //

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

// ** MACROS ** -----------------------------------//

#define ARRAYSIZE(sel) (sizeof(sel) / sizeof(sel[0]))
#define SELECT_END  12
#define SELECT_ENDS 6
#define SELECT_ENDI 4
#define SELECT_ENDD 2
#define KEY_UP      72
#define KEY_DOWN    80
#define KEY_LEFT    75
#define KEY_RIGHT   77
#define KEY_ENTER   13
#define KEY_ESCAPE  27
#define MAX         1000
//-------------------------------------------------//

// ** STRUCTURI ** //

// INFO
struct INFO
{
    char apa[MAX];
    char gaz[MAX];
    char curent[MAX];
    long int diferenta;
    float mcApa;
    float mcGaz;
    float kwatt;
    float plata;
};
// TIMP
struct TIMP
{
    char zi[MAX];
    char luna[MAX];
    char an[MAX];
};
//------------------//

// Functii Prototip //
//-------------------------------------------------//

void AdaugaInfo();
void selectorAdaugaInfo(unsigned int select);
void adaugaInfoApa(struct INFO* info, struct TIMP* timp);
void adaugaInfoGaz(struct INFO* info, struct TIMP* timp);
void adaugaInfoCurent(struct INFO* info, struct TIMP* timp);
void adaugaPretMcApa();
void adaugaPretMcGaz();
void adaugaPretKw();

void ModificaInfo();
void selectorModificaInfo(unsigned int select);
void modificaInfoApa(struct INFO* info, struct TIMP* timp);
void modificaInfoGaz(struct INFO* info, struct TIMP* timp);
void modificaInfoCurent(struct INFO* info, struct TIMP* timp);
void modificaPretMcApa(struct INFO* info);
void modificaPretMcGaz(struct INFO* info);
void modificaPretKw(struct INFO* info);

void StergeInfo();
void selectorStergeInfo(unsigned int select);
void stergeInfoApa();
void stergeInfoGaz();
void stergeInfoCurent();

//-------------------------------------------------//
void menu();
void selector(unsigned  int select);

//-------------------------------------------------//
void Informatii();
void selectorInformatii(unsigned int select);
void Facturi();
void selectorFacturi(unsigned int select);
void Despre();
void selectorDespre(unsigned int select);

//-------------------------------------------------//
void InfoApa();
void InfoGaz();
void InfoCurent();
//-------------------------------------------------//

//-------------------------------------------------//
void DespreAutor();
void DespreProgram();
//-------------------------------------------------//

// Functii auxiliare //

void fullScreen();
void hideCursor();
//-------------------------------------------------//

// Functie de afisare si stergere //

void printInfo(FILE *file);
void stergeIndex(FILE *file1, FILE *file2, const int line);
int diferentaIndex(const char* index1, const char* index2);
int inputOnlyNumbers(float *tmp);
int modifyOnlyNumbers(float *tmp);
//-------------------------------------------------// 
Last edited on
Now this menus.c has other functions too located in other files
Why not add them all to the DLL?
https://translate.google.com/translate?sl=en&tl=ro&u=https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fcpp%2Fbuild%2Fwalkthrough-creating-and-using-a-dynamic-link-library-cpp%3Fview%3Dmsvc-160
Looks like an easy method is to create a new project as in the link, then add all the files that you want to combine into that new project. Just make sure you don't have a main function in those files, that should be left to the user to call. I'm not sure if it will compile a dll with main() inside but I don't think it would.
(Make sure to read the whole page, it also goes over how to link your new .dll file to a project.)

If not using visual studio, then the google search to use for code::blocks is "how to build a dll library code::blocks", replace the IDE name with whatever you use.
Last edited on
To add onto what newbieg said about C::B; it has a DLL template. I highly suggest sticking to that for your first few stubs.
Last edited on
Yes guys I'm using Code::Blocks... and I already read about it in Microsoft. Now a dynamic library shouldn't include a function definition? I mean if I include all my .c files in that 1 .dll file and call it with the main function, which request only 3 functions from that dll it will work ? I mean the main.c has no knowledge about what happens in other .c files because is only calling menu() the 3rd function, which has knowledge about what is going on with the rest of the functions.
Okay I will create from scratch a new project dll. and try add them carefully and see if its working.. I shouldn't write this program like some programmers call it (bad design)... cause now I have this code in my face and I had to think first if I wana design it to work properly with a dll file, and all I see is a mess of functions calling other functions, which is calling other functions and so on...
Last edited on
I can use multiple dll as well in my application to complicate less with other functions, since I already have that in my main() function I did a separate dll like this for the first 2 functions:

main.c for the index_c.dll

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
29
/// Functii auxiliare ///
///-------------------///

// ** HEADERS ** ----//
#include "index_c.h"

//------------------------------------------------//
// ** FUNCTII AUXILIARE ** //

//- MOD ECRAN INTREG -//

void fullScreen()
{
    keybd_event(VK_MENU, 0x38, 0, 0);
    keybd_event(VK_RETURN, 0x1c, 0, 0);
    keybd_event(VK_RETURN, 0x1c, KEYEVENTF_KEYUP, 0);
    keybd_event(VK_MENU, 0x38, KEYEVENTF_KEYUP, 0);
}

//- MOD ASCUNDE CURSOR -//

void hideCursor()
{
    HANDLE cursorHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO info;
    info.dwSize = 100;
    info.bVisible = FALSE;
    SetConsoleCursorInfo(cursorHandle, &info);
}


and the header index_c.h

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
//Header for the main.cpp index_c.dll
#ifndef __INDEX_C_H__
#define __INDEX_C_H__

#include <windows.h>

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


#ifdef __cplusplus
extern "C"
{
#endif

void fullScreen();
void hideCursor();

#ifdef __cplusplus
}
#endif

#endif // __INDEX_C_H__ 


Now as I create the index_c.dll how do I link it in my program for the first 2 functions ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
///** INDEX_CENTER **///
//-------------------//

// ** HEADERS ** //
#include "resource.h"

// ** MAIN FUNCTION  ** //
int main()
{
    fullScreen();     //  instead to call function from other .c file
    hideCursor();    //  like this ?
    menu();
}
Last edited on
Topic archived. No new replies allowed.