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
Normal Topic How to? .h definitions not being recognized (Read 5403 times)
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12076
Location: United Kingdom
Joined: Apr 10th, 2010
How to? .h definitions not being recognized
Jan 19th, 2016 at 7:25pm
Print Post  
Quote:
Is the main Arduino .ino file just another .cpp file insofar as Visual Micro, or, for that matter, Visual Studio is concerned?

For example, if I have myProject.ino can I #include myProject.h within myProject.ino as I would any other .cpp file?

In Visual Studio I see a file called .myProject.vsarduino.h in the Visual Studio explorer, which is a Visual Micro generated file.  Would that interfere with an additional file .h file, e.g., myProject.h?

Thanks.


Hi

The project_name.ino is always the main file.

It becomes cpp during compile so you can include .h files as if it is a .cpp

You should ignore vsarduino.h it is for intellisense and is not part of compile.
« Last Edit: Jan 21st, 2016 at 1:47am by Tim@Visual Micro »  
Back to top
WWW  
IP Logged
 
arduinoUser2
Junior Member
**
Offline


Posts: 41
Joined: Dec 30th, 2015
Re: Error - Multiple definition of variable
Reply #1 - Jan 19th, 2016 at 10:48pm
Print Post  
I had assumed that the .ino file was just the main .cpp file.

The problem I am having is that when I create a project_name.h file and #include it in project_name.ino, the project will not compile.  I get the following:

Compiling debug version of 'Temp_RH_WiFi_Arduino' for 'Arduino/Genuino Uno'
Temp_RH_WiFi_Arduino.cpp.o:In function `__static_initialization_and_destruction_0
Temp_RH_WiFi_Arduino.ino:~SoftwareSerial()
Temp_RH_WiFi_Arduino.cpp.o:In function `setup
Temp_RH_WiFi_Arduino.ino:begin(long)
Temp_RH_WiFi_Arduino.ino:listen()
Temp_RH_WiFi_Arduino.cpp.o:In function `__static_initialization_and_destruction_0
Temp_RH_WiFi_Arduino.ino:SoftwareSerial(unsigned char, unsigned char, bool)
ESP8266_Functions.cpp.o:In function `ESP8266CmdResponse(char const*, unsigned long, unsigned long, SoftwareSerial&)
ESP8266_Functions.cpp:listen()
collect2.exe*:error: ld returned 1 exit status
Error creating .elf

When I comment out the lines in project_name.h and simply add those lines to the project_name.ino file it does compile.

Any idea why this would be the case?
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12076
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Error - .h definitions not being recognized
Reply #2 - Jan 19th, 2016 at 11:09pm
Print Post  
Email your code to info[at]visualmicro.com

Thanks
  
Back to top
WWW  
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12076
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Error - .h definitions not being recognized
Reply #3 - Jan 19th, 2016 at 11:47pm
Print Post  
Thanks for the code

For C++ you need to mark external code as extern

Code
Select All
extern  




tips

You can remove all folder paths from libraries. Ensure that all used libraries are included in the .ino so that library paths are resolved for intellisense correctly.

#include <Lib.h>

not

#include <Lib\Lib.h>

You should not create a variable called ESP8266 because it is #defined as part of the tool chain. Your softwareSerial is called that.

avr\ relates to arduino Uno


« Last Edit: Jan 20th, 2016 at 12:08am by Tim@Visual Micro »  
Back to top
WWW  
IP Logged
 
arduinoUser2
Junior Member
**
Offline


Posts: 41
Joined: Dec 30th, 2015
Re: Error - .h definitions not being recognized
Reply #4 - Jan 20th, 2016 at 4:23pm
Print Post  
Tim,

Your comments were helpful.  I also found that I was exhausting my 2KB of SRAM with diagnostic strings in Serial.print() functions which I fixed by the use of the F() macro.

I have a question regarding the use of "extern".

By #including header files in any .cpp file which wants to see a constant or variable, with #include guards in them, why would I have to use "extern" at all?  Is it not recommended to simply #include relevant .h files everywhere needed?  That is to say, should I only #include a .h file with its associated .cpp file, and then use the "extern" modifier in other files which want visibility to a given constant or variable?
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12076
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Error - .h definitions not being recognized
Reply #5 - Jan 20th, 2016 at 4:30pm
Print Post  
These are good questions for the Arduino.cc forum. I am no expert with cpp so I stick to supporting the plugin

Interested to hear your findings thanks
  
Back to top
WWW  
IP Logged
 
arduinoUser2
Junior Member
**
Offline


Posts: 41
Joined: Dec 30th, 2015
Re: Error - .h definitions not being recognized
Reply #6 - Jan 21st, 2016 at 12:46am
Print Post  
Tim,

I did as you suggested and here is the gist of it.

A variable intended for global, project scope should be defined in one .h file only, e.g.,

Code (C++)
Select All
int myVariable; 



The definition could take other forms, depending on the intent, e.g.,

Code (C++)
Select All
const int myConstant = 1;
 



If it is then desired to use either myVariable or myConstant in another .cpp file, then in the .h file associated with that .cpp file, or in the .cpp file itself (outside any function definition), would then have the code,

Code (C++)
Select All
extern int myVariable; 



or

Code (C++)
Select All
extern int myConstant; 



I think I have that right.

The whole discussion is at https://forum.arduino.cc/index.php?topic=373340.0.  If you have the time to read it and get something more or different out of it, please post it here.
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12076
Location: United Kingdom
Joined: Apr 10th, 2010
Re: How to? .h definitions not being recognized
Reply #7 - Jan 21st, 2016 at 1:13am
Print Post  
thanks
« Last Edit: Jan 21st, 2016 at 1:48am by Tim@Visual Micro »  
Back to top
WWW  
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint