I'm have problem compiling a code.

ardutest.ino:
<!-- language-all: lang-cpp -->
    
    #include "mylib.h"
    #include <SD.h>

    void setup() {

    }

    void loop() {
      fn();
    }

mylib.h:

    #ifndef mylib_h
    #define mylib_h

    #include "Arduino.h"


    void fn(File &_file);

    #endif

mylib.c:

    #include "mylib.h"

    void fn(File &_file)
    {
      _file = SD.open("LOG.TXT", FILE_WRITE);
      _file.println(42);
      _file.close();
    }

When checking code in Arduino IDE I have this error:


    In file included from sketch/mylib.c:1:0:
    mylib.h:7: error: expected ')' before '&' token
     void fn(File &_file);
              ^
    mylib.c:3: error: expected ')' before '&' token
     void fn(File &_file)
              ^
    Multiple libraries were found for "SD.h"
     Used: /home/mario/Arduino/libraries/SD
     Not used: /home/mario/Programs/arduino/libraries/SD
    exit status 1
    expected ')' before '&' token


But if I put SD.h in mylib.h (other files are the same):

    #ifndef mylib_h
    #define mylib_h

    #include "Arduino.h"
    #include <SD.h>

    void fn(File &_file);

    #endif

I have this error:

    In file included from /home/user/Arduino/libraries/SD/src/utility/Sd2Card.h:26:0,
                     from /home/user/Arduino/libraries/SD/src/utility/SdFat.h:29,
                     from /home/user/Arduino/libraries/SD/src/SD.h:20,
                     from sketch/mylib.h:5,
                     from sketch/mylib.c:1:
    /home/user/Arduino/libraries/SD/src/utility/Sd2PinMap.h:423:14: error: unknown type name 'pin_map_t'
     static const pin_map_t digitalPinMap[] = {
    
                  ^
    /home/user/Arduino/libraries/SD/src/utility/Sd2PinMap.h:447:69: error: 'pin_map_t' undeclared here (not in a function)
     static const uint8_t digitalPinCount = sizeof(digitalPinMap)/sizeof(pin_map_t);
    
                                                                         ^
    /home/user/Arduino/libraries/SD/src/utility/Sd2PinMap.h: In function 'getPinMode':
    /home/user/Arduino/libraries/SD/src/utility/Sd2PinMap.h:455:32: error: request for member 'ddr' in something not a structure or union
         return (*digitalPinMap[pin].ddr >> digitalPinMap[pin].bit) & 1;
    
                                    ^
    /home/user/Arduino/libraries/SD/src/utility/Sd2PinMap.h:455:58: error: request for member 'bit' in something not a structure or union
         return (*digitalPinMap[pin].ddr >> digitalPinMap[pin].bit) & 1;
    
                                                              ^
    /home/user/Arduino/libraries/SD/src/utility/Sd2PinMap.h: In function 'setPinMode':
    /home/user/Arduino/libraries/SD/src/utility/Sd2PinMap.h:464:26: error: request for member 'ddr' in something not a structure or union
           *digitalPinMap[pin].ddr |= 1 << digitalPinMap[pin].bit;
    
                              ^
    /home/user/Arduino/libraries/SD/src/utility/Sd2PinMap.h:464:57: error: request for member 'bit' in something not a structure or union
           *digitalPinMap[pin].ddr |= 1 << digitalPinMap[pin].bit;
    
                                                             ^
    /home/user/Arduino/libraries/SD/src/utility/Sd2PinMap.h:466:26: error: request for member 'ddr' in something not a structure or union
           *digitalPinMap[pin].ddr &= ~(1 << digitalPinMap[pin].bit);
    
                              ^
    /home/user/Arduino/libraries/SD/src/utility/Sd2PinMap.h:466:59: error: request for member 'bit' in something not a structure or union
           *digitalPinMap[pin].ddr &= ~(1 << digitalPinMap[pin].bit);
    
                                                               ^
    /home/user/Arduino/libraries/SD/src/utility/Sd2PinMap.h: In function 'fastDigitalRead':
    /home/user/Arduino/libraries/SD/src/utility/Sd2PinMap.h:475:32: error: request for member 'pin' in something not a structure or union
         return (*digitalPinMap[pin].pin >> digitalPinMap[pin].bit) & 1;
    
                                    ^
    /home/user/Arduino/libraries/SD/src/utility/Sd2PinMap.h:475:58: error: request for member 'bit' in something not a structure or union
         return (*digitalPinMap[pin].pin >> digitalPinMap[pin].bit) & 1;
    
                                                              ^
    /home/user/Arduino/libraries/SD/src/utility/Sd2PinMap.h: In function 'fastDigitalWrite':
    /home/user/Arduino/libraries/SD/src/utility/Sd2PinMap.h:484:26: error: request for member 'port' in something not a structure or union
           *digitalPinMap[pin].port |= 1 << digitalPinMap[pin].bit;
    
                              ^
    /home/user/Arduino/libraries/SD/src/utility/Sd2PinMap.h:484:58: error: request for member 'bit' in something not a structure or union
           *digitalPinMap[pin].port |= 1 << digitalPinMap[pin].bit;
    
                                                              ^
    /home/user/Arduino/libraries/SD/src/utility/Sd2PinMap.h:486:26: error: request for member 'port' in something not a structure or union
           *digitalPinMap[pin].port &= ~(1 << digitalPinMap[pin].bit);
    
                              ^
    /home/user/Arduino/libraries/SD/src/utility/Sd2PinMap.h:486:60: error: request for member 'bit' in something not a structure or union
           *digitalPinMap[pin].port &= ~(1 << digitalPinMap[pin].bit);
    
                                                                ^
    In file included from /home/user/Arduino/libraries/SD/src/utility/SdFat.h:29:0,
                     from /home/user/Arduino/libraries/SD/src/SD.h:20,
                     from sketch/mylib.h:5,
    Multiple libraries were found for "SD.h"
     Used: /home/user/Arduino/libraries/SD
     Not used: /home/user/Programs/arduino/libraries/SD
                     from sketch/mylib.c:1:
    /home/user/Arduino/libraries/SD/src/utility/Sd2Card.h: At top level:
    /home/user/Arduino/libraries/SD/src/utility/Sd2Card.h:70:1: error: initializer element is not constant
     uint8_t const  SD_CHIP_SELECT_PIN = SS;
    
     ^
    /home/user/Arduino/libraries/SD/src/utility/Sd2Card.h:79:1: error: initializer element is not constant
     uint8_t const  SPI_MOSI_PIN = MOSI;
    
     ^
    /home/user/Arduino/libraries/SD/src/utility/Sd2Card.h:81:1: error: initializer element is not constant
     uint8_t const  SPI_MISO_PIN = MISO;
    
     ^
    /home/user/Arduino/libraries/SD/src/utility/Sd2Card.h:83:1: error: initializer element is not constant
     uint8_t const  SPI_SCK_PIN = SCK;
    
     ^
    /home/user/Arduino/libraries/SD/src/utility/Sd2Card.h:176:1: error: unknown type name 'class'
     class Sd2Card {
    
     ^
    /home/user/Arduino/libraries/SD/src/utility/Sd2Card.h:176:15: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
     class Sd2Card {
    
                   ^
    In file included from /home/user/Arduino/libraries/SD/src/utility/SdFat.h:30:0,
                     from /home/user/Arduino/libraries/SD/src/SD.h:20,
                     from sketch/mylib.h:5,
                     from sketch/mylib.c:1:
    /home/user/Arduino/libraries/SD/src/utility/FatStructs.h:405:1: error: initializer element is not constant
     uint8_t const DIR_ATT_FILE_TYPE_MASK = (DIR_ATT_VOLUME_ID | DIR_ATT_DIRECTORY);
    
     ^
    In file included from /home/user/.arduino15/packages/arduino/hardware/avr/1.6.21/cores/arduino/Print.h:27:0,
                     from /home/user/Arduino/libraries/SD/src/utility/SdFat.h:31,
                     from /home/user/Arduino/libraries/SD/src/SD.h:20,
                     from sketch/mylib.h:5,
                     from sketch/mylib.c:1:
    /home/user/.arduino15/packages/arduino/hardware/avr/1.6.21/cores/arduino/Printable.h:25:1: error: unknown type name 'class'
     class Print;
     ^
    /home/user/.arduino15/packages/arduino/hardware/avr/1.6.21/cores/arduino/Printable.h:33:1: error: unknown type name 'class'
     class Printable
     ^
    /home/user/.arduino15/packages/arduino/hardware/avr/1.6.21/cores/arduino/Printable.h:34:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
     {
     ^
    In file included from /home/user/Arduino/libraries/SD/src/utility/SdFat.h:31:0,
                     from /home/user/Arduino/libraries/SD/src/SD.h:20,
                     from sketch/mylib.h:5,
                     from sketch/mylib.c:1:
    /home/user/.arduino15/packages/arduino/hardware/avr/1.6.21/cores/arduino/Print.h:37:1: error: unknown type name 'class'
     class Print
     ^
    /home/user/.arduino15/packages/arduino/hardware/avr/1.6.21/cores/arduino/Print.h:38:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
     {
     ^
    In file included from /home/user/Arduino/libraries/SD/src/SD.h:20:0,
                     from sketch/mylib.h:5,
                     from sketch/mylib.c:1:
    /home/user/Arduino/libraries/SD/src/utility/SdFat.h:39:1: error: unknown type name 'class'
     class SdVolume;
    
     ^
    /home/user/Arduino/libraries/SD/src/utility/SdFat.h:55:1: error: initializer element is not constant
     uint8_t const O_RDONLY = O_READ;
    
     ^
    /home/user/Arduino/libraries/SD/src/utility/SdFat.h:59:1: error: initializer element is not constant
     uint8_t const O_WRONLY = O_WRITE;
    
     ^
    /home/user/Arduino/libraries/SD/src/utility/SdFat.h:61:1: error: initializer element is not constant
     uint8_t const O_RDWR = (O_READ | O_WRITE);
    
     ^
    /home/user/Arduino/libraries/SD/src/utility/SdFat.h:63:1: error: initializer element is not constant
     uint8_t const O_ACCMODE = (O_READ | O_WRITE);
    
     ^
    /home/user/Arduino/libraries/SD/src/utility/SdFat.h:94:1: error: initializer element is not constant
     uint8_t const FAT_FILE_TYPE_MIN_DIR = FAT_FILE_TYPE_ROOT16;
    
     ^
    /home/user/Arduino/libraries/SD/src/utility/SdFat.h:137:1: error: unknown type name 'class'
     class SdFile : public Print {
    
     ^
    /home/user/Arduino/libraries/SD/src/utility/SdFat.h:137:14: error: expected '=', ',', ';', 'asm' or '__attribute__' before ':' token
     class SdFile : public Print {
    
                  ^
    /home/user/Arduino/libraries/SD/src/utility/SdFat.h:431:1: error: unknown type name 'class'
     class SdVolume {
    
     ^
    /home/user/Arduino/libraries/SD/src/utility/SdFat.h:431:16: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
     class SdVolume {
    
                    ^
    In file included from /home/user/Arduino/libraries/SD/src/SD.h:21:0,
                     from sketch/mylib.h:5,
                     from sketch/mylib.c:1:
    /home/user/Arduino/libraries/SD/src/utility/SdFatUtil.h: In function 'FreeRam':
    /home/user/Arduino/libraries/SD/src/utility/SdFatUtil.h:43:7: error: 'reinterpret_cast' undeclared (first use in this function)
       if (reinterpret_cast<int>(__brkval) == 0) {
    
           ^
    /home/user/Arduino/libraries/SD/src/utility/SdFatUtil.h:43:7: note: each undeclared identifier is reported only once for each function it appears in
    /home/user/Arduino/libraries/SD/src/utility/SdFatUtil.h:43:24: error: expected expression before 'int'
       if (reinterpret_cast<int>(__brkval) == 0) {
    
                            ^
    /home/user/Arduino/libraries/SD/src/utility/SdFatUtil.h:45:36: error: expected expression before 'int'
         free_memory = reinterpret_cast<int>(&free_memory)
    
                                        ^
    /home/user/Arduino/libraries/SD/src/utility/SdFatUtil.h:49:36: error: expected expression before 'int'
         free_memory = reinterpret_cast<int>(&free_memory)
    
                                        ^
    /home/user/Arduino/libraries/SD/src/utility/SdFatUtil.h: In function 'SerialPrint_P':
    /home/user/Arduino/libraries/SD/src/utility/SdFatUtil.h:62:52: error: 'Serial' undeclared (first use in this function)
       for (uint8_t c; (c = pgm_read_byte(str)); str++) Serial.write(c);
    
                                                        ^
    /home/user/Arduino/libraries/SD/src/utility/SdFatUtil.h: In function 'SerialPrintln_P':
    /home/user/Arduino/libraries/SD/src/utility/SdFatUtil.h:72:3: error: 'Serial' undeclared (first use in this function)
       Serial.println();
    
       ^
    In file included from sketch/mylib.h:5:0,
                     from sketch/mylib.c:1:
    /home/user/Arduino/libraries/SD/src/SD.h: At top level:
    /home/user/Arduino/libraries/SD/src/SD.h:26:1: error: unknown type name 'namespace'
     namespace SDLib {
     ^
    /home/user/Arduino/libraries/SD/src/SD.h:26:17: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
     namespace SDLib {
                     ^
    /home/user/Arduino/libraries/SD/src/SD.h:119:1: error: unknown type name 'using'
     using namespace SDLib;
     ^
    /home/user/Arduino/libraries/SD/src/SD.h:119:17: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'SDLib'
     using namespace SDLib;
                     ^
    /home/user/Arduino/libraries/SD/src/SD.h:123:14: error: expected '=', ',', ';', 'asm' or '__attribute__' before ':' token
     typedef SDLib::File    SDFile;
                  ^
    /home/user/Arduino/libraries/SD/src/SD.h:124:14: error: expected '=', ',', ';', 'asm' or '__attribute__' before ':' token
     typedef SDLib::SDClass SDFileSystemClass;
                  ^
    In file included from sketch/mylib.c:1:0:
    mylib.h:7: error: expected ')' before '&' token
     void fn(File &_file);
                  ^
    mylib.c:3: error: expected ')' before '&' token
     void fn(File &_file)
                  ^
    exit status 1
    expected ')' before '&' token

If I use SD.h function in mylib, I need this in mylib.h file. Why is it happens?

The first error is about passing object as function argument. I'm following sample I found in others forum, then I don't know why it isn't working.


[SOLVED]
I rename mylib.c to mylib.cpp and solved the problem.