Trouble with a class in a DLL

Hey.


I have a problem. I search the web for class in DLLs.
I found some pages and try it.

here is the code for my class.

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
#include <windows.h>

#include "InDLL.h"

#ifdef __cplusplus    // If used by C++ code,
extern "C" {          // we need to export the C interface
#endif


// a sample exported function
void CLASSINDLL_CLASS_DECL __cdecl msgbox(const LPCSTR sometext)
{
    MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
}

#include <stdio.h>
#include <string>

using namespace std;

// Ein File Objekt wird erzeugt das zum schreiben des Log-Files verwendet wird.
FILE * pLogFile;

class CLASSINDLL_CLASS_DECL log_creator
{
public:

    log_creator()
    {
        // Classen-Funktion zum öffnen des Log-Files wird aufgerufen
        open_log();
    }

    ~log_creator()
    {
        // Classen-Funktion zum Schließen des Log-Files wird aufgerufen.
        close_log();
    }

    void log(char* ctext)
    {
        /*
        Ein einfacher Text wird hier mit Datum und Uhrzeit in den Log geschrieben.

        Als übergabe wert kann nur ein einfacher char Variablen Text verwendet werden.
        Hier kann der Text einfach direkt eingegeben werden.
        */
        char buffer [80];
        time_t rawtime;
        time ( &rawtime );
        struct tm * timeinfo;

        timeinfo = localtime ( &rawtime );
        strftime (buffer,80,"%c",timeinfo);
        fprintf (pLogFile, "%s\t %s\n",buffer,ctext);
    }

    void log(string &ctext)
    {
        /*
        Ein Text wird mit Datum und Uhrzeit in den Log geschrieben.

        Hier wird eine String Variable verwendet die Vorher mit verschiedenen Programm
        Variablen gefülllt worden ist. Die Variable muss zuvor im Programm erzeugt und
        gefüllt worden sein, und kann nur als Variable Übergeben werden.
        */
        char buffer[80]="";
        char buffer2[1024]="";
        int length;
        time_t rawtime;
        time ( &rawtime );
        struct tm * timeinfo;

        timeinfo = localtime ( &rawtime );
        strftime (buffer,80,"%c",timeinfo);
        length=ctext.copy(buffer2,ctext.size());
        buffer2[length]='\0';
        fprintf (pLogFile, "%s\t %s\n",buffer,buffer2);
    }

protected:

    void open_log(char* cfile="log.txt")
    {
        /*
        Log-File wird geöffnet.
        Wird keine spezielle Datei angegeben wird automatisch "log.txt" verwendet.
        Anschließend wir eine zeile in den Log geschrieben der den Anfang des aktuellen
        Log markiert.
        */
        pLogFile = fopen (cfile,"a");
        fprintf (pLogFile,"--- Neuer Log!!! ---\n\n");
    }

    void close_log(void)
    {
        /*
        Log-File wird geschlossen.
        Vorher wird noch eine Abschlußzeile in das Log geschrieben, die das Ende des
        aktuellen Log markiert, und abstandszeilen für den nächsten Log geschaffen.
        */
        fprintf (pLogFile,"\n--- Log Ende!!! ---\n\n\n");
        fclose (pLogFile);
    }

};

#ifdef __cplusplus
}
#endif 


InDLL.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
27
28
29
30
31
32
33
// InDLL.h: interface for the CInDLL class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_INDLL_H__179A79AA_4C1A_11D2_B67C_006008AC62A9__INCLUDED_)
#define AFX_INDLL_H__179A79AA_4C1A_11D2_B67C_006008AC62A9__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

#include "ClassInDLL.h"


#include <string.h>

class CLASSINDLL_CLASS_DECL log_creator
{
public:
	log_creator();
	virtual ~log_creator();

	void log(char* ctext);
	void log(string &ctext);

protected:

    void open_log(char* cfile="log.txt");
    void close_log(void);

};


#endif // !defined(AFX_INDLL_H__179A79AA_4C1A_11D2_B67C_006008AC62A9__INCLUDED_) 


ClassInDLL.h
1
2
3
4
5
6
7
8
9
10
11
12

#ifndef ClassInDLL_H
#define ClassInDLL_H

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

#endif // ClassInDLL_H


Error:
InDLL.h line 16 syntax error befor "log_creator"

What is my problem??? I made it like it was in the web!

Sorry for my english. German :-)

I use the MinGW with Code::Blocks.
closed account (z05DSL3A)
have you got BUILD_DLL defined your DLL project settings under C/C++ Preprocessor?
Last edited on
Yes it is defined.
i try it to only define it without the if-think. no diferent.
closed account (z05DSL3A)
Try putting the contenst of ClassInDLL.h into InDLL.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
27
28
29
30
31
32
33
34
35
36
37
38
// InDLL.h: interface for the CInDLL class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_INDLL_H__179A79AA_4C1A_11D2_B67C_006008AC62A9__INCLUDED_)
#define AFX_INDLL_H__179A79AA_4C1A_11D2_B67C_006008AC62A9__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

#ifdef DLLTEST_EXPORTS
	#define CLASSINDLL_CLASS_DECL __declspec(dllexport)
#else
	#define CLASSINDLL_CLASS_DECL __declspec(dllimport)
#endif



#include <string.h>

class CLASSINDLL_CLASS_DECL log_creator
{
public:
	log_creator();
	virtual ~log_creator();

	void log(char* ctext);
	void log(string &ctext);

protected:

    void open_log(char* cfile="log.txt");
    void close_log(void);

};


#endif // !defined(AFX_INDLL_H__179A79AA_4C1A_11D2_B67C_006008AC62A9__INCLUDED_) 


No diferent.

line 21 is the error: syntax error befor "log_creator"

Line 21: class CLASSINDLL_CLASS_DECL log_creator

But why???
closed account (z05DSL3A)
Sorry, just noticed that line 11 is

#ifdef DLLTEST_EXPORTS

instead of your

#ifdef BUILD_DLL

Have you got any code to test the dll (that includes InDLL.h) in the same project?
i check my code it is ok.

Like u see there is a test function.

when i comment out the class thinks and only the function is in the code, then it works and i can use the dll in other programms.

It is only the d§$% class think!
Last edited on
closed account (z05DSL3A)
I think I have it, forgive me for not noticing earlier!

your class declaration goes in the header file (InDLL.h) in the main.c file gose the class definitions.

1
2
3
4
5
6
7
8
9
log_creator::log_creator()
{
...
}

void log_creator::log(char* ctext)
{
...
}
I try it in different ways:
1
2
3
4
5
6
7
8
9
10
11
12
log_creator::log_creator() | syntax error befor ":"

class log_creator::log_creator | syntax error befor "log_creator"

class EXPORT log_creator::log_creator | syntax error befor "log_creator"

class EXPORT log_creator | syntax error befor "log_creator"

class log_creator | syntax error befor "log_creator"

log_creator | syntax error befor "{"
{
closed account (z05DSL3A)
Do you know how to split the clase definition from the declaration?

test.h (declaration)
1
2
3
4
5
6
7
class  test 
{
public:
    test(void);
	
    int testFunc(void);
};


test.cpp (definitions)
1
2
3
4
5
6
7
8
9
10
#include "test.h"
test::test()
{
    return;
}

int test::testFunc(void)
{
    return 0;
}


Last edited on
test.h produce this error:
Line 1
syntax error befor "test"
I delete the whole file an make a clean one.

When i make functions everythink is fine.

But when i try to declare a class i get this ... syntax error think.

In an Exe it is fine, but not in a DLL. Why???

Is my Compiler the problem?

I use the MinGW with Code::Blocks.

Is there somethink special i have to know???
Last edited on
closed account (z05DSL3A)
Can you just try this?

Create a new project (console application)

copy the following code into your main file (delete any other contents)
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
#include<iostream>
using namespace std;

class  test 
{
public:
    test(void);
	
    int testFunc(void);
};

test::test()
{
    return;
}

int test::testFunc(void)
{
    return 0;
}


int main()
{
	test t;
	t.testFunc();
	return 0;
}


compile it and tell us what errors you get.
No errors!

All went well.

It was a console executable and like i say there is no problems with classes.

Only in DLLs i get these errors. :-(
closed account (z05DSL3A)
I was thinking it may have been a 'compiler error' of some sort when you said (a couple of posts back) that test.h produced a syntax error before "test".


The only thing I can say is you will need to separate your class declaration from your definitions.
The problem was very easy!

I can't believe it.

My codefile was named "main.c".
To solve the problem i had to rename the "main.c" to "main.cpp", and voila the compiler said "everythink is fine".

THX for all your help.
closed account (z05DSL3A)
I was leaning towards it being compiled in C, I hadn't noticed the name of the file name though.

Topic archived. No new replies allowed.