1

I am making a multi-file arduino project, but the compiler can't recognize a function. I am using the arduino-cli version 0.10.0

This is the error I received

undefined reference to pin_init() collect2.exe: error: ld returned 1 exit status Error during build: exit status 1

This is the command:

arduino-cli compile -b arduino:avr:uno C:\Users\Cioccarelli\Desktop\transmitterCopy\main

main.ino:

#include "C:\Users\Cioccarelli\Desktop\transmitter\automation\automation.h"

void setup() {
    pin_init();
}

void loop() {}

automation.h:

#pragma once

// This is where the components input pins are stored, they are put here by the add_pin function
byte pinSetup[0];
byte pinSetupSize = 0;

// This function is called in the setup() and it initializes every pin listed in pinSetup[] as input
void pin_init();

automation.cpp:

#include "C:\Users\Cioccarelli\Desktop\transmitter\automation\automation.h"

extern pinSetup;
extern pinSetupSize;

void pin_init() {
    for (int i = 0; i < pinSetupSize; i++)
        pinMode(i, INPUT);
}

I think this issue might have to do with the fact that I don't completely understand file inclusion, but I haven't found anything useful in my research

On the left is the file setup

Update: today I rebuilt the project with different names and I cut everything which wasn't related to the problem, in hope to identify it.

new main:

#include "C:\Arduino\transmitter\nightmare\nightmare.h"

void setup() {
    pin_init();
}

void loop() {}

new automation.h (nightmare.h):

#pragma once

// This function is called in the setup() and it initializes every pin listed 
//in pinSetup[] as input
void pin_init();

new automation.cpp (nightmare.cpp):

#include "C:\Arduino\transmitter\nightmare\nightmare.h"

void pin_init() {}

new file structure:

enter image description here

I also made a copy and made some changes so it could become a C++ file (substituting the setup() with main() and replace every "byte" type with "int")

I then put it in Visual studio, it compiled perfectly. The arduino IDE gives the same error

2
  • To be simple, put all files into one directory, and do #include "automation.h". Since you have a small project, this can work out.
    – Linny
    Jul 28 '20 at 22:52
  • @Linny I know this would work, but I want to do things this way because I want to learn how to manage multiple directories and because this is a very early stage of the project, in the future that kind of solution would work very poorly Jul 29 '20 at 15:44
1

The compiler didn't have a problem; the linker did. That is, once your main function was compiled (correctly) to call pin_init(), the loader tried to put together all of the compiled modules and link up calls that were not satisfied locally - within the same file - to functions defined in another module. That is when this message got generated. No such function definition was found, despite the declaration of it in main.ino.

If you can compile this with the Arduino IDE, watch what it does: It will compile each of your source-code modules, one at a time. Then link all of your modules' compiled versions together to produce the .hex file (that will eventually get uploaded to the Arduino). You need to mimic that when you're compiling & loading from the command line.

Update:

today I rebuilt the project with different names

Is your .cpp file in the same folder as your .ino file? Or in another folder, such as the folder containing your .h file? I suspect it's the latter. When I built your project it in the Arduino IDE, it succeeded when the .ino and the .h were in the same folder, failed exactly as you described when I moved the .cpp file to where the .h file is.

The solution goes back to what I suggested earlier; that no definition of pin_input() was found, as the .cpp file containing it wasn't seen by the IDE (or your command-line compiler) and therefore not compiled and linked. Each of your files needs to be compiled and all of them together need to be linked, for the external calls to be satisfied.

With the IDE, put your project's .ino and .cpp files in the same folder so the IDE will see and compile each of them and know what to files to link. The IDE won't give you much flexibility for locating your .cpp files.

With the command line compiler, you can put your files wherever you will, make a separate[1] compile only run for each of them, then make another run of the linker component[2] naming all of the compiled units (the IDE names them as .d) and provide the path to your library folder(s).

[1] I'm not familiar with the command-line compiler's command-line syntax; maybe you're able to specify a number of compiles with one command.
[2] Again, IDK whether you need invoke the linker with the same command but with a link-only option switch, or you'll need to invoke the linker yourself, on the command line.

1
  • the arduino IDE gives the same error, I made some additions to the question Jul 31 '20 at 14:57
0

The Arduino IDE (and I guess arduino-cli behaves the same) will try to link into your project every source file that lives in the same forlder as the main .ino file.

Try this: get rid of the “automation” and “main” subfolders, put everything at the root of the project, and try to build again. If you end up with too much stuff in the root, you may consider the possibility of creating your own libraries, but that's for another day...

0

I got very similar error when I forgot to declare all the variables in the "class.cpp". The "class.h" had a declaration "class XYZ { byte test; };", but in the "class.cpp" I forgot to write "byte XYZ::test;".

1
  • 1
    I don't see the similarity
    – Juraj
    Oct 7 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.