VS Arduino
Visual Micro >> Visual Studio 2017, 2019, 2022 >> #pragma once doesn't work
https://www.visualmicro.com/forums/YaBB.pl?num=1571841070

Message started by Evgeny Zhekaus on Oct 23rd, 2019 at 2:31pm

Title: #pragma once doesn't work
Post by Evgeny Zhekaus on Oct 23rd, 2019 at 2: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
[/code]


Title: Re: #pragma once doesn't work
Post by Visual Micro on Oct 23rd, 2019 at 2: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.

Title: Re: #pragma once doesn't work
Post by Evgeny Zhekaus on 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.
https://www.visualmicro.com/forums/YaBB.pl?action=downloadfile;file=output_040.txt ( 87 KB | 2 Downloads )

Title: Re: #pragma once doesn't work
Post by Visual Micro on 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.

Title: Re: #pragma once doesn't work
Post by Paul Martinsen on Apr 28th, 2020 at 11:00pm
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.

Title: Re: #pragma once doesn't work
Post by Visual Micro on Apr 29th, 2020 at 11:31am
Paul I am unsure what you are saying. The #pragma once is not something Visual Micro manages or has control over.


Title: Re: #pragma once doesn't work
Post by Paul Martinsen on May 1st, 2020 at 5: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.

Title: Re: #pragma once doesn't work
Post by Visual Micro on May 1st, 2020 at 12: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.

Title: Re: #pragma once doesn't work
Post by Paul Martinsen on May 1st, 2020 at 9:57pm
All great suggestions! Thanks Tim.

Title: Re: #pragma once doesn't work
Post by Paul Martinsen on Oct 25th, 2022 at 1:20am
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.

Title: Re: #pragma once doesn't work
Post by Tim@Visual Micro on Oct 25th, 2022 at 12:40pm
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

VS Arduino » Powered by YaBB 2.6.12!
YaBB Forum Software © 2000-2024. All Rights Reserved.