Problem mit C++ Klasse: expected '=', ',', ';', 'asm' or '__attribute__' be..

OP #2221771
Lesenswert?

Moin,

ich habe eine C++ Klasse geschrieben, die ich in mein AVR-gcc Programm 
einbinden will.

Ich benutzte AVR Studio5.

Ich binde die Klasse ein und lege instanzen an:
1
#include "protocol.h"
2

3
protocol *forward = new protocol();
4
protocol *com_in = new protocol(forward);
5
protocol *com_out = new protocol();

Hier die Klasse, protocol.h
1
class protocol
2
{
3
public:
4
    protocol();
5
    protocol(protocol *forward);
6
    unsigned char decode(unsigned char inChar);
7

8
    #ifndef DEBUG_PC
9
    unsigned char encode(fifo_t *fifo);
10
    #endif
11

12
    unsigned char encode(unsigned char* buffer);
13

14
    unsigned char get_servo();
15
    unsigned char get_motor();
16
    unsigned char get_bow();
17

18
protected:
19
    int check_byte;
20
    unsigned char data[100];
21

22
private:
23
    protocol *forward;
24
    unsigned char mode; //0x00: Normal    0xFF: Forward Data
25
    void reset_data(void);
26
};

Aber ich bekomme immer die Fehlermeldungen:
1
Error  3  expected '=', ',', ';', 'asm' or '__attribute__' before 'protocol'  C:\Users\Philipp\Documents\AVRStudio\Autopilot_0_1\Autopilot_0_1\protocol.h  52  7  Autopilot_0_1
2
Error  4  expected '=', ',', ';', 'asm' or '__attribute__' before '*' token  C:\Users\Philipp\Documents\AVRStudio\Autopilot_0_1\Autopilot_0_1\Autopilot_0_1.c  17  10  Autopilot_0_1
3
Error  5  expected '=', ',', ';', 'asm' or '__attribute__' before '*' token  C:\Users\Philipp\Documents\AVRStudio\Autopilot_0_1\Autopilot_0_1\Autopilot_0_1.c  18  10  Autopilot_0_1
4
Error  6  expected '=', ',', ';', 'asm' or '__attribute__' before '*' token  C:\Users\Philipp\Documents\AVRStudio\Autopilot_0_1\Autopilot_0_1\Autopilot_0_1.c  19  10  Autopilot_0_1

was mache ich falsch?

Ich hoffe ihr könnt mir helfen?

MfG
Philipp
OP #2221827
Lesenswert?

Hallo,

die Klasse liegt in der protocol.h/protocol.cpp.
Ich mache nur die Klassen in C++ Dateien, den Rest wollte ich in C 
Dateien machen, oder wärs da sinnvoller direkt nur C++ Dateien zu 
verwenden?
Hier mal alle Dateien:
Autopilot.c
1
#include "defines.h"
2
#include "functions.h"
3
#include "uarts.h"
4
#include "fifo.h"
5
#include "protocol.h"
6
#include "navi.h"
7

8
#include <avr/io.h>
9
#include <stdio.h>
10
#include <stdlib.h>
11

12
fifo_t GPS;
13
fifo_t xbee_in;
14
fifo_t xbee_out;
15

16

17
protocol *forward = new protocol();
18
protocol *com_in = new protocol(forward);
19
protocol *com_out = new protocol();
20

21
volatile int counter;
22

23
int main(void){
24
  
25
  TCCR0B = ( 1 << CS02 ) | ( 1 << CS00 ); // Teiler: 1024
26
  TIMSK0 = ( 1 << TOIE0 );         // Overflow Interrupt einschalten
27
  
28
  //Leds
29
  DDRC |= (1<<2)|(1<<5);
30
  DDRD |= (1<<7);
31
  
32
  set_leds(0x07);
33
  
34
  //FIFO Buffer
35
  uint8_t gps_buffer[BUFFER_SIZE];
36
  fifo_init(&GPS,gps_buffer,BUFFER_SIZE);
37
  
38
  uint8_t xbee_in_buffer[BUFFER_SIZE];
39
  fifo_init(&xbee_in,xbee_in_buffer,BUFFER_SIZE);
40
  
41
  uint8_t xbee_out_buffer[BUFFER_SIZE];
42
  fifo_init(&xbee_out,xbee_out_buffer,BUFFER_SIZE);
43
  
44
  sei();
45
  uart_init();
46
  
47
  uint8_t a[] = {"\nReady for Connection\n"};
48
  for(int i=0;i<22;i++){
49
    fifo_put(&xbee_out,a[i]);
50
  }
51
  
52
    while(1){
53
    
54
        int tmp = fifo_get(&xbee_in);
55
    if(tmp!=-1){
56
      uint8_t t = decode((uint8_t)tmp);
57
      if(t==0xff){
58
        //Decoded
59
      }else if(t==0x01){
60
        fifo_put(&xbee_out,'E');
61
        fifo_put(&xbee_out,'R');
62
        fifo_put(&xbee_out,'R');
63
        fifo_put(&xbee_out,'O');
64
        fifo_put(&xbee_out,'R');
65
        set_leds(0x04);
66
      }
67
    }      
68
    }
69
}
70

71
ISR(USART0_RX_vect){
72
  uint8_t tmp = UDR0;
73
  fifo_put(&GPS,tmp);
74
}
75

76
ISR(USART1_RX_vect){
77
  uint8_t tmp = UDR1;
78
  fifo_put(&xbee_in,tmp);
79
}
80

81
ISR(USART1_UDRE_vect){
82
  int tmp = fifo_get(&xbee_out);
83
  if(tmp==-1){
84
    set_leds(0x00);
85
    UCSR1B &= ~(1<<UDRIE1);
86
  }else{
87
    set_leds(0x01);
88
    UDR1 = (uint8_t)tmp;
89
  }
90
}
91

92
//Timer Overflow
93
ISR(TIMER0_OVF_vect){
94
  counter++;
95
  if(counter>=10){
96
    
97
    //Senden
98
    
99
    counter = 0;
100
  }
101
}

protocol.h
1
#ifndef PROTOCOL_H
2
#define PROTOCOL_H
3

4
//#define DEBUG_PC
5

6
#include <stdio.h>
7

8
#ifndef DEBUG_PC
9
#include "fifo.h"
10
#endif
11

12
#define START_BYTE '*'
13
#define END_BYTE '$'
14

15
#define SERVO 0x01
16
#define MOTOR 0x02
17
#define BOW 0x03
18

19
#define STATUS_1 0x04
20
#define STATUS_2 0x05
21

22
#define KRAN 0x06
23

24
#define AUTOPILOT 0x07
25

26
#define NEW_WAYPOINT_ID 0x08
27
#define NEW_WAYPOINT_LON1 0x09
28
#define NEW_WAYPOINT_LON2 0x0A
29
#define NEW_WAYPOINT_LON3 0x0B
30
#define NEW_WAYPOINT_LON4 0x0C
31
#define NEW_WAYPOINT_LAT1 0x0D
32
#define NEW_WAYPOINT_LAT2 0x0E
33
#define NEW_WAYPOINT_LAT3 0x0F
34
#define NEW_WAYPOINT_LAT4 0x10
35

36
#define DIRECT 0x11
37

38
#define KURS_SOLL 0x12
39
#define KURS_AKTUELL 0x13
40

41
#define RGB_ID 0x14
42
#define RGB_R 0x15
43
#define RGB_G 0x16
44
#define RGB_B 0x17
45

46
#define LICHT_1 0x18
47
#define LICHT_2 0x19
48
#define LICHT_3 0x20
49
#define LICHT_4 0x21
50
#define LICHT_5 0x22
51

52
class protocol
53
{
54
public:
55
    protocol();
56
    protocol(protocol *forward);
57
    unsigned char decode(unsigned char inChar);
58

59
    #ifndef DEBUG_PC
60
    unsigned char encode(fifo_t *fifo);
61
    #endif
62

63
    unsigned char encode(unsigned char* buffer);
64

65
    unsigned char get_servo();
66
    unsigned char get_motor();
67
    unsigned char get_bow();
68

69
protected:
70
    int check_byte;
71
    unsigned char data[100];
72

73
private:
74
    protocol *forward;
75
    unsigned char mode; //0x00: Normal    0xFF: Forward Data
76
    void reset_data(void);
77
};
78

79

80
#endif // PROTOCOL_H

protocol.cpp
1
#include "protocol.h"
2

3
protocol::protocol()
4
{
5
    this->check_byte = 0x00;
6
    this->mode = 0x00;
7
    this->reset_data();
8
}
9

10
protocol::protocol(protocol *forward)
11
{
12
    this->mode = 0xff;
13
    this->forward = forward;
14
    this->check_byte = 0x00;
15
    this->reset_data();
16
}
17

18
unsigned char protocol::decode(unsigned char inChar)
19
{
20
    static unsigned char buffer[100];
21
    static unsigned char indexer;
22

23
    if(inChar==START_BYTE)
24
    {
25
        for(unsigned char i=0; i<100; i++)
26
        {
27
            buffer[i] = 0;
28
        }
29
        indexer = 0;
30
    }
31

32
    buffer[indexer] = inChar;
33

34
    if(buffer[indexer]==END_BYTE)
35
    {
36
        if(buffer[0]==START_BYTE)
37
        {
38
            unsigned char tmp_indexer = 2;
39
            unsigned char tmp_letter = buffer[1];
40
            while(tmp_indexer<indexer)
41
            {
42
                switch(tmp_letter)
43
                {
44
                case SERVO: //Servo
45
                    this->data[SERVO] = buffer[tmp_indexer];
46
                    if(this->mode==0xff)
47
                    {
48
                        if(this->data[AUTOPILOT]==0)
49
                        {
50
                            this->forward->data[SERVO] = this->data[SERVO];
51
                            this->forward->check_byte |= (1<<SERVO);
52
                        }
53
                    }else{
54
                        this->check_byte |= (1<<SERVO);
55
                    }
56
                    break;
57
                case MOTOR: //Motor
58
                    this->data[MOTOR] = buffer[tmp_indexer];
59
                    if(this->mode==0xff)
60
                    {
61
                        if(this->data[AUTOPILOT]==0)
62
                        {
63
                            this->forward->data[MOTOR] = this->data[MOTOR];
64
                            this->forward->check_byte |= (1<<MOTOR);
65
                        }
66
                    }else{
67
                        this->check_byte |= (1<<MOTOR);
68
                    }
69
                    break;
70
                case BOW:
71
                    this->data[BOW] = buffer[tmp_indexer];
72
                    if(this->mode==0xff)
73
                    {
74
                        if(this->data[AUTOPILOT]==0)
75
                        {
76
                            this->forward->data[BOW] = this->data[BOW];
77
                            this->forward->check_byte |= (1<<BOW);
78
                        }
79
                    }else{
80
                        this->check_byte |= (1<<BOW);
81
                    }
82
                    break;
83
                case STATUS_1:
84
                    this->data[STATUS_1] = buffer[tmp_indexer];
85
                    tmp_indexer++;
86
                    this->data[STATUS_2] = buffer[tmp_indexer];
87
                    this->check_byte |= (1<<STATUS_1);
88
                    break;
89
                case KRAN:
90
                    this->data[KRAN] = buffer[tmp_indexer];
91
                    if(this->mode==0xff)
92
                    {
93
                        this->forward->data[KRAN] = this->data[KRAN];
94
                        this->forward->check_byte |= (1<<KRAN);
95
                    }else{
96
                        this->check_byte |= (1<<KRAN);
97
                    }
98
                    break;
99
                case AUTOPILOT:
100
                    this->data[AUTOPILOT] = buffer[tmp_indexer];
101
                    this->check_byte |= (1<<AUTOPILOT);
102
                    break;
103
                case NEW_WAYPOINT_ID:
104
                    this->data[NEW_WAYPOINT_ID] = buffer[tmp_indexer];
105
                    tmp_indexer++;
106
                    this->data[NEW_WAYPOINT_LON1] = buffer[tmp_indexer];
107
                    tmp_indexer++;
108
                    this->data[NEW_WAYPOINT_LON2] = buffer[tmp_indexer];
109
                    tmp_indexer++;
110
                    this->data[NEW_WAYPOINT_LON3] = buffer[tmp_indexer];
111
                    tmp_indexer++;
112
                    this->data[NEW_WAYPOINT_LON4] = buffer[tmp_indexer];
113
                    tmp_indexer++;
114
                    this->data[NEW_WAYPOINT_LAT1] = buffer[tmp_indexer];
115
                    tmp_indexer++;
116
                    this->data[NEW_WAYPOINT_LAT2] = buffer[tmp_indexer];
117
                    tmp_indexer++;
118
                    this->data[NEW_WAYPOINT_LAT3] = buffer[tmp_indexer];
119
                    tmp_indexer++;
120
                    this->data[NEW_WAYPOINT_LAT4] = buffer[tmp_indexer];
121
                    this->check_byte |= (1<<NEW_WAYPOINT_ID);
122
                    break;
123
                case DIRECT:
124
                    this->data[DIRECT] = buffer[tmp_indexer];
125
                    this->check_byte |= (1<<DIRECT);
126
                    break;
127
                case RGB_ID:
128
                    if(mode==0xff)
129
                    {
130
                        this->forward->data[RGB_ID] = buffer[tmp_indexer];
131
                        tmp_indexer++;
132
                        this->forward->data[RGB_R] = buffer[tmp_indexer];
133
                        tmp_indexer++;
134
                        this->forward->data[RGB_G] = buffer[tmp_indexer];
135
                        tmp_indexer++;
136
                        this->forward->data[RGB_B] = buffer[tmp_indexer];
137
                        this->forward->check_byte |= (1<<RGB_ID);
138
                    }
139
                    else
140
                    {
141
                        this->data[RGB_ID] = buffer[tmp_indexer];
142
                        tmp_indexer++;
143
                        this->data[RGB_R] = buffer[tmp_indexer];
144
                        tmp_indexer++;
145
                        this->data[RGB_G] = buffer[tmp_indexer];
146
                        tmp_indexer++;
147
                        this->data[RGB_B] = buffer[tmp_indexer];
148
                        this->check_byte |= (1<<RGB_ID);
149
                    }
150
                    break;
151
                case LICHT_1:
152
                    if(mode==0xff)
153
                    {
154
                        this->forward->data[LICHT_1] = buffer[tmp_indexer];
155
                        tmp_indexer++;
156
                        this->forward->data[LICHT_2] = buffer[tmp_indexer];
157
                        tmp_indexer++;
158
                        this->forward->data[LICHT_3] = buffer[tmp_indexer];
159
                        tmp_indexer++;
160
                        this->forward->data[LICHT_4] = buffer[tmp_indexer];
161
                        this->forward->check_byte |= (1<<LICHT_1);
162
                        break;
163
                    }else{
164
                        this->data[LICHT_1] = buffer[tmp_indexer];
165
                        tmp_indexer++;
166
                        this->data[LICHT_2] = buffer[tmp_indexer];
167
                        tmp_indexer++;
168
                        this->data[LICHT_3] = buffer[tmp_indexer];
169
                        tmp_indexer++;
170
                        this->data[LICHT_4] = buffer[tmp_indexer];
171
                        this->check_byte |= (1<<LICHT_1);
172
                        break;
173
                    }
174
                }
175
                tmp_letter = buffer[tmp_indexer];
176
                tmp_indexer++;
177
            }
178
            return 0xff;
179
        }
180
        return 0x01;
181
    }
182

183
    indexer++;
184
    return 0x00;
185
}
186

187
#ifndef DEBUG_PC
188
unsigned char protocol::encode(fifo_t *fifo)
189
{
190
    if(this->check_byte!=0x00)
191
    {
192
        fifo_put(&fifo,STARTBYTE);
193
        if(this->check_byte&(1<<SERVO))
194
        {
195
            fifo_put(&fifo,SERVO);
196
            fifo_put(&fifo,data[SERVO]);
197
        }
198

199
        if(this->check_byte&(1<<MOTOR))
200
        {
201
            fifo_put(&fifo,MOTOR);
202
            fifo_put(&fifo,data[MOTOR]);
203
        }
204

205
        if(this->check_byte&(1<<BOW))
206
        {
207
            fifo_put(&fifo,BOW);
208
            fifo_put(&fifo,data[BOW]);
209
        }
210

211
        if(this->check_byte&(1<<STATUS_1))
212
        {
213
            fifo_put(&fifo,STATUS_1);
214
            fifo_put(&fifo,data[STATUS_1]);
215
            fifo_put(&fifo,data[STATUS_2]);
216
        }
217

218
        if(this->check_byte&(1<<KRAN))
219
        {
220
            fifo_put(&fifo,KRAN);
221
            fifo_put(&fifo,data[KRAN]);
222
        }
223

224
        if(this->check_byte&(1<<AUTOPILOT))
225
        {
226
            fifo_put(&fifo,AUTOPILOT);
227
            fifo_put(&fifo,data[AUTOPILOT]);
228
        }
229

230
        if(this->check_byte&(1<<NEW_WAYPOINT_ID))
231
        {
232
            fifo_put(&fifo,NEW_WAYPOINT_ID);
233
            fifo_put(&fifo,data[NEW_WAYPOINT_ID]);
234
            fifo_put(&fifo,data[NEW_WAYPOINT_LON1]);
235
            fifo_put(&fifo,data[NEW_WAYPOINT_LON2]);
236
            fifo_put(&fifo,data[NEW_WAYPOINT_LON3]);
237
            fifo_put(&fifo,data[NEW_WAYPOINT_LON4]);
238
            fifo_put(&fifo,data[NEW_WAYPOINT_LAT1]);
239
            fifo_put(&fifo,data[NEW_WAYPOINT_LAT2]);
240
            fifo_put(&fifo,data[NEW_WAYPOINT_LAT3]);
241
            fifo_put(&fifo,data[NEW_WAYPOINT_LAT4]);
242
        }
243

244
        if(this->check_byte&(1<<DIRECT))
245
        {
246
            fifo_put(&fifo,DIRECT);
247
            fifo_put(&fifo,data[DIRECT]);
248
        }
249

250
        if(this->check_byte&(1<<KURS_AKTUELL))
251
        {
252
            fifo_put(&fifo,KURS_AKTUELL);
253
            fifo_put(&fifo,data[KURS_AKTUELL]);
254
        }
255

256
        if(this->check_byte&(1<<KURS_SOLL))
257
        {
258
            fifo_put(&fifo,KURS_SOLL);
259
            fifo_put(&fifo,data[KURS_SOLL]);
260
        }
261

262
        if(this->check_byte&(1<<RGB_ID))
263
        {
264
            fifo_put(&fifo,RGB_ID;
265
            fifo_put(&fifo,data[RGB_ID]);
266
            fifo_put(&fifo,data[RGB_R]);
267
            fifo_put(&fifo,data[RGB_G]);
268
            fifo_put(&fifo,data[RGB_B]);
269
        }
270

271
        if(this->check_byte&(1<<LICHT_1))
272
        {
273
            fifo_put(&fifo,LICHT_1;
274
            fifo_put(&fifo,data[LICHT_1]);
275
            fifo_put(&fifo,data[LICHT_2]);
276
            fifo_put(&fifo,data[LICHT_3]);
277
            fifo_put(&fifo,data[LICHT_4]);
278
            fifo_put(&fifo,data[LICHT_5]);
279
        }
280

281
        fifo_put(&fifo,END_BYTE);
282
        return 0xff;
283
    }
284
    return 0;
285
}
286
#endif
287

288
unsigned char protocol::encode(unsigned char* buffer)
289
{
290
    if(this->check_byte!=0x00)
291
    {
292
        buffer[0] = START_BYTE;
293
        unsigned char indexer = 1;
294
        if(this->check_byte&(1<<SERVO))
295
        {
296
            buffer[indexer] = SERVO;
297
            buffer[indexer+1] = data[SERVO];
298
            indexer+=2;
299
        }
300

301
        if(this->check_byte&(1<<MOTOR))
302
        {
303
            buffer[indexer] = MOTOR;
304
            buffer[indexer+1] = data[MOTOR];
305
            indexer+=2;
306
        }
307

308
        if(this->check_byte&(1<<BOW))
309
        {
310
            buffer[indexer] = BOW;
311
            buffer[indexer+1] = data[BOW];
312
            indexer+=2;
313
        }
314

315
        if(this->check_byte&(1<<STATUS_1))
316
        {
317
            buffer[indexer] = STATUS_1;
318
            buffer[indexer+1] = data[STATUS_1];
319
            buffer[indexer+2] = data[STATUS_2];
320
            indexer+=3;
321
        }
322

323
        if(this->check_byte&(1<<KRAN))
324
        {
325
            buffer[indexer] = KRAN;
326
            buffer[indexer+1] = data[KRAN];
327
            indexer+=2;
328
        }
329

330
        if(this->check_byte&(1<<AUTOPILOT))
331
        {
332
            buffer[indexer] = AUTOPILOT;
333
            buffer[indexer+1] = data[AUTOPILOT];
334
            indexer+=2;
335
        }
336

337
        if(this->check_byte&(1<<NEW_WAYPOINT_ID))
338
        {
339
            buffer[indexer] = NEW_WAYPOINT_ID;
340
            buffer[indexer+1] = data[NEW_WAYPOINT_ID];
341
            buffer[indexer+2] = data[NEW_WAYPOINT_LON1];
342
            buffer[indexer+3] = data[NEW_WAYPOINT_LON2];
343
            buffer[indexer+4] = data[NEW_WAYPOINT_LON3];
344
            buffer[indexer+5] = data[NEW_WAYPOINT_LON4];
345
            indexer+=6;
346
            buffer[indexer] = data[NEW_WAYPOINT_LAT1];
347
            buffer[indexer+1] = data[NEW_WAYPOINT_LAT2];
348
            buffer[indexer+2] = data[NEW_WAYPOINT_LAT3];
349
            buffer[indexer+3] = data[NEW_WAYPOINT_LAT4];
350
            indexer+=4;
351
        }
352

353
        if(this->check_byte&(1<<DIRECT))
354
        {
355
            buffer[indexer] = DIRECT;
356
            buffer[indexer+1] = data[DIRECT];
357
            indexer+=2;
358
        }
359

360
        if(this->check_byte&(1<<KURS_AKTUELL))
361
        {
362
            buffer[indexer] = KURS_AKTUELL;
363
            buffer[indexer+1] = data[KURS_AKTUELL];
364
            indexer+=2;
365
        }
366

367
        if(this->check_byte&(1<<KURS_SOLL))
368
        {
369
            buffer[indexer] = KURS_SOLL;
370
            buffer[indexer+1] = data[KURS_SOLL];
371
            indexer+=2;
372
        }
373

374
        if(this->check_byte&(1<<RGB_ID))
375
        {
376
            buffer[indexer] = RGB_ID;
377
            buffer[indexer+1] = data[RGB_ID];
378
            buffer[indexer+2] = data[RGB_R];
379
            buffer[indexer+3] = data[RGB_G];
380
            buffer[indexer+4] = data[RGB_B];
381
            indexer+=5;
382
        }
383

384
        if(this->check_byte&(1<<LICHT_1))
385
        {
386
            buffer[indexer] = LICHT_1;
387
            buffer[indexer+1] = data[LICHT_1];
388
            buffer[indexer+2] = data[LICHT_2];
389
            buffer[indexer+3] = data[LICHT_3];
390
            buffer[indexer+4] = data[LICHT_4];
391
            buffer[indexer+5] = data[LICHT_5];
392
            indexer+=6;
393
        }
394

395
        buffer[indexer] = END_BYTE;
396
        return 0xff;
397
    }
398
    return 0x00;
399
}
400

401
unsigned char protocol::get_servo()
402
{
403
    return this->data[SERVO];
404
}
405

406
unsigned char protocol::get_motor()
407
{
408
    return this->data[MOTOR];
409
}
410

411
unsigned char protocol::get_bow()
412
{
413
    return this->data[BOW];
414
}
415

416
void protocol::reset_data(void)
417
{
418
    for(unsigned char i=0;i<30;i++)
419
    {
420
        this->data[i] = 0;
421
    }
422
    check_byte = 0x00;
423
}

Ich habe die Klasse vorher in einem C++ Prgramm auf dem Rechner mit 
Codeblocks getestet, und da ging alles.

Ist es generell eigentlich möglich, das Komplette AVR-Programm einfach 
in C++ zu schreiben?

MfG
Philipp
Gast #2221836
Lesenswert?

Lange Dateien als Anhang.

Philipp Maricek schrieb:
> protocol *forward = new protocol();
> protocol *com_in = new protocol(forward);
> protocol *com_out = new protocol();

Hier willst du Code außerhalb eines Blocks ausführen, das wird so nicht 
gehen. Trenne Deklaration und Definition auf, wenn die Variablen global 
sein müssen.
#2221996
Lesenswert?

Das kommt drauf an.

Und zwar weniger auf deine Aufgabenstellung, sondern ob man mit C++ 
umgehen kann.

Man kann vieles deutlich eleganter, prägnanter und wartbarer 
formulieren.
Schon in C kann man prinzipiell alles machen, kann also auch viel falsch 
machen - wenn man es nicht verstanden hat.
In C++ hat man dann deutlich mehr Möglichkeiten, Gutes oder Schlechtes 
zu tun.

Ich nehme auch auf einem MC deutlich lieber C++, wobei die Programme 
über weite Strecken dadurch nicht anders aussehen (auf einem MC 
zumindest).
Aber an vielen Stellen kann man C++ dann sehr gut brauchen.

Ein Aufteilen auf C- und C++-Teile ist möglich, aber nicht unbedingt 
sinnvoll (es sei denn, man hat bereits C-Teile fertig; die kann man 
natürlich unverändert nehmen).
Schließlich kann man in eine *.cpp ja genau dasselbe reinschreiben wie 
in eine *.c, solange man die Möglichkeiten von C++ gerade nicht braucht.
OP #2222067
Lesenswert?

Ich habe gerade mal Probiert in Avr Studio 5 ein Projekt zu erstellen. 
Die Datei mit der main Funktion habe ich in eine C++ Datei umbenannt.
Beim Kompilieren bekam ich zuerst immer nur die Meldung:
1
undefined referenze to main
nach ein paar Versuchen, steht jetzt nichts mehr in der Error List, 
sondern nur noch das:
1
------ Build started: Project: Autopilot_0_2, Configuration: Debug AVR ------
2
Build started.
3
Project "Autopilot_0_2.avrgccproj" (default targets):
4
Target "PreBuildEvent" skipped, due to false condition; ('$(PreBuildEvent)'!='') was evaluated as (''!='').
5
Target "CoreBuild" in file "C:\Program Files (x86)\Atmel\AVR Studio 5.0\Vs\AvrGCC.targets" from project "C:\Users\Philipp\Documents\AVRStudio\Autopilot_0_2\Autopilot_0_2\Autopilot_0_2.avrgccproj" (target "Build" depends on it):
6
  Task "RunAvrGCC"
7
    C:\Program Files (x86)\Atmel\AVR Studio 5.0\AVR ToolChain\bin\make.exe all 
8
    make: *** Keine Regel vorhanden, um das Target ╗makedep.mk½, 
9
      ben÷tigt von ╗Autopilot_0_2.elf½, zu erstellen.  Schluss.
10
  Done executing task "RunAvrGCC" -- FAILED.
11
Done building target "CoreBuild" in project "Autopilot_0_2.avrgccproj" -- FAILED.
12
Done building project "Autopilot_0_2.avrgccproj" -- FAILED.
13

14
Build FAILED.
15
========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========

Was mache ich falsch?

MfG
Philipp
OP #2222085
Lesenswert?

Hier das MakeFile, habs vergessen zu ändern. Ist jetzt auf 
Autopilot_0_2.cpp
Ich habe die Datei aber direkt im Studio umbenannt.
1
################################################################################
2
# Automatically-generated file. Do not edit!
3
################################################################################
4

5
SHELL := cmd.exe
6
RM := rm -rf
7

8
USER_OBJS :=
9

10
LIBS := 
11
PROJ := 
12

13
O_SRCS := 
14
C_SRCS := 
15
S_SRCS := 
16
S_UPPER_SRCS := 
17
OBJ_SRCS := 
18
ASM_SRCS := 
19
PREPROCESSING_SRCS := 
20
OBJS := 
21
OBJS_AS_ARGS := 
22
C_DEPS := 
23
C_DEPS_AS_ARGS := 
24
EXECUTABLES := 
25
LIB_AS_ARGS :=
26
OUTPUT_FILE_PATH :=
27
OUTPUT_FILE_PATH_AS_ARGS :=
28
HEX_FLASH_FILE_PATH :=
29
HEX_FLASH_FILE_PATH_AS_ARGS :=
30
HEX_EEPROM_FILE_PATH :=
31
HEX_EEPROM_FILE_PATH_AS_ARGS :=
32
LSS_FILE_PATH :=
33
LSS_FILE_PATH_AS_ARGS :=
34
MAP_FILE_PATH :=
35
MAP_FILE_PATH_AS_ARGS :=
36
AVR_APP_PATH :=C:/Program Files (x86)/Atmel/AVR Studio 5.0/AVR ToolChain/bin/
37
QUOTE := "
38
ADDITIONAL_DEPENDENCIES:=
39
OUTPUT_FILE_DEP:=
40

41
# Every subdirectory with source files must be described here
42
SUBDIRS := 
43

44

45
# Add inputs and outputs from these tool invocations to the build variables 
46
C_SRCS +=  \
47
../Autopilot_0_2.cpp
48

49

50
PREPROCESSING_SRCS += 
51

52

53
ASM_SRCS += 
54

55

56
OBJS +=  \
57
Autopilot_0_2.o
58

59

60
OBJS_AS_ARGS +=  \
61
Autopilot_0_2.o
62

63

64
C_DEPS +=  \
65
Autopilot_0_2.d
66

67

68
C_DEPS_AS_ARGS +=  \
69
Autopilot_0_2.d
70

71

72
OUTPUT_FILE_PATH +=Autopilot_0_2.elf
73

74
OUTPUT_FILE_PATH_AS_ARGS +=Autopilot_0_2.elf
75

76
HEX_FLASH_FILE_PATH +=Autopilot_0_2.hex
77

78
HEX_FLASH_FILE_PATH_AS_ARGS +=Autopilot_0_2.hex
79

80
HEX_EEPROM_FILE_PATH +=Autopilot_0_2.eep
81

82
HEX_EEPROM_FILE_PATH_AS_ARGS +=Autopilot_0_2.eep
83

84
LSS_FILE_PATH +=Autopilot_0_2.lss
85

86
LSS_FILE_PATH_AS_ARGS +=Autopilot_0_2.lss
87

88
MAP_FILE_PATH =Autopilot_0_2.map
89

90
MAP_FILE_PATH_AS_ARGS =Autopilot_0_2.map
91

92
LIB_AS_ARGS +=libAutopilot_0_2.a
93

94
ADDITIONAL_DEPENDENCIES:= $(HEX_FLASH_FILE_PATH) $(LSS_FILE_PATH) $(HEX_EEPROM_FILE_PATH) size
95

96
OUTPUT_FILE_DEP:= ./makedep.mk
97

98
# AVR/GNU C Compiler
99

100
./%.o: .././%.c
101
  @echo Building file: $<
102
  @echo Invoking: AVR/GNU C Compiler
103
  $(QUOTE)$(AVR_APP_PATH)avr-gcc.exe$(QUOTE) -funsigned-char -funsigned-bitfields -O0 -fpack-struct -fshort-enums -g2 -Wall -c -std=gnu99  -mmcu=atmega1284p   -MD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<"
104
  @echo Finished building: $<
105

106
./%.o: .././%.cpp
107
  @echo Building file: $<
108
  @echo Invoking: AVR/GNU CPP Compiler
109
  $(QUOTE)$(AVR_APP_PATH)avr-g++.exe$(QUOTE)   -MD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<"
110
  @echo Finished building: $<
111

112

113

114
# AVR/GNU Assembler
115

116

117

118

119
ifneq ($(MAKECMDGOALS),clean)
120
ifneq ($(strip $(C_DEPS)),)
121
-include $(C_DEPS)
122
endif
123
endif
124

125
# Add inputs and outputs from these tool invocations to the build variables 
126

127
# All Target
128
all: $(OUTPUT_FILE_PATH) $(ADDITIONAL_DEPENDENCIES)
129

130
# AVR/GNU C/C++ Linker
131
$(OUTPUT_FILE_PATH): $(OBJS) $(USER_OBJS) $(OUTPUT_FILE_DEP)
132
  @echo Building target: $@
133
  @echo Invoking: AVR/GNU C/C++ Linker
134
  $(QUOTE)$(AVR_APP_PATH)avr-gcc.exe$(QUOTE)  -mmcu=atmega1284p  -Wl,-Map=$(MAP_FILE_PATH_AS_ARGS) -o$(OUTPUT_FILE_PATH_AS_ARGS) $(OBJS_AS_ARGS) $(USER_OBJS) $(LIBS)
135
  @echo Finished building target: $@
136

137

138

139
$(HEX_FLASH_FILE_PATH): $(OUTPUT_FILE_PATH)
140
  $(QUOTE)$(AVR_APP_PATH)avr-objcopy.exe$(QUOTE) -O ihex -R .eeprom -R .fuse -R .lock -R .signature  $(QUOTE)$<$(QUOTE) $(QUOTE)$@$(QUOTE)
141

142
$(HEX_EEPROM_FILE_PATH): $(OUTPUT_FILE_PATH)
143
  -$(QUOTE)$(AVR_APP_PATH)avr-objcopy.exe$(QUOTE) -j .eeprom --set-section-flags=.eeprom=alloc,load --change-section-lma .eeprom=0 --no-change-warnings -O ihex $(QUOTE)$<$(QUOTE) $(QUOTE)$@$(QUOTE) || exit 0
144

145
$(LSS_FILE_PATH): $(OUTPUT_FILE_PATH)
146
  $(QUOTE)$(AVR_APP_PATH)avr-objdump.exe$(QUOTE) -h -S $(QUOTE)$<$(QUOTE) > $(QUOTE)$@$(QUOTE)
147

148
size: $(OUTPUT_FILE_PATH)
149
  @$(QUOTE)$(AVR_APP_PATH)avr-size.exe$(QUOTE) -C --mcu=atmega1284p $(OUTPUT_FILE_PATH_AS_ARGS)
150

151
# Other Targets
152
clean:
153
  -$(RM) $(OBJS_AS_ARGS)$(C_DEPS_AS_ARGS) $(EXECUTABLES) $(LIB_AS_ARGS) $(HEX_FLASH_FILE_PATH_AS_ARGS) $(HEX_EEPROM_FILE_PATH_AS_ARGS) $(LSS_FILE_PATH_AS_ARGS) $(MAP_FILE_PATH_AS_ARGS)

Jetzt gibts aber einen neuen Fehler:
1
Error  1  avr architecture of input file `Autopilot_0_2.o' is incompatible with avr:51 output  C:\Users\Philipp\Documents\AVRStudio\Autopilot_0_2\Autopilot_0_2\Debug  1  1  Autopilot_0_2

MfG
Philipp
#2222117
Lesenswert?

Da sind ja auch für *.c und *.cpp andere Compileroptionen definiert:
1
...
2
./%.o: .././%.c
3
  @echo Building file: $<
4
  @echo Invoking: AVR/GNU C Compiler
5
  $(QUOTE)$(AVR_APP_PATH)avr-gcc.exe$(QUOTE) -funsigned-char -funsigned-bitfields -O0 -fpack-struct -fshort-enums -g2 -Wall -c -std=gnu99  -mmcu=atmega1284p   -MD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<"
6
  @echo Finished building: $<
7

8
./%.o: .././%.cpp
9
  @echo Building file: $<
10
  @echo Invoking: AVR/GNU CPP Compiler
11
  $(QUOTE)$(AVR_APP_PATH)avr-g++.exe$(QUOTE)   -MD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<"
12
  @echo Finished building: $<
13
...

Ich weiß nicht, ob man beim AVRStudio einfach das Makefile ändert, oder 
irgendwo anders die Compileroptionen ändert.
Aber jedenfalls sollte man auch bei C++ -mmcu=atmega1284p setzen.
-fpack-struct -fshort-enums würde ich auch angleichen.

Und natürlich ein komplettes Rebuild machen (make clean).

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren