Before logging an issue, please update to the latest release of Visual Micro from the Downloads Page.

When Logging a Support Issue in the Forum, please ensure you have also:-

  • Enabled vMicro > Compiler > Show Build Properties
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
  • Click the Reply button and attach as .txt file OR
  • Click here to Email us with the file attached, and a link to your post
Support requests without the output above may be impossible to answer, so please help us to help you
 
Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 8 Replies) #pragma once doesn't work (Read 4731 times)
Evgeny Zhekaus
Junior Member
**
Offline


Posts: 37
Joined: Sep 19th, 2018
#pragma once doesn't work
Oct 23rd, 2019 at 2:31pm
Print Post  
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
Select All
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
 



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


Posts: 12071
Location: United Kingdom
Joined: Apr 10th, 2010
Re: #pragma once doesn't work
Reply #1 - Oct 23rd, 2019 at 2:41pm
Print Post  
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: 37
Joined: Sep 19th, 2018
Re: #pragma once doesn't work
Reply #2 - Feb 16th, 2020 at 6:21am
Print Post  
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.
« Last Edit: Feb 16th, 2020 at 6:23am by Evgeny Zhekaus »  

Please Register or Login to the Forum to see File Attachments
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12071
Location: United Kingdom
Joined: Apr 10th, 2010
Re: #pragma once doesn't work
Reply #3 - Feb 16th, 2020 at 8:22pm
Print Post  
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++)
Select All
#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.
« Last Edit: Feb 16th, 2020 at 8:22pm by Tim@Visual Micro »  
Back to top
WWW  
IP Logged
 
Paul Martinsen
Junior Member
**
Offline


Posts: 14
Joined: Apr 12th, 2017
Re: #pragma once doesn't work
Reply #4 - Apr 28th, 2020 at 11:00pm
Print Post  
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
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12071
Location: United Kingdom
Joined: Apr 10th, 2010
Re: #pragma once doesn't work
Reply #5 - Apr 29th, 2020 at 11:31am
Print Post  
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: 14
Joined: Apr 12th, 2017
Re: #pragma once doesn't work
Reply #6 - May 1st, 2020 at 5:26am
Print Post  
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
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12071
Location: United Kingdom
Joined: Apr 10th, 2010
Re: #pragma once doesn't work
Reply #7 - May 1st, 2020 at 12:00pm
Print Post  
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: 14
Joined: Apr 12th, 2017
Re: #pragma once doesn't work
Reply #8 - May 1st, 2020 at 9:57pm
Print Post  
All great suggestions! Thanks Tim.
  
Back to top
 
IP Logged
 
Paul Martinsen
Junior Member
**
Offline


Posts: 14
Joined: Apr 12th, 2017
Re: #pragma once doesn't work
Reply #9 - Oct 25th, 2022 at 1:20am
Print Post  
A couple of years later I hit the same issue with #pragma once on a new project & version 2022.905.2207 of Visual Micro. 

It looks like this problem occurs because Visual Micro's path include path options add the project source folder, not the build folder, to the path when compiling source files. 

I imagine this confuses gcc's logic for matching header files when it processes the #pragma once options. Turning off the flags and adding -I"{build.path}" to the Extra cpp flags option still resolves this problem.
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12071
Location: United Kingdom
Joined: Apr 10th, 2010
Re: #pragma once doesn't work
Reply #10 - Oct 25th, 2022 at 12:40pm
Print Post  
Thanks, I am not sure the Arduino IDE adds the build path as an include. We will look.

This is the syntax Arduino.cc uses in their headers:-

Quote:
#ifndef _MY_HEADER_h
#define _MY_HEADER_h

     #include "arduino.h"

     //more code here

#endif 
  
Back to top
WWW  
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint