Welcome, Guest. Please Login or Register
Arduino IDE for Visual Studio
 
 
When Logging a Support Issue in the Forum, please ensure you have also:-
  • Enabled vMicro > Compiler > Show Build Properties and Verbose
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
Support requests without the output above may be impossible to answer, so please help us to help you
 
  HomeSearch Tutorials & Wiki visualmicro.com LoginHelpRegister  
 
Page Index Toggle Pages: 1
Send Topic Print
#pragma once doesn't work (Read 538 times)
Evgeny Zhekaus
Junior Member
**
Offline



Posts: 32
#pragma once doesn't work
Oct 23rd, 2019 at 3:31pm
 
I’ve got a quite strange problem. Today, I start work with updating VS to 16.3.6.

After that I’ve got the following error:
```
Code:
Compiling 'Arduino_GSM_Relay' for 'ATmega328P (Nano 57600)'

gsm_action.h:10: In file included from
MyApp.cpp:21: from

GSMClass.h: 17:7: error: redefinition of 'class ZhGSMClass
   class ZhGSMClass \\ **
   ^~~~~~~~~~
Error compiling project sources

MyApp.h:3: In file included from
MyApp.cpp:4: from
GSMClass.h:17: note  previous definition of class ZhGSMClass
   class ZhGSMClass \\ **
   ^~~~~~~~~~
Build failed for project 'Arduino_GSM_Relay'

```

Here is the code:
```
#pragma once

// #ifndef GSMCLASS_H
// #define  GSMCLASS_H

#ifdef ARDUINO
#include <Arduino.h>
#endif

#include <SoftwareSerial.h>

class ZhGSMClass;

typedef void (*SmsParserCb)(ZhGSMClass* pGSM, const char* pcMsg, const char* pcNum);
typedef int (*CallCb)(const char* pcNum);

class ZhGSMClass // **
```

As you see, there is `#pragma once`. And I am certain I’ve got the only file with such a name and the only class definition.
I can’t see such a problem with ordinary C++ projects. That makes me think that the problem relates to vMicro after last VS update.

The problem is gone when I add a classic header guard.
#ifndef GSMCLASS_H
#define  GSMCLASS_H
 



Back to top
« Last Edit: Oct 23rd, 2019 at 3:39pm by Visual Micro »  
WWW  
IP Logged
 
Visual Micro
Administrator
*****
Offline



Posts: 10653
United Kingdom
Re: #pragma once doesn't work
Reply #1 - Oct 23rd, 2019 at 3:41pm
 
Visual Micro and Visual Studio do not have any connection to how the code is compiled. We simply run the respective tool chain .exe to compile each file.

So this sounds like you are using a different or older tool chain. The information requested in the yellow box above will show you the compilation so you can see the .exe that runs the compile for each file.
Back to top
 
WWW  
IP Logged
 
Evgeny Zhekaus
Junior Member
**
Offline



Posts: 32
Re: #pragma once doesn't work
Reply #2 - Feb 16th, 2020 at 6:21am
 
Such a problem became more common on my computer  and I a little tired adding header guards.

So, I became curious again, why #pragma once is ignored...

Here is my compilation log file.

In every header file I have #pragma once at the top. However, in many places it is ignored some-how by the compiler.
Back to top
« Last Edit: Feb 16th, 2020 at 6:23am by Evgeny Zhekaus »  

output_040.txt (87 KB | )
WWW  
IP Logged
 
Visual Micro
Administrator
*****
Offline



Posts: 10653
United Kingdom
Re: #pragma once doesn't work
Reply #3 - Feb 16th, 2020 at 8:22pm
 
Many versions of Gcc does not appear to take any notice of the command. Please use the syntax you see when creating .cpp/.h file using the "vMicro>Add Code" menu. That is the syntax that Arduino uses.

Code (C++):
#ifndef _MY_HEADER_h
#define _MY_HEADER_h

	#include "arduino.h"

	//more code here


#endif 



Replace MY_HEADER with the name of the header file.
Back to top
« Last Edit: Feb 16th, 2020 at 8:22pm by Visual Micro »  
WWW  
IP Logged
 
Paul Martinsen
Junior Member
**
Offline



Posts: 13
Re: #pragma once doesn't work
Reply #4 - Apr 29th, 2020 at 12:00am
 
For me, this problem occurred from two (apparently) new options in VM: Compiler -> add path include for project, Compiler -> add path include for shared projects.

I make extensive use of shared projects and previously added these manually in the project properties. Unchecking both resolved the problem for me.

Having this built into VM saves adding custom properties, but I haven't had time to work that through.
Back to top
 
 
IP Logged
 
Visual Micro
Administrator
*****
Offline



Posts: 10653
United Kingdom
Re: #pragma once doesn't work
Reply #5 - Apr 29th, 2020 at 12:31pm
 
Paul I am unsure what you are saying. The #pragma once is not something Visual Micro manages or has control over.

Back to top
 
WWW  
IP Logged
 
Paul Martinsen
Junior Member
**
Offline



Posts: 13
Re: #pragma once doesn't work
Reply #6 - May 1st, 2020 at 6:26am
 
I'm not sure what I"m saying either! I found this post while trying to solve a similar problem in code that had previously compiled (a year ago) & really just stumbled onto this as I don't believe those complier options were available when I first set up the project.

When I set up the project, I had to add the Extra cpp flags:
-I"{build.path}" -Wno-multichar -Wno-unknown-pragmas  -Wno-reorder

And now with the Compiler -> add path include for project, Compiler -> add path include for shared projects checked, I get a bundle of type redefinition when compiling. 

Maybe it helps someone else. I would have been distraught to have to spread old fashioned included guards everywhere.
Back to top
 
 
IP Logged
 
Visual Micro
Administrator
*****
Offline



Posts: 10653
United Kingdom
Re: #pragma once doesn't work
Reply #7 - May 1st, 2020 at 1:00pm
 
Thanks. I think this means you have updated your toolchain in board manager and that the toolchain previously supported #unknown statements.

It always helps to post your verbose build output with the build properties enabled then we can be more specific in our answers which often depend on the toolchain you have installed for each type of board.

The output would also help us knowif you still have a build failure or not.

Yes, there have been a few changes in the past year to ensure we are arduino compatible by default. There have also been huge changes in arduino and tool chains. The ability to configure compiler paths has been added to visual micro in the past year because the std. arduino way of doing things does not provide that functionality and we needed to be compatible with arduino out of the box.

All changes were added to the relase notes of the respective vendors but I know that doesn't help people who come back to things after a lengthy break.

Sometimes it's best to re-start existing code with same versions of tool chain, arduino ide and visual micro that you used previously. This is so that you can prove things still work before updating.
Back to top
 
WWW  
IP Logged
 
Paul Martinsen
Junior Member
**
Offline



Posts: 13
Re: #pragma once doesn't work
Reply #8 - May 1st, 2020 at 10:57pm
 
All great suggestions! Thanks Tim.
Back to top
 
 
IP Logged
 
Page Index Toggle Pages: 1
Send Topic Print