Ld problems with multiple source files

Hi,

I am trying to compile a sketch with some additional c language files in the same directory. The compile and ld seems to miss the extra files.

How do I fix that?

Here is a directory listing from the directory containing my

$ ls -1
flexpwm_auxiliary.c
flexpwm_auxiliary.h
LCCD_Controller_250514.ino

And here is the output from the compile and link,

/home/nelson/.arduino15/packages/teensy/tools/teensy-compile/11.3.1/arm/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld: /home/nelson/.cache/arduino/sketches/4636C8904CF489ED832AFE7140B4A5E6/sketch/LCCD_Controller_250514.ino.cpp.o: in function `loop':
/home/nelson/Projects/TeensyDataAcquistion/SPI_Instrumenation_Project/Firmware/LCCD_Controller_250514/LCCD_Controller_250514.ino:3211: undefined reference to `pwm_baseaddr(unsigned char)'
/home/nelson/.arduino15/packages/teensy/tools/teensy-compile/11.3.1/arm/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld: /home/nelson/Projects/TeensyDataAcquistion/SPI_Instrumenation_Project/Firmware/LCCD_Controller_250514/LCCD_Controller_250514.ino:3212: undefined reference to `pwm_submodule(unsigned char)'
/home/nelson/.arduino15/packages/teensy/tools/teensy-compile/11.3.1/arm/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld: /home/nelson/Projects/TeensyDataAcquistion/SPI_Instrumenation_Project/Firmware/LCCD_Controller_250514/LCCD_Controller_250514.ino:3213: undefined reference to `pwm_channel(unsigned char)'
collect2: error: ld returned 1 exit status
exit status 1

Compilation error: exit status 1

After posting this, I recalled that the IDE picks up .cpp files, not .c files in the sketch directory.

Is there a way to get around that behavior, other than renaming .c files to .cpp?

You need extern "C" to tell the C++ compiler that the functions are written in C.

Modify the header file like this:

#ifdef __cplusplus
extern "C" {
#endif

// C function prototypes here

#ifdef __cplusplus
}
#endif

Explanation: the C++ compiler mangles the function names to include parameter types, e.g. pwm_submodule(unsigned char) becomes _Z13pwm_submoduleh.

Incorrect, .c files are compiled as well.

1 Like

Thank you, that works. But now my Serial.print() inside of the .c file, are undefined.

Is there an easy fix for that?

/home/nelson/Projects/TeensyDataAcquistion/SPI_Instrumenation_Project/Firmware/LCCD_Controller_250514/flexpwm_auxiliary.c: In function 'printbits16_':
/home/nelson/Projects/TeensyDataAcquistion/SPI_Instrumenation_Project/Firmware/LCCD_Controller_250514/flexpwm_auxiliary.c:161:5: error: 'Serial' undeclared (first use in this function)
  161 |     Serial.print((u16>>bits)&1);
      |     ^~~~~~
/home/nelson/Projects/TeensyDataAcquistion/SPI_Instrumenation_Project/Firmware/LCCD_Controller_250514/flexpwm_auxiliary.c:161:5: note: each undeclared identifier is reported only once for each function it appears in
/home/nelson/Projects/TeensyDataAcquistion/SPI_Instrumenation_Project/Firmware/LCCD_Controller_250514/flexpwm_auxiliary.c: In function 'pwm_dump':
/home/nelson/Projects/TeensyDataAcquistion/SPI_Instrumenation_Project/Firmware/LCCD_Controller_250514/flexpwm_auxiliary.c:182:3: error: 'Serial' undeclared (first use in this function)
  182 |   Serial.print("PWM pin");
      |   ^~~~~~
/home/nelson/Projects/TeensyDataAcquistion/SPI_Instrumenation_Project/Firmware/LCCD_Controller_250514/flexpwm_auxiliary.c:200:57: error: 'BIN' undeclared (first use in this function)
  200 |   Serial.print( "MCTRL2 MONPLL "); Serial.print(u16&0XF,BIN);
      |                                                         ^~~
exit status 1

Compilation error: 'Serial' undeclared (first use in this function)

Here are the .c and .h files. This is meant to add some functionality to the Teensy 4 pwm support, to facilitate low level access to the PWM registers.

flexpwm_auxiliary.c (10.0 KB)
flexpwm_auxiliary.h (360 Bytes)

An now another odd behavior.

I renamed the file back to .cpp. But the compile and ld still finds the now non-existent .c file!!!

So... I found the cache and manually deleted it. There must be some way to do this in a more organized or systematic way in the IDE.

If it is necessary to delete the cache when files are renamed, would that not seem to qualify as a bug or design error? It is certainly a foreseeable use case.

It is absolutely a bug. The Arduino developers are tracking the bug here:

If you have a GitHub account, you can subscribe to that thread to get notifications of any new developments related to this subject:


:red_exclamation_mark: Please only comment on the GitHub issue thread if you have new technical information that will assist with the resolution. General discussion and support requests are always welcome here on the Arduino Forum.


Great. So in other words, the answer to the question "how do we clear the cache", is that we should not need to clear the cache.

Still, that would seem to be an important function to have in the ide.