Why I can't develop a C library in Arduino libraries folder ?

Hello,

How to write C libraries in Arduino libraries folder ?

Of course with a C library I get this error.

Arduino: 1.8.9 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\tvtc_\AppData\Local\Temp\cc7ua8lH.ltrans0.ltrans.o: In function `setup':

C:\Users\tvtc_\Desktop\lib1/lib1.ino:6: undefined reference to `mod_init()'

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino/Genuino Uno.

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

This is my code:

#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <Arduino.h>
#include "libtest.h"


void mod_init(void){

}
#ifndef LIBTEST_H_
#define LIBTEST_H_
  
  void mod_init(void);

#endif

Arduino code:

#include "libtest.h"


void setup() {
  // put your setup code here, to run once:
  mod_init();
}

void loop() {
  // put your main code here, to run repeatedly:

}

Is there a way that I can write C libraries for both Arduino IDE and Atmel Studio, without modifying the library extension.

This topic has come up a few times.

The small magnifying glass would have helped you find some good explanations and on such is HERE.

There are also a couple of links in that post which should also provide some use to you.

Bob.

For your header file

#ifndef LIBTEST_H_
#define LIBTEST_H_

#ifdef __cplusplus
extern "C" {
#endif
void mod_init(void);
#ifdef __cplusplus
}
#endif
#endif

OK

First I had to convert the file extension to .cpp because that's how Arduino IDE works.

But; for example, like, WIRE library that has twi.c in its utility folder. So how that library used a .c code ?

Look in the wire.cpp file.

extern "C" {
  #include <stdlib.h>
  #include <string.h>
  #include <inttypes.h>
  #include "utility/twi.h"
}

#include "Wire.h"

tf68:
Look in the wire.cpp file.

extern "C" {

#include <stdlib.h>
  #include <string.h>
  #include <inttypes.h>
  #include “utility/twi.h”
}

#include “Wire.h”

Thank you so much, now I learned what is the use of

extern "C"

wolfrose:
Thank you so much, now I learned what is the use of

extern "C"

See reply #2 :slight_smile:

sterretje:
See reply #2 :slight_smile:

Now there are 2 methods in #2 and #4.

Is your method specifically, defines the things in the middle to be C stuff ? If that's true then the 2nd method is more easier as I understood that method 2 means that everything after that extern identifier is a C code.

Am I right or I'm way too far of the truth :slight_smile: