Linking against user library fails

Hi !

Liking against my own library fails.

I followed the tuto there : Arduino - Libraries

I created a snippet containing one line int the setup() which call the simple function int parseCmd(int i) : see testLib.ino.

The function parseCmd() is defined in userLib.c file which is located in C:\Users\sylwa\Documents\Arduino\libraries\userLib directory along with its header.

I include this libreary on my project, compilation is fine.

Linking fails :

Linking everything together…
“C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-gcc” -w -Os -g -flto -fuse-linker-plugin -Wl,–gc-sections -mmcu=atmega328p -o “C:\Users\sylwa\AppData\Local\Temp\arduino_build_395256/testLib.ino.elf” “C:\Users\sylwa\AppData\Local\Temp\arduino_build_395256\sketch\testLib.ino.cpp.o” “C:\Users\sylwa\AppData\Local\Temp\arduino_build_395256\libraries\userLib\userLib.c.o” “C:\Users\sylwa\AppData\Local\Temp\arduino_build_395256/core\core.a” “-LC:\Users\sylwa\AppData\Local\Temp\arduino_build_395256” -lm
C:\Users\sylwa\AppData\Local\Temp\ccE17soN.ltrans0.ltrans.o: In function `setup’:

S:\svn\srGir\perso\knowledge\Electronic\numeric\Arduino\userLib\testLib/testLib.ino:5: undefined reference to `parseCmd(int)’

collect2.exe: error: ld returned 1 exit status

Utilisation de la bibliothèque userLib prise dans le dossier : C:\Users\sylwa\Documents\Arduino\libraries\userLib (legacy)
exit status 1
Erreur de compilation pour la carte Arduino/Genuino Uno

May I ask you some help please ?

Cheers !


testLib.ino (175 Bytes)

userLib.h (159 Bytes)

userLib.c (66 Bytes)

Does userLib.c have to be called userLib.c and not userLib.cpp ?
If it must be .c, look here software - How to call C functions from Arduino sketch? - Arduino Stack Exchange

Also, if you are experimenting with libraries, copy these into the sketch folder and instead of saying this in your .ino file:
#include <userLib.h>
#include “userLib.h”

This way, you don’t have to copy code into the libraries folder.

post your code (in code tags) inline, never attach small files. Attachments are a pain to read!


You wrote your library in C. In order to make a C library usable in C++ code, you have to take some extra steps. You have to make sure that in C++ code all your library's external functions and variables are declared as extern "C".

This is what your header file should look like

#ifndef USER_LIB_H
#define USER_LIB_H

#ifdef __cplusplus
extern "C" {

int parseCmd(int i);

#ifdef __cplusplus

#endif // USER_LIB_H

P.S. There's no reason to include Arduino.h into this header file. You are not using anything from Arduino.h here. But if you will, then keep it included.