Vscode-arduino: Source subfolder and linking problem

Created on 29 Jan 2019  ยท  4Comments  ยท  Source: microsoft/vscode-arduino

I'm trying to reorganize my project with subfolders, but I can't seem to get the linker to find the cpp sources once they're moved. The extension seems to magically find cpp files next to the main ino and compile them properly, how do I configure this?

I want to go from this (working structure):

project/
โ”œโ”€โ”€ src/
    โ”œโ”€โ”€ app.ino
    โ”œโ”€โ”€ some_utility.h
    โ””โ”€โ”€ some_utility.cpp

To this structure:

project/
โ”œโ”€โ”€ src/
    โ”œโ”€โ”€ app.ino
    โ””โ”€โ”€ utilities/
         โ”œโ”€โ”€ some_utility.h
         โ””โ”€โ”€ some_utility.cpp

This is my c_cpp_properties.json file.

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "C:/Program Files (x86)/Arduino/hardware/arduino/avr/cores/arduino",
                "C:/Program Files (x86)/Arduino/hardware/arduino/avr/variants/standard",
                "C:/Program Files (x86)/Arduino/hardware/tools/avr/avr/include",
                "C:/Program Files (x86)/Arduino/hardware/tools/avr/avr/include/avr",
                "C:/Program Files (x86)/Arduino/hardware/tools/avr/lib/gcc/avr/5.4.0/include",
                "C:/Program Files (x86)/Arduino/hardware/arduino/avr/libraries/**",
                "${workspaceFolder}/src/**",
                "${workspaceRoot}/src/**"
            ],
            "forcedInclude": [
                "C:/Program Files (x86)/Arduino/hardware/arduino/avr/cores/arduino/Arduino.h"
            ],
            "intelliSenseMode": "gcc-x64",
            "compilerPath": "C:/Program Files (x86)/Arduino/hardware/tools/avr/bin/avr-g++.exe",
            "defines": [
                "USBCON",
                "ARDUINO=100",
                "ARDUINO_AVR_NANO",
                "ARDUINO_ARCH_AVR",
                "__AVR_ENHANCED__"
            ],
            "cStandard": "c11",
            "cppStandard": "c++17"
        }
    ],
    "version": 4
}

Note, its unclear if ${workspaceFolder} or ${workspaceRoot} should be used anymore.

Apologies if this is actually a vscode-cpptools question, its kind of tricky to tell where one ends and the other begins sometimes.

Most helpful comment

I figured it out. To avoid a Wisdom of the Ages situation. This has nothing to do with vscode-arduino, its a side effect of this change: https://github.com/arduino/arduino-builder/pull/148

Rearranging my folder tree so it looks like:

project/
โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ utilities/
โ”‚         โ”œโ”€โ”€ some_utility.h
โ”‚         โ””โ”€โ”€ some_utility.cpp
โ””โ”€โ”€ app.ino

Causes the sub folders to be properly included and compiled.

Morale of the story: your sketch must be in the root of the project, and any other files to compile must within the src directory.

All 4 comments

I figured it out. To avoid a Wisdom of the Ages situation. This has nothing to do with vscode-arduino, its a side effect of this change: https://github.com/arduino/arduino-builder/pull/148

Rearranging my folder tree so it looks like:

project/
โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ utilities/
โ”‚         โ”œโ”€โ”€ some_utility.h
โ”‚         โ””โ”€โ”€ some_utility.cpp
โ””โ”€โ”€ app.ino

Causes the sub folders to be properly included and compiled.

Morale of the story: your sketch must be in the root of the project, and any other files to compile must within the src directory.

I recently began working on someone else's code, and it took me an entire day to figure out all those linker errors were due to THIS. Shouldn't this be mentioned in the extension's instructions??

Hi @maikeriva
This has nothing to do with vscode-arduino. How your project is compiled depends solely on the Arduino-backend which is used by vscode-arduino. vscode-arduino can only operate within the limitations of what the backend provides.
This remains valid as well for the include path search mechanism and library design which people often do not understand.

The rule here is: If something doesn't compile: If it doesn't compile with the Arduino IDE it's a problem with your project. If your project compiles with the Arduino IDE but not with vscode-arduino: You can file an issue here.

BTW: you can help improving the documentation by writing a section for the documentation and file a pull request!
Regards, EW

Was this page helpful?
0 / 5 - 0 ratings

Related issues

formulahendry picture formulahendry  ยท  7Comments

khaled-hamam picture khaled-hamam  ยท  4Comments

boskoe picture boskoe  ยท  3Comments

micahalan picture micahalan  ยท  6Comments

clarkbremer picture clarkbremer  ยท  6Comments