Using C/C++ libraries that haven't been built for Arduino

I am trying to read ID3 data from audio files from an SD card using a Teensy 4.1. I haven't found any libraries written for Teensy or Arduino that will do this, but this C library does.
From what I can see (using my limited C and library structure knowledge), the library seems like it has many similarities to Arduino libraries. Is it possible to get a library like this to compile in the Arduino IDE, perhaps with some minor reworking?

Try to use the library. Remove all #includes for which no Arduino files exist. Then fix all unresolved symbols. Report problems.

Yes. For suitable definition of "minor reworking."

There are two parts:

  1. Move files into Arduino-compatible places.
  2. Ensure build parameters and instructions get set appropriately.
  3. Replace any calls to non-Arduino infrastructure with Arduino-compatible calls. For instance, "fread(myfile,...)" would turn into something like "myfile.read()" - the library looks pretty nice - only two freads in the whole thing, so that shouldn't be hard.

Um. T HREE parts. Better have a comfy chair!

  1. Move files into Arduino-compatible places.

is easy enough! I've just put the main id3v2lib.c in a library folder, then all the other xxxx.h and xxxx.c files in a subfolder. This was partly how the library was organised already.

  1. Ensure build parameters and instructions get set appropriately.

I think I have managed this by adding #includes to reference all the xxxx.c files in id3v2lib.h (previously it just referenced the xxxx.h files, which in turn also did not include any xxxx.c files). What else do I need to look out for here?

  1. Replace any calls to non-Arduino infrastructure with Arduino-compatible calls.

I have replaced fread with file.read(), for example:

//fread(buffer, header_size+10, 1, file);
file.read(buffer, header_size+10);

although I'm not certain this can be a straight swap?

I've also replaced fopen and fclose with the corresponding SD functions, such as:

//file = fopen(file_name, "rb");
file = sd.open(file_name);

although from my understanding, in this case the fread was specifically reading as binary (hence the "rb"). Do I need to specify this for SD.open(), and if so how?

The only compile error I get so far is

Documents\Arduino\libraries\id3v2lib_arduino_1/id3v2lib/id3v2lib.c:555:80: error: 'tag_set_album_cover_from_bytes' was not declared in this scope
     tag_set_album_cover_from_bytes(album_cover_bytes, mimetype, image_size, tag);

which is odd because that function is declared right below where this error occurs in id3v2lib.c:

   .............
    mimetype = get_mime_type_from_filename(filename);
    tag_set_album_cover_from_bytes(album_cover_bytes, mimetype, image_size, tag);

    free(album_cover_bytes);
}

void tag_set_album_cover_from_bytes(char* album_cover_bytes, char* mimetype, int picture_size, ID3v2_tag* tag)
{
    ID3v2_frame* album_cover_frame = NULL;
    if( ! (album_cover_frame = tag_get_album_cover(tag)))
    {
        album_cover_frame = new_frame();
        add_to_list(tag->frames, album_cover_frame);
    }

    set_album_cover_frame(album_cover_bytes, mimetype, picture_size, album_cover_frame);
}

Any clue as to what might be causing this?

Definitely a comfy chair required!

The function is defined below. Add a declaration above the error or in the header file.

A forward declaration ends with a semicolon where a definition has the code block {...} appended to the function header.

The id3v2lib.h header file includes:

void tag_set_album_cover_from_bytes(char* album_cover_bytes, char* mimetype, int picture_size, ID3v2_tag* tag);

Does this declare it?

Ah...
4. Wrap any function prototypes in the .h files with extern "C" {}

#ifndef id3v2lib_frame_h
#define id3v2lib_frame_h

#include "types.h"
#include "constants.h"

#ifdef __cplusplus
extern "C"
{
#endif
ID3v2_frame* parse_frame(char* bytes, int offset, int version);
int get_frame_type(char* frame_id);
ID3v2_frame_text_content* parse_text_frame_content(ID3v2_frame* frame);
ID3v2_frame_comment_content* parse_comment_frame_content(ID3v2_frame* frame);
ID3v2_frame_apic_content* parse_apic_frame_content(ID3v2_frame* frame);
#ifdef __cplusplus
}
#endif
#endif

Right :slight_smile:

All done, but no joy here unfortunately! The "not declared" error remains.

We also have a conflict between this library and the SD library:

C:\Users\tompo\Documents\Arduino\libraries\id3v2lib_arduino_1/id3v2lib/header.c:45:10: error: cannot convert 'File' to 'FILE* {aka __sFILE*}' in assignment
     file = SD.open(file_name);
          ^

which is repeated everywhere the variable 'file' is used in conjunction with an SD function (file.open(), file.read(), file.close() ).
'file' is declared each time as:

FILE* file;

Solved the SD library issue by replacing

FILE *file;

with

File file;

as per the usual setup of the SD library.

I think you have not created a valid library structure. Please check the Arduino Library Specification to learn the accepted library structures:
https://arduino.github.io/arduino-cli/latest/library-specification/#layout-of-folders-and-files

The source code found in src folder and all its subfolders is compiled

Placing the current structure in a folder named "src" caused Arduino to lose the location of the library.

In...case the 1.0 library format is applied...the source code is searched from the library root folder and the utility folder

Renaming the subfolder to "utilities" and updating the #include addresses in id3v2lib.h ends us back where we started with the 'not delcared' error.

(source: Library specification - Arduino CLI)

In order for the library to be valid, you must have a header file in the root of the src subfolder. This file is used for library discovery.

The main header file (id3v2lib.h) was indeed in the 'src' folder. The structure was:

  • libraries
    • id3v2lib
      • src
        • id3v2lib.h
        • id3v2lib
          • all other .c and .h files
          • etc

Is this incorrect?

You must have a library.properties metadata file in the root of the library (libraries/id3v2lib/library.properties).

This doesn't appear to be a requirement for Arduino to recognise and compile a library? The fields are mostly to aid human users it seems. Unless I'm misreading it, I don't see how any of the fields metadata file would help with undeclared functions?
Arduino appears to be seeing all the files in the library, and throws errors if I make any incorrect changes to any of the files. I'm not sure my issue is with Arduino recognising the library, possibly more with how the library itself is written?

It is indeed a requirement for libraries that have the recursive layout. It is not a requirement for libraries of the "1.0" or "legacy" format:
https://arduino.github.io/arduino-cli/latest/library-specification/#old-library-format-pre-15

I'll quote your words back to you once again as you seem to have forgotten what we are discussing:

Looks correct. Please note that the IDE compiles and links source files (c/cpp/s) from all sub folders of src but the #include search path is limited to src (no sub folders). I.e. if you want to include a header which lives in src/subfolder from a file in src/ you need to qualify its path relative to src/ . (see here Library specification - Arduino CLI for details).

(Of course files living in the same folder will be found by #include )

I have added the library.properties file, so the whole structure is now:

  • libraries
    • id3v2lib
      • src
        • id3v2lib.h
        • library.properties
        • id3v2lib
          • all other .c and .h files
          • etc

which Arduino is able to read, you're correct.
However, the compiler still shows:

Documents\Arduino\libraries\id3v2lib_arduino_1\src/id3v2lib/id3v2lib.c:564:80: error: 'tag_set_album_cover_from_bytes' was not declared in this scope
     tag_set_album_cover_from_bytes(album_cover_bytes, mimetype, image_size, tag);
                                                                                ^

and a couple more errors of the same type for different functions. Should this new structure have fixed this?

The file must be here:

libraries/id3v2lib/library.properties

Your diagram shows it is here:

libraries/id3v2lib/src/library.properties

Please share your current version of the library with us. You can put it in a .zip file and then attach it to a reply here on the forum. Or if you are developing the library in a fork on GitHub, post a link to it.