Go Down

Topic: IDE include path bug (Read 746 times) previous topic - next topic

dack

There is a serious issue with include paths when using .cpp files. It can be demonstrated as follows:

Create a new sketch using IDE version 1.0.5
Add a new file called "test.cpp"
Paste the following code into test.cpp:
Code: [Select]
#include <SPI.h>
void test(){SPI.begin();}

Attempt to compile. It fails as follows (verbose detail shown):
Code: [Select]
C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=105 -IC:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino -IC:\Program Files (x86)\Arduino\hardware\arduino\variants\standard C:\Users\myuser\AppData\Local\Temp\build5595337925678702317.tmp\foo.cpp -o C:\Users\myuser\AppData\Local\Temp\build5595337925678702317.tmp\foo.cpp.o
foo.cpp:1:17: warning: SPI.h: No such file or directory
foo.cpp: In function 'void test()':
foo.cpp:2: error: 'SPI' was not declared in this scope


The same code compiles fine when pasted into the main file instead of a separate .cpp file. The issue appears to be the generation of the include paths for the g++ command. When using a .cpp file, the include path for SPI.h is not added automatically. Nor can I find any options to manually add it. The result is that libraries can't be used from a .cpp file.

Coding Badly

When using a .cpp file, the include path for SPI.h is not added automatically. Nor can I find any options to manually add it.


Add...

Code: [Select]
#include <SPI.h>

...to top of the sketch (INO or PDE file).

_qrt

Please note the 'Add' to .ino file. I read this post and 'moved' the #include from the .h to the .ino and it did not work. It was only by chance that I found out that it works if the #include is in the .ino and the .h (for whatever reason).

PaulS

Quote
There is a serious issue with include paths when using .cpp files. It can be demonstrated as follows:
No, there isn't. The bug involves you not understanding the build process, having not read the section of the reference page dealing with the build process.

The sketch is converted to a cpp file, and it, and all files on other tabs, are copied to a build directory. Any file referenced IN THE SKETCH, is also copied, along with its source file, if relevant. Then, the compiler runs.

Since SPI.h was not referenced in the sketch, it was not copied SO IT IS NOT AVAILABLE to include in the other file.

westfw

#4
Feb 07, 2015, 11:05 am Last Edit: Feb 07, 2015, 11:06 am by westfw
The Arduino IDE pre-preprocessin reads the .ino file in order to build the search paths (for .h files) used during the compile of all the files in the project.  So if a library .h file isn't mentioned in the .ino file, it's directory isn't in the search paths, and compiles of the .cpp files won't find it.
(Source code isn't actually copied, except for the sketch .ino files that are all written to a .cpp file in the build directory.
For example, here's the IDE compiling wire.cpp:
/Applications/arduino/Arduino-1.0.6.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106 -I/Applications/arduino/Arduino-1.0.6.app/Contents/Resources/Java/hardware/arduino/cores/arduino -I/Applications/arduino/Arduino-1.0.6.app/Contents/Resources/Java/hardware/arduino/variants/standard -I/Applications/arduino/Arduino-1.0.6.app/Contents/Resources/Java/libraries/Wire -I/Applications/arduino/Arduino-1.0.6.app/Contents/Resources/Java/libraries/Wire/utility /Applications/arduino/Arduino-1.0.6.app/Contents/Resources/Java/libraries/Wire/Wire.cpp -o /var/folders/jz/5yb8f2hr8xjcpf0059bsfz4r0000gn/T/build7578358985282342496.tmp/Wire/Wire.cpp.o

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy